際際滷

際際滷Share a Scribd company logo
C++ Loops
What are Loops in C++?
There may arise some situations during programming where programmers
need to execute a block of code several times (with slight variations
sometimes). In general, statements get executed sequentially with a C++
program, one statement followed by another. C++ provides statements for
several control structures along with iteration/repetition capability that allows
programmers to execute a statement or group of statements multiple times.
C++ supports following types of loops:
 while loops
 do while loops
 for loops
All are slightly different and provides loops for different situations.
Figure - Flowchart of Looping:
FOR - for loops are the most useful type. The syntax for a for loop is
for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}
The variable initialization allows you to either declare a variable and give it a
value or give a value to an already existing variable. Second, the condition
tells the program that while the conditional expression is true the loop should
continue to repeat itself. The variable update section is the easiest way for a
for loop to handle changing of the variable. It is possible to do things like x++,
x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could
call other functions that do nothing to the variable but still have a useful effect
on the code. Notice that a semicolon separates each of these sections, that
is important. Also note that every single one of the sections may be empty,
though the semicolons still have to be there. If the condition is empty, it is
evaluated as true and the loop will repeat until something else stops it.
void main()
{
for (i = 1; i <= 10; i++)
{
Cout<< "Hello Worldn";
}
}
While Loop
While studying for loop we have seen that the number of iterations is
known beforehand, i.e. the number of times the loop body is needed to be
executed is known to us. while loops are used in situations where we do
not know the exact number of iterations of loop beforehand. The loop
execution is terminated on the basis of test condition.
Syntax:
We have already stated that a loop is mainly consisted of three statements
 initialization expression, test expression, update expression. The syntax
of the three loops  For, while and do while mainly differs on the placement
of these three statements.
initialization expression;
while (test_expression)
{
// statements
update_expression;
}
void main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6)
{
printf( "Hello Worldn");
// update expression
i++;
}
}
do while loop
In do while loops also the loop execution is terminated on the basis of test
condition. The main difference between do while loop and while loop is in
do while loop the condition is tested at the end of loop body, i.e do while
loop is exit controlled whereas the other two loops are entry controlled
loops.
Note: In do while loop the loop body will execute at least once irrespective
of test condition.
Syntax:
initialization expression;
do
{
// statements
update_expression;
} while (test_expression);
void main()
{
int i = 2; // Initialization expression
do
{
// loop body
cout << "Hello Worldn";
// update expression
i++;
} while (i < 1); // test expression
}

More Related Content

Loops in c++

  • 1. C++ Loops What are Loops in C++? There may arise some situations during programming where programmers need to execute a block of code several times (with slight variations sometimes). In general, statements get executed sequentially with a C++ program, one statement followed by another. C++ provides statements for several control structures along with iteration/repetition capability that allows programmers to execute a statement or group of statements multiple times. C++ supports following types of loops: while loops do while loops for loops All are slightly different and provides loops for different situations. Figure - Flowchart of Looping:
  • 2. FOR - for loops are the most useful type. The syntax for a for loop is for ( variable initialization; condition; variable update ) { Code to execute while the condition is true } The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable update section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could call other functions that do nothing to the variable but still have a useful effect on the code. Notice that a semicolon separates each of these sections, that is important. Also note that every single one of the sections may be empty, though the semicolons still have to be there. If the condition is empty, it is evaluated as true and the loop will repeat until something else stops it.
  • 3. void main() { for (i = 1; i <= 10; i++) { Cout<< "Hello Worldn"; } } While Loop While studying for loop we have seen that the number of iterations is known beforehand, i.e. the number of times the loop body is needed to be executed is known to us. while loops are used in situations where we do not know the exact number of iterations of loop beforehand. The loop execution is terminated on the basis of test condition. Syntax: We have already stated that a loop is mainly consisted of three statements initialization expression, test expression, update expression. The syntax of the three loops For, while and do while mainly differs on the placement of these three statements. initialization expression; while (test_expression) { // statements update_expression; } void main() { // initialization expression int i = 1;
  • 4. // test expression while (i < 6) { printf( "Hello Worldn"); // update expression i++; } } do while loop In do while loops also the loop execution is terminated on the basis of test condition. The main difference between do while loop and while loop is in do while loop the condition is tested at the end of loop body, i.e do while loop is exit controlled whereas the other two loops are entry controlled loops. Note: In do while loop the loop body will execute at least once irrespective of test condition. Syntax: initialization expression; do { // statements update_expression; } while (test_expression); void main() { int i = 2; // Initialization expression do {
  • 5. // loop body cout << "Hello Worldn"; // update expression i++; } while (i < 1); // test expression }