THE LOOP CONTROL STRUCTURE
5) CONTINUES STATEMENT
In some programming situations , we want to take the control to the beginning of the loop ,bypassing the statement inside the loop,which have not yet been executed. The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop.
6) THE DO-WHILE LOOPS
THE DO WHILE LOOP
do
{
this ;
and this ;
and this;
and this...
up to so on
} while (the condition is true);
there is minor difference between the working of while and do while loops. This difference betweenthe working of while and do while loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statement within the while loop. As against this,the do while tests the condition after having executed the statements within the loop.
7) DECISION USING WITCH
The control statement that allows us to make a decision form the number of choices is called a switch or more correctly a switch -case-default ,since these the three keywords go together to make up the control statement. They most often appear as follows.
switch(integer expression)
{
case constant 1;
do this ;
case constant 2 ;
do this;
case constant 3 ;
do this ;
default ;
do this ;
}
The integer expression following the keyword switch in any c expression that will yield an integer value. The keyword case is followed by an integer an integer or a character constant. Each constant in each case must be different from all the others. The "do this" lines in the above from of switch represent any valid C statement.
8) SWITCH VERSUS IF-ELSE LADDER.
There are some things that you simply can't do with a switch.
these are :
a) A float expression can't be tested using a switch.
b) Cases can never have variable expression.
c) Multiple cases can't use same expression. Thus whether following switch is illegal:
switch(a)
{
case 3
....
case 1+2;
....
}
a b and c above may lead you to believe that these are obvious , disadvantages with a switch, especially since there are weren't any such limitations with if-else. because the compiler generates a jump table for a switch during compilation. As a result during execution it simply refers the jump table to decide which case is satisfied .As against this , if-else are slower because the condition in them are evaluated at execution time.Thus a switch with 10 cases would work faster than an equivalent if-else ladder .If the 10th case is satisfied the jump tab;e would be referred and an if-else ladder 10 conditions would be evaluated at execution time which makes is slow. Note that a lookup in the jump table is faster than evaluation ,especially if the condition is complex.
9) THE GOTO KEYWORD
Avoid goto keyword ! They make make a C programmer 's life miserable .There is seldom a legitimate reason for using goto , and its use is one of the reasons that program become unreliable ,unreadable , and hard to debug . And yet many programmers find goto seductive.
In a difficult programming situation ,it seems so easy to use a goto to take the control where you want .However ,almost always ,there is a more elegant way of writing the same program using if,for,while,do-while, and switch .These constructs are far more logical and easy to understand.
The big problem with gotos is that when we do use them we can never be sure how we got to a certain point in our code. They obscure the flow of control . So as far as possible skip them. You can always get the job done without them, Trust me,with good programming skills goto can always be avoided .This is the first and last time that we are going to use goto in this book.
THE FOLLOWING TOPICS ARE COVERED IN THIS POST.
3) THE FOR LOOP
NESTING OF LOOPS
The versatility of the computer lies in its ability to perform a set of instruction repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction.
There are three methods by way of which we can repeat a part of a program. they are :
1) Using a for statement.
2) Using a while statement.
3) Using a do-while statement.
It is often the case in programming that you want to do something a fixed number of times, perhaps you want to calculate gross salaries of 100 different people, or you want to convert temperature from centigrade to fahrenheit for 25 cities. The while loop is ideally suited for hits.
WHILE LOOP FOR C AND C++ |
the form of while loop initialize loop counter ;
while(test loop counter using a condition)
{
do this ;
and this ;
increment loop counter ;
}
some rules for while loop when we use.
The statement within the while loop would keep on getting executed till the condition being tested the follows the body of the while loop.
In place of the condition there can be any other valid expression. So long as the expression evaluates to a non-zero value the statement within the loop would get executed.
3) THE FOR LOOP
Perhaps one reason why few programmers use while is that they are too busy using the for, which is probably the most popular looping instruction. The for allows us to specify three things about a looping instruction. The for allows us to specify three things about a loop in a single line :
a) setting a loop counter to an initial value.
b) Testing the loop counter to determine whether is the value has reached the number of repetitions desired.
c) Increasing the value of loop counter each time the program segment the loop has been executed.
The general form of for statement is an under.
for(initialize counter : test counter : increment counter)
{
do this
and this
and this :
}
4) THE BREAK STATEMENT
We often come across situations where we want to jump out of a loop instantly , without to get back to the conditional test.The keyword break allows us to do this . When break is encountered inside any loop control automatically passes to the first statement after the loop. A break is usually associated with an if.
a) setting a loop counter to an initial value.
b) Testing the loop counter to determine whether is the value has reached the number of repetitions desired.
c) Increasing the value of loop counter each time the program segment the loop has been executed.
for loop in c and c++ structure |
The general form of for statement is an under.
for(initialize counter : test counter : increment counter)
{
do this
and this
and this :
}
NESTING LOOPS
The way if statement can be nested , similarly whiles and fors can also be nested .4) THE BREAK STATEMENT
We often come across situations where we want to jump out of a loop instantly , without to get back to the conditional test.The keyword break allows us to do this . When break is encountered inside any loop control automatically passes to the first statement after the loop. A break is usually associated with an if.
5) CONTINUES STATEMENT
In some programming situations , we want to take the control to the beginning of the loop ,bypassing the statement inside the loop,which have not yet been executed. The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop.
6) THE DO-WHILE LOOPS
THE DO WHILE LOOP
do-while in c and c++ structure |
do
{
this ;
and this ;
and this;
and this...
up to so on
} while (the condition is true);
there is minor difference between the working of while and do while loops. This difference betweenthe working of while and do while loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statement within the while loop. As against this,the do while tests the condition after having executed the statements within the loop.
7) DECISION USING WITCH
The control statement that allows us to make a decision form the number of choices is called a switch or more correctly a switch -case-default ,since these the three keywords go together to make up the control statement. They most often appear as follows.
switch(integer expression)
{
case constant 1;
do this ;
case constant 2 ;
do this;
case constant 3 ;
do this ;
default ;
do this ;
}
The integer expression following the keyword switch in any c expression that will yield an integer value. The keyword case is followed by an integer an integer or a character constant. Each constant in each case must be different from all the others. The "do this" lines in the above from of switch represent any valid C statement.
8) SWITCH VERSUS IF-ELSE LADDER.
There are some things that you simply can't do with a switch.
these are :
a) A float expression can't be tested using a switch.
b) Cases can never have variable expression.
c) Multiple cases can't use same expression. Thus whether following switch is illegal:
switch(a)
{
case 3
....
case 1+2;
....
}
a b and c above may lead you to believe that these are obvious , disadvantages with a switch, especially since there are weren't any such limitations with if-else. because the compiler generates a jump table for a switch during compilation. As a result during execution it simply refers the jump table to decide which case is satisfied .As against this , if-else are slower because the condition in them are evaluated at execution time.Thus a switch with 10 cases would work faster than an equivalent if-else ladder .If the 10th case is satisfied the jump tab;e would be referred and an if-else ladder 10 conditions would be evaluated at execution time which makes is slow. Note that a lookup in the jump table is faster than evaluation ,especially if the condition is complex.
9) THE GOTO KEYWORD
Avoid goto keyword ! They make make a C programmer 's life miserable .There is seldom a legitimate reason for using goto , and its use is one of the reasons that program become unreliable ,unreadable , and hard to debug . And yet many programmers find goto seductive.
In a difficult programming situation ,it seems so easy to use a goto to take the control where you want .However ,almost always ,there is a more elegant way of writing the same program using if,for,while,do-while, and switch .These constructs are far more logical and easy to understand.
The big problem with gotos is that when we do use them we can never be sure how we got to a certain point in our code. They obscure the flow of control . So as far as possible skip them. You can always get the job done without them, Trust me,with good programming skills goto can always be avoided .This is the first and last time that we are going to use goto in this book.