0% found this document useful (0 votes)
80 views17 pages

For Loop: - The Most Popular Looping Instruction - The For Loop Allows Us To Specify Three Things in A Single Line

The for loop allows programmers to specify initialization, condition, and increment/decrement in a single line to iterate over a block of code a specified number of times. It initializes a counter variable, tests a condition on each loop, and increments/decrements the counter. The break statement exits the current loop, while continue skips remaining code in the current loop iteration. The do-while loop ensures its block runs at least once before checking its condition, differing from a regular while loop which may not run if the condition is false.

Uploaded by

Mir Fida Nadeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views17 pages

For Loop: - The Most Popular Looping Instruction - The For Loop Allows Us To Specify Three Things in A Single Line

The for loop allows programmers to specify initialization, condition, and increment/decrement in a single line to iterate over a block of code a specified number of times. It initializes a counter variable, tests a condition on each loop, and increments/decrements the counter. The break statement exits the current loop, while continue skips remaining code in the current loop iteration. The do-while loop ensures its block runs at least once before checking its condition, differing from a regular while loop which may not run if the condition is false.

Uploaded by

Mir Fida Nadeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

for loop

• The most popular looping instruction


• The for loop allows us to specify three things in a
single line
1. Initialization:
• Setting a loop counter to an initial value
2. Condition:
• Testing loop counter to check whether it reaches the desired
number of repetitions
3. Increment / decrement part:
• Increase or decrease the value of loop counter each
time when the statement within the loop are
executed
1
General form of for loop
;
for ( initialization ;
condition ) Incr/ decr
{
do this;
and this;
and this;
}

Incr/ decr stands for increment/ decrement

2
Sequence of execution of for loop
1 2 5 8 4 7
for ( initialization
; ; condition
) Incr/ decr
{
3 do this;
6 and this;
and this;
}

3
Cont.
• It is important to note that the initialization,
testing and increment part of a for loop can be
replaced by any valid expression.

4
Ways for writing for loop

5
Cont.

6
Cont.

7
Nested for loop
/* Demonstration of nested loops */
main( )
{
int r, c, sum ;
for ( r = 1 ; r <= 3 ; r++ ) /* outer loop */
{
for ( c = 1 ; c <= 2 ; c++ ) /* inner loop */
{
sum = r + c ;
printf ( "r = %d c = %d sum = %d\n", r, c, sum ) ;
}
}
}

8
Example program 1
• Write a program to find
the factorial value of any
number entered through
the keyboard.

9
Example program 2
• Write a program that displays the table of x up
to y. The value of x and y is input by the user.
e.g. x = 4 and y = 5
4x1=4
4x2=8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20

10
The break Statement
• There are situations where we want to jump out of a
loop instantly, without waiting 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.
• The keyword break, breaks the control only from the
loop in which it is placed

11
Cont.
• Example: Write a program to determine
whether a number is prime or not. A prime
number is one, which is divisible only by 1 or
itself.

12
break statement in nested loop
main( )
{
int i = 1 , j = 1 ;
while ( i++ <= 100 )
{
while ( j++ <= 200 )
{
if ( j == 150 )
break ;
else
printf ( "%d %d\n", i, j ) ;
}
}
}
13
The continue Statement
• continue statement allows to take the control
to the beginning of the loop, bypassing the
statements inside the loop, which have not yet
been executed
• A continue is usually associated with an if.

14
The do-while Loop
• Post test repetition structure
• The statement in the loop block are executed
at least once, whether the loops condition is
true or false
do
{
this ;
and this ;
and this ;
} while ( this condition is true ) ;

15
General form of do while loop

16
Example program

Program output
2
7

j = j + i;
i = i + j;

17

You might also like