loops
Loop Type
|
Description
|
1.while loop
|
Repeats a statement or group of statements while a
given condition is true. It tests the condition before executing the loop body. |
2.for loop
|
Execute a sequence of statements multiple times
and abbreviates the code that manages the loop variable. |
3.do...while loop
|
Like a while statement, except that it tests the condition
at the end of the loop body |
nested loops
|
You can use one or more loop inside any another
while, for or do..while loop. |
While Loop:
1. A while loop statement repeatedly executes
a target statement as long as a given condition is true.
Syntax: The syntax of a while loop
in C++ is:
while(condition)
{
statement(s);
}
|
Here, statement(s) may be a single
statement or a block of statements. The condition may be any expression,
and true is any nonzero value. The loop iterates while the condition is true.
EXAMPLE:
#include
<iostream.h>
int
main ()
{
//
Local variable declaration:
int
a = 10;
//
while loop execution
while(
a < 20 )
{
cout
<< "value of a: " << a << endl;
a++;
}
return
0;
}
FOR LOOP :
1. A for loop is a repetition
control structure that allows you to
efficiently write a loop that needs to execute a specific number of
times
2. Syntax:
for ( init; condition; increment )
{
statement(s);
}
|
Here is the flow of control in a for loop:
·
The init step is executed first, and only once. This
step allows you to declare and initialize any loop control variables. You are
not required to put a statement here, as long as a semicolon appears.
·
Next, the condition is evaluated. If it is true, the
body of the loop is executed. If it is false, the body of the loop does not
execute and flow of control jumps to the next statement just after the for
loop.
·
After the body of the for loop executes, the flow of control
jumps back up to the increment statement. This statement allows you to
update any loop control variables. This statement can be left blank, as long as
a semicolon appears after the condition.
·
The condition is now evaluated again. If it is true, the
loop executes and the process repeats itself (body of loop, then increment
step, and then again condition). After the condition becomes false, the for
loop terminates
EXAMPLE.
#include <iostream.h>
int main ()
{
for( int a
= 10; a < 20; a ++ ) // for loop
execution
{
cout
<< "value of a: " << a << endl;
}
return 0;
}
Do…while Loop:
1.
Unlike for and
while loops, which test the loop condition at the top of the loop,
the do...while loop checks its condition at the bottom of the loop.
2. A do...while loop is
similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
3. Syntax:
do
{
statement(s);
}while( condition );
|
·
Notice that the conditional expression appears at the end of
the loop, so the statement(s) in the loop execute once before the condition is
tested.
·
If the condition is true, the flow of control jumps back up
to do, and the statement(s) in the loop execute again. This process repeats
until the given condition becomes false
·
EXAMPLE:
#include
<iostream.h>
int
main ()
{
// Local variable declaration:
int
a = 10;
// do loop execution
do
{
cout
<< "value of a: " << a << endl;
a
= a + 1;
}while(
a < 20 );
return
0;
}
The following program uses a nested
for loop to find the prime numbers from 2 to 100:
#include
<iostream.h>
int
main ()
{
int
i, j;
for(i=2;
i<100; i++) {
for(j=2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) cout
<< i << " is prime\n";
}
return
0;
}
Loop Control Statements: Loop control statements change
execution from its normal sequence. When execution leaves a scope, all
automatic objects that were created in that scope are destroyed.
Control Statement
|
Description
|
|
break statement
|
Terminates the loop or switch statement
and transfers execution to the statement immediately following the loop or switch. |
|
continue statement
|
Causes the loop to skip the remainder of its
body and immediately retest its condition prior to reiterating. |
|
goto statement
|
Transfers control to the labeled statement.
Though it is not advised to use goto statement in your program. |
Break statement:
Ø
When the break statement is encountered inside a
loop, the loop is immediately terminated and program control resumes at the
next statement following the loop.
Ø
It can be used to
terminate a case in the switch statement
Ø
Syntax: break;
Ø
EX:
#include <iostream.h>
int main ()
{
int a = 10; // Local
variable declaration:
do // do
loop execution
{
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15)
{
break; // terminate the loop
}
}while( a < 20 );
return 0;
}
Continue statement: The continue statement
works somewhat like the break statement. Instead of forcing termination,
however, continue forces the next iteration of the loop to take place, skipping
any code in between.
For
the for loop, continue causes the conditional test and increment
portions of the loop to execute. For the while and do...while loops,
program control passes to the conditional tests.
Syntax: continue;
EX:
#include
<iostream.h>
int
main ()
{
int
a = 10; // Local
variable declaration
do
//
do loop execution
{
if( a == 15)
{
a = a + 1;
continue; // skip the iteration.
}
cout << "value of a: "
<< a << endl;
a = a + 1;
}while( a < 20 );
return
0;
}
Goto statement: A goto statement
provides an unconditional jump from the goto to a labeled statement in the same
function.
Syntax:
goto
label;
..
.
label: statement;
Where label is an
identifier that identifies a labeled statement. A labeled statement is any
statement that is preceded by an identifier followed by a colon (:).
EX:
#include
<iostream.h>
int
main ()
{
int a = 10; // Local variable
declaration:
LOOP:do // do loop execution
{
if( a == 15)
{
a = a + 1;
goto LOOP; //
skip the iteration.
}
cout << "value of a: "
<< a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}
Statement
|
Description
|
if statement
|
An if statement consists of a boolean
expression followed by one or more statements. |
if...else statement
|
An if statement can be followed by an
optional else statement, which executes when the boolean expression is false. |
switch statement
|
A switch statement allows a variable to
be tested for equality against a list of values. |
nested if statements
|
You can use one if or else if statement
inside another if or else if statement(s). |
nested switch statements
|
You can use one swicth statement inside
another switch statement(s). |
if statement:
·
An if statement
consists of a boolean expression followed by
one or more statements.
·
Syntax:
if(boolean_expression)
{
//
statement(s) will execute if the boolean
expression is true
}
|
If the
boolean expression evaluates to true, then the block of code inside the
if statement will be executed. If boolean expression evaluates to false, then
the first set of code after the end of the if statement (after the closing
curly brace) will be executed.
EX: #include <iostream.h>
int main ()
{ // local
variable declaration:
int a
= 10; // check the boolean
condition
if( a
< 20 )
{ // if condition is true
then print the following
cout
<< "a is less than 20;" << endl;
}
cout
<< "value of a is : " << a << endl;
return
0; }
if…else
statement:
· An if statement can be
followed by an optional else statement, which executes when the boolean
expression is false.
·
Syntax:
if(boolean_expression)
{
//
statement(s) will execute if the boolean expression is true
}
else
{
//
statement(s) will execute if the boolean expression is false
}
|
EXAMPLE:
#include
<iostream.h>
int
main ()
{
// local variable declaration:
int
a = 100;
// check the boolean condition
if(
a < 20 ) // if condition
is true then print the following
cout
<< "a is less than 20;" << endl;
}
else
{ // if
condition is false then print the following
cout
<< "a is not less than 20;" << endl;
}
cout
<< "value of a is : " << a << endl;
return
0;
}
No comments:
Post a Comment