Nothing Special   »   [go: up one dir, main page]

04 Loops in C++

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Loops in C++

While, for, Do while


Iterative or Repetition Statements
• Also known as looping statements (segment of code is executed
repeatedly)
• Generally 3 structures are used for loops
1. while
2. for
3. do while
Loop Statements Execution

• All looping statements include the following steps

1. Initialization of conditional variable

2. Test the control statement(Check the condition)

3. Execute the body of loop based on the condition

4. Update the conditional variable


while repetition structure
Start SYNTAX
initialize loop counter;
Initialize
while (condition)
{
Test Condition body statement (s);
increment or decrement loop
Stop counter;
Body of Loop }

Increment or Decrement
While Example Program
// Print the I Values Numbers from 0-10
#include <stdio.h>
int main()
{
int i;
i = 0;
while(i<=10)
{
printf(“The Value is %d\n”,i);
++i;
}
return 0;
}
Do While Repetition Structure
• do while structure first executes initialize loop counter;
the statements in the braces do
then checks the condition so
even if the condition is false {
body statements are executed statements;
once increment or decrement loop counter;

• It is called exit controlled }


structure
while (condition);
• do while ends with a semi colon
Start

Control Flow of
Initialize

do while Loop
Body of Loop

Increment or Decrement

True
Test Condition

False

Stop
#include <iostream>

int main ()
{
// variable declaration:
Program of do
int a = 10;
// do while loop execution
do
Example

{
cout<<“value of a is ”<<a<<endl;
a++;
while

} while( a < 20 ) ;

return 0;
}
FOR REPETITION STRUCTURE
• for loop is another repetitive control structure used to execute
statements repeatedly until condition becomes false
• for loop structure is divided by semicolons into three separate
expressions: the “Initialize expression”, the “test expression”, and the
“Increment/Decrement expression”

SYNTAX of for loop


for(Initialize counter variable ; Condition ; Increment/Decrement the counter variable)

{
Body Statements of the for loop;
}
Initialization

Control Flow of for


expression

Test
Expression Exit
/Condition False

True

Body of loop
loop

Increment /
Decrement
Expression
for loop structure Example Program:
#include <iostream>
using namespace std;
int main()
{
int counter;
for(counter = 1; counter <= 10; counter++ )
{
cout <<“counter = “<<counter<<endl;
}
return 0;
}
Nested Loops
• One loop inside another loop
• In nested loop the inner loop is executed first and then outer
Syntax of for Nested Loop:

for ( init; condition; increment )


{
for ( init; condition;
increment )
{
statement(s);
}
statement(s);
}
Loop Control Structures
• Loop control statements change execution from its normal sequence.

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 labelled statement.


Break Statements:
• 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.

• If you are using nested loops ( i.e. one loop inside another loop), the
break statement will stop the execution of the innermost loop and
start executing the next line of code after the block
#include <iostream>

EXAMPLE
PROGRAM
using namespace std;

Void main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
cout<<"value of a “<<a<<endl;
a++;
if( a > 15)
{
/* terminate the loop using break statement */
break;
}
}
}
#include <iostream>

CONTINUE STATEMENT using namespace std;


Void main ()
{
/* local variable definition */
• continue forces the next
int a = 10;
repetition of the loop to /* while loop execution */
take place, skipping any while( a < 20 )
code in between. {
cout<<"value of a “<<a<<endl;
a++;
• Example Program if( a == 15)
{
a++;
continue;
}
}
}
GOTO STATEMENT #include <iostream>
using namespace std;
void main ()
{
• goto statement /* local variable definition */
provides an int a = 10;
/* do loop execution */
unconditional jump LOOP : do
from the goto to a {
labeled: statement if( a == 15)
in the same {
/* skip the iteration */
function. a ++;
goto LOOP;
}
cout<<“value of a “<<a<<endl;
a++;
}while( a < 20 );
}

You might also like