0% found this document useful (0 votes)
14 views33 pages

Loops2

Uploaded by

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

Loops2

Uploaded by

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

Lecture

Repetitions (For Loop)


C Programming
 For Statement for Looping

 Nested Loops
For loop

 Another type of loop in c is the For loop.


 All the parts (priming, testing and updating)
are in one place.
 format:
for (prime expression; test expression; update
expression)
 Since the expressions are all in one place, many
people prefer for to while when the number of
iterations is known.
SYNTAX

for ( initialization ; test expression ; update )


{
0 or more statements to repeat

}
The for loop contains

 an initialization

 an expression to test for continuing

 an update to execute after each iteration


of the body
 For loops are good for creating definite
loops.
int counter;
1. Priming: Set 2. Test Condition: 3. Update: Update the
the start value. Set the stop value. value.

for (counter =1;counter <= 10;counter++)


printf ("%d\n", counter); Note that each section is
separated by a semicolon.
int num;

for ( num = 1 ; num <= 3 ; num++ )


{
printf( “ % d Potato \n ” , num );
}
num ?

int num;

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT
num 1

int num;

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT
num 1

int num;
true

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT
num 1

int num;

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
num 2

int num;

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
num 2

int num;
true

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
num 2

int num;

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
2Potato
num 3

int num;

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
2Potato
num 3

int num;
true

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
2Potato
num 3

int num;

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
2Potato
3Potato
num 4

int num;

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
2Potato
3Potato
num 4

int num;
false

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

OUTPUT

1Potato
2Potato
3Potato
num 4

int num;
false

for ( num = 1 ; num <= 3 ; num++ )

printf( “ % d Potato \n ” , num );

When the loop control condition


is evaluated and has value false, the
loop is said to be “satisfied” and
control passes to the statement
following the For statement.
1Potato
2Potato
3Potato
int count ;
for ( count = 4 ; count > 0 ; count-- )
{
printf( “ %d \n “ , count ) ;
}
printf ( “Done” ) ;

OUTPUT: 4
3
2
1
Done
int count;

for ( count = 0 ; count < 10 ; count++ )


{
printf( “*” );
}
**********

NOTE: the 10 asterisks are all on one line. Why?


 any expression may be more complex:
for (i=100*y; i>=1; i--)
for (i=100; i>=y/2; i--)
for (i=100; i>=1; i-=4*y)
 Increment may be negative:
for (i=100; i>=1; i--)
 This counts from 100 to 1.

 Increment may be greater than 1:


for (i=100; i>=5; i-=5)
 This counts from 100 to 5 in steps of 5
int count;

for (count = 0; count < 10; count++) ;


{
printf ( “*” );
}
 One * ! Why?
 the ; right after the ( ) means that the body
statement is a null statement
 in general, the Body of the for loop is whatever
statement immediately follows the ( )
 that statement can be a single statement, a
block, or a null statement
 actually, the code outputs one * after the loop
completes its counting to 10
 You can still end up with an infinite loop when
using for loops

for (counter=0; counter<=10; counter--)


 It is also possible to place a for
loop inside another for loop. Output:
**********
int rows, columns; **********
**********
Outer Loop
for (rows=1; rows<=5; rows++)
**********
{
**********
for (columns=1; columns<=10; columns++)
{
printf ("*");
}
printf ("\n");
} Inner Loop
#include <stdio.h> Output:
*
main ()
**
{
***
int rows, columns; Outer Loop
****

for (rows=1; rows<=5; rows++) *****


{
for (columns=1; columns<=rows; columns++)
{
Inner Loop printf ("*");
}
printf ("\n");
}
}
initialize outer loop
while ( outer loop condition )
{ ...

initialize inner loop


while ( inner loop condition )
{
inner loop processing and update
}
...
}
Write a program that displays the
multiplication tables ( 1 - 12 ).

1 x 1 = 1
1 x 2 = 2
.…
1 x 12 = 12
2 x 1 = 2
….
12 x 12 = 144

You might also like