A Quick Guide for C++ #3

Review of C++ language 


Control Statements

C++ provides us control structures, statements that can alter the flow of a sequence of instructions.

Compound statements

Compound statements or block is a group of statements which are separated by semicolons (:) and grouped together in a block enclosed in braces {} is called a compound statement.
Example:
{
temp = a;
a = b;
b = temp;
}


Types of control statements


C++ supports two basic control statements.
□Selection statements
□Iteration statements

Selection statements 

This statement allows us to select a statements or set of statements for execution based on some condition.
The different selection statements are:
1. if statement
ii. if-else statement
ili nested statement
iv. switch statement

if statement

This is the simplest form of if statement. This statement is a called as one-way branching. This statement is used to decide whether a statement or set
of statements should be executed or not. The decision  based on a condition  which can be evaluated to TRUE or FALSE.

if(test_condition)
Statement1;

Example: 
if (n = = 100)
  cout<<"n is 100";

The if-else statement

This statement is also called as two-way branching. It is used there are alternative statements need to be executed based on the condition. It executes some set of statements when the given condition is TRUE and if the condition is FALSE then other set of statements to be executed.
Syntax:


if(expression)
 {
 // statement will execute if
    the expression is true
 } 
else 
{ 
// statement(s) will execute if
    the expression is false 
}

Nested-if statement

An if statement may contain another if statement within it. Such an if statement is called as nested if statement.
Syntax:

if(test_condition1)
{
if(test_condition2)
{ 
Statement 1;
}
else
{ 
Statement 2;
}
if(test_condition3)
{ 
Statement 3;
}
Statement 4;
}


There are two forms of nested if statements:

Format 1 : if-else-if statement

This structure is also called as else-if ladder. This structure will be used to verify a range of values. This statement allows a choice to be made between different possible alternatives. A choice must be made between more than two possibilities
Syntax:


if(expression 1) {
   // Executes when the expression 1 is true
} else if(expression 2) {
   // Executes when the expression 2 is true
} else if(expression 3) {
   // Executes when the expression 3 is true
} else {
   // executes when the none of the above condition is true.
}
Format II:

This structure contains an if-else statement within another if-else statement
Syntax:
if(expression/condition1)       
           if(condition2)
                    statement1;
            else 
                     statement2;
else 
             if(condition3) 
                      statement3; 
              else 
                      statement4;

Switch statement

C++ has a built-in multiple-branch selection statement, switch. This successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that code is executed

if(expression){
 case label_1: statement _1;            
               break;
case label_2:  statement _2;     
               break 
 case label_n: statement _n;         
               break;
default      : default_statement;
}


Iteration statements or loops

Iteration statements are also called as loops. Loop is a statement that allows repeated execution of a set of instructions until certain condition is satisfied.
This conditionmay be predefined or post-defined. Loops have a purpose to repeat a
set of statements a certain number of times or some condition is fulfilled. We use three types of looping structures in C++,
Ø while loop
Ø do-while loop
Ø for loop

while loop

This looping structure is also called as pre-tested looping structure. This statement repeats the execution of a set of statements while the condition is TRUE.

Syntax:

while(test_condition)
{
Statement 1;
Statement2;
.............
Statement n;
}

Example:
C = 1;             
while(c <= 10)
           cout<<setw(4)<<c;

do-while loop

○This looping structure is also called as post-tested looping structure.
○Unlike while loop that test the loop condition at the beginning the do-while loop checks the condition after the execution of the statements in it.
○This means that a do-while loop always executes at least  once.
○Its functionality is exactly the same as the while loop except that the condition in the do while loop is evaluated after the execution of statement, instead of before
Syntax:.

do
{
 Statement 1;
 Statement 2;
 ......... 
Statement n;
}while(test_condition);

Example:
 c = 1;
 do
{
     cout<setw(4)<<c;
} while(c <= 10);

The for loop

○This statement is called as the fixed execution looping statement.
○ It is normally used when we know in advance exactly how many times a set of statements should be repeatedly executed again and again.
○It provides initialization, loop-end-condition and increment/decrement process statements in a single line.
○When one is aware of fixed number of iterations, then this looping structure is best suited.
Syntax:


for(initialization;condition; increment/decrement)
{
 Statement 1; 
Statement 2; 
........ 
Statement n;
}


Example:
for(i=0;i<=5;i++)
cout<<setw(4)<<i;


Jump statements or Transfer of control from within loop

Transfer of control within looping are used to
Ø Terminate the execution of loop
Ø Exiting a loop
Ø Half way through to skip the loop.

All these can be done by using breakexit, and continue statements.

break statement


○The break statement has two uses. You can use it to terminate a case in the switch statement.
○And you can also uses it to force immediate termination of a loop like, while, do-while and for, by passing the normal loop conditional test.
○When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement.

The general format of break statement is:
break;
Example:
for( n = 0; n < 100; n++)
       cout<<n;
if(n==10 )
break;

Above program segment prints the numbers 0 through 10 on the screen. Then the loop terminates because break causes immediate exit from the loop, overriding the conditional test   1<100.

continue statement

The continue statement causes the program to skip the rest of the loop in the current iteration as
if the end of the statement block had been reached, causing it to jump to the start of the following iteration

The continue statement works somewhat like break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between

The general form of the continue statement is:
continue;


exit() function

Just as you can break out of a loop, you can break out of a program by using the standard library function exit). This function causes immediate termination of the entire program forcing a return to the operating system. In effect, the exit() function acts as if it were breaking out of the entire program

The general form of the exit() function is:
exit();
OR
 void exit(int return_code);

The return code is used by some operating  systems  and may be used by calling  programs. By convention,  an exit code of 0 means that the program finished normally and any other value means that some errors or  unexpected results happend.

Comments

Popular Posts

Translate