advertisement

For Loop

50 %
50 %
advertisement
Information about For Loop
Technology

Published on November 26, 2008

Author: Ghaffar

Source: slideshare.net

advertisement

The wizards of For Loop A Lecture By Abdul Ghaffar Khan

‘ C’ and Iterative Logic To apply the Iterative logic we have three control structures in ‘C’ Language as listed below, for Loop while loop do while loop Ch – 8 For Loop By Abdul Ghaffar Khan

To apply the Iterative logic we have three control structures in ‘C’ Language as listed below,

for Loop

while loop

do while loop

The for Loop: for loop is used when a process is to repeat a fixed number of times. The structure of a for loop is as follows for(expression_l; expression_2; expression_3) any_instruction; Or for(expression_l; expression_2; expression_3) { body-of-IOOP } Ch – 8 For Loop By Abdul Ghaffar Khan

for loop is used when a process is to repeat a fixed number of times. The structure of a for loop is as follows

for(expression_l; expression_2; expression_3)

any_instruction;

Or

for(expression_l; expression_2; expression_3)

{

body-of-IOOP

}

Order of execution When the loop is started normally a control variable is initialized by executing expression_l. expression_2 is tested if it is false then control is transferred outside the loop otherwise do next step 3 Body of loop is executed. Value of control variable is altered by expression_3 Steps (2) to (4) are repeated until condition becomes false Ch – 8 For Loop By Abdul Ghaffar Khan for(exl; ex2; ex3) { body-of-IOOP }

When the loop is started normally a control variable is initialized by executing expression_l.

expression_2 is tested if it is false then control is transferred outside the loop otherwise do next step 3

Body of loop is executed.

Value of control variable is altered by expression_3

Steps (2) to (4) are repeated until condition becomes false

for(exl; ex2; ex3)

{

body-of-IOOP

}

Example 8.1 ( Print 10 natural numbers) Ch – 8 For Loop By Abdul Ghaffar Khan main() { int NBR; clrscr(); for(NBR=l; NBR<=10; ++NBR) printf(&quot;%d &quot;,NBR); getch() } Output: 2 3 4 5 6 7 8 9 10

Example 8.2 ( print a table ) main() { int n, p, i= 0; clrscr(); printf(&quot;Enter a number whose table is to print &quot;); scanf(&quot;%d&quot;, &n); printf(&quot;Table of %d &quot;, n); for (i=1; i<=12; ++i) { p = i* n; printf(&quot;%d * %4 = %d &quot; n, i, p ); . } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output Enter a number whose table is to print 6 6 * 1 = 6 6 * 2 = 12 6 * 3 =18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60 6 * 11 = 66 6 * 12 = 72

main()

{

int n, p, i= 0;

clrscr();

printf(&quot;Enter a number whose table is to print &quot;);

scanf(&quot;%d&quot;, &n);

printf(&quot;Table of %d &quot;, n);

for (i=1; i<=12; ++i)

{

p = i* n;

printf(&quot;%d * %4 = %d &quot; n, i, p ); .

}

getch();

}

Output

Enter a number whose table is to print 6

6 * 1 = 6

6 * 2 = 12

6 * 3 =18

6 * 4 = 24

6 * 5 = 30

6 * 6 = 36

6 * 7 = 42

6 * 8 = 48

6 * 9 = 54

6 * 10 = 60

6 * 11 = 66

6 * 12 = 72

Example 8.3 Write down a program to generate the following output Ch – 8 For Loop By Abdul Ghaffar Khan C8 lower left corner BC lower right corner

Write down a program to generate the following output

Example 8.3 void main(void) { int x, y; clrscr(); . printf (&quot;xc9&quot;); for (x=1; x<72 ; ++x) { printf(&quot;xcd&quot;) ; } printf(&quot;xBB &quot;); // drawing vertical lines for (x=1;x<10;++x) { if (x==5) printf(&quot;xBA Welcome To Learning C xBA &quot;); printf(&quot;xBA xBA &quot;); } Ch – 8 For Loop By Abdul Ghaffar Khan printf(&quot;xC8&quot;); // drawing bottom line for (x=1;x<72;++x) { printf(&quot;xcd&quot;); } printf(&quot;xBC &quot;) ; getch(); }

void main(void) {

int x, y;

clrscr(); .

printf (&quot;xc9&quot;);

for (x=1; x<72 ; ++x)

{

printf(&quot;xcd&quot;) ;

}

printf(&quot;xBB &quot;);

// drawing vertical lines

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

{

if (x==5)

printf(&quot;xBA Welcome To Learning C xBA &quot;);

printf(&quot;xBA xBA &quot;);

}

Multiple assignments in the for loop We can use more than one initialization statement and more than one Increment decrement statements however only one condition is allowed within a for loop. When used multiple assignments or increment commas separate each statement. for( assignment I, asslgnment2; Condition; Increment1, intement2) . { body of loop } Ch – 8 For Loop By Abdul Ghaffar Khan

We can use more than one initialization statement and more than one Increment decrement statements however only one condition is allowed within a for loop. When used multiple assignments or increment commas separate each statement.

for( assignment I, asslgnment2; Condition; Increment1, intement2) .

{

body of loop

}

Example 8.3 #include<stdio.h> #include<conio.h> void main(void) { int x, y; clrscr(); for(x = 1, y= 10; x<= 10; ++x, --y) printf(&quot;x = %d Y = %d &quot;,x,y); getch(); ; } Ch – 8 For Loop By Abdul Ghaffar Khan Output: X= 1 Y = 10 X= 2 Y = 9 X= 3 Y = 8 X= 4 Y = 7 X=5 Y = 6 X=6 Y = 5 X=7 Y = 4 X=8 Y =3 X=9 Y =2 X=10 Y =1

#include<stdio.h>

#include<conio.h>

void main(void)

{

int x, y;

clrscr();

for(x = 1, y= 10; x<= 10; ++x, --y)

printf(&quot;x = %d Y = %d &quot;,x,y);

getch(); ;

}

Output:

X= 1 Y = 10

X= 2 Y = 9

X= 3 Y = 8

X= 4 Y = 7

X=5 Y = 6

X=6 Y = 5

X=7 Y = 4

X=8 Y =3

X=9 Y =2

X=10 Y =1

The break statement break statement is used to take immediate exit from the for loop. When break statement is executed the control immediately be transferred to the first statement following the for loop. Ch – 8 For Loop By Abdul Ghaffar Khan main() { for( exp_l ; exp_2; exp_3) { statement1; statement2; if( condition) beak; statement3; statement4; } statement5; statement6; }

break statement is used to take immediate exit from the for loop. When break statement is executed the control immediately be transferred to the first statement following the for loop.

main()

{

for( exp_l ; exp_2; exp_3)

{

statement1;

statement2;

if( condition)

beak;

statement3;

statement4;

}

statement5;

statement6;

}

The continue statement Continue statement is used to skip the remaining statement in a loop this statement immediately transfer the control to the increment or decrement expression in the loop Ch – 8 For Loop By Abdul Ghaffar Khan main() { for( exp_l ; exp_2;exp_3) { statement 1; statement 2.; if( condition) continue; statement3; statement4; } statement5;. statement6; }

Continue statement is used to skip the remaining statement in a loop this statement immediately transfer the control to the increment or decrement expression in the loop

Nested for loop: When a for loop is completely embedded ( enclosed) within another for loop, the structure is called nested for loop, Ch – 8 For Loop By Abdul Ghaffar Khan for ( exp1 ; condition;exp2) { body-of-loop 1 for( exp3; condition2;exp4) { body-of-loop2 } }

When a for loop is completely embedded ( enclosed) within another for loop, the structure is called nested for loop,

Example 8.4 ( Factorials) void main(void) { int i,j; long fact; . clrscr(); for (i =1;i<=7;++i) { fact=l. for (j=i; j> I; --j) { fact *= j; } printf(&quot; %d! = %d &quot;,i,fact); } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output: 1! = I 2! =2 3! =6 4! =24 5! = 120 6! = 720 7! = 5040

void main(void)

{

int i,j; long fact; .

clrscr();

for (i =1;i<=7;++i)

{

fact=l.

for (j=i; j> I; --j)

{

fact *= j;

}

printf(&quot; %d! = %d &quot;,i,fact);

}

getch();

}

Output:

1! = I

2! =2

3! =6

4! =24

5! = 120

6! = 720

7! = 5040

Example 8.5 ( Stars) void main(void) { int I,j; clrscr(); for (i =1;i<=10;++i) { for 0= 1; j<=i ; ++j) { printf('*'); , } printf(&quot; &quot;); } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output: * ** *** **** ***** ****** ******* ******** ********* **********

void main(void)

{

int I,j;

clrscr();

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

{

for 0= 1; j<=i ; ++j)

{

printf('*'); ,

}

printf(&quot; &quot;);

}

getch();

}

Output:

*

**

***

****

*****

******

*******

********

*********

**********

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Home - Loop 5

Die Piraten sind los im LOOP5MC Family FundayGutscheinverkauf | Wichtige KundeninformationErleben Sie LIVE den Südhessischen Azubi Floristik Wettbewerb ...
Read more

Loop – Wikipedia

Loop (engl.: ‚Schleife‘ oder ‚Schlaufe‘) steht für: Loop (Musik), (zeitlich begrenztes) Klangelement, das durch technische Mittel wiederholt wird
Read more

dict.cc | loop | Wörterbuch Englisch-Deutsch

Übersetzung für loop im Englisch-Deutsch-Wörterbuch dict.cc.
Read more

Loop (Musik) – Wikipedia

Definitionen. Loop bezeichnet ein zeitlich begrenztes Klangereignis, das üblicherweise mit technischen Mitteln wiederholt wiedergegeben wird. Das ...
Read more

Loop-In

Willkommen in unserer Kite- und Windsurfschule Dierhagen auf der Halbinsel Fischland Darss Zingst.
Read more

Prepaid-Tarife bei o2 online

Noch mehr Freiheit beim Telefonieren, Surfen und Simsen mit der kostenlosen O 2 Loop Freikarte. Mit den Smart-Tarifoptionen kannst du deine Freikarte ...
Read more

Stores Archive - Loop 5

© Sonae Sierra 2016 Alle Rechte vorbehalten.
Read more

dict.cc | to be in the loop | Wörterbuch Englisch-Deutsch

Übersetzung für to be in the loop im Englisch-Deutsch-Wörterbuch dict.cc.
Read more

Polar Loop Aktivitätsarmband | Polar Deutschland

Für Fitness- und Crosstraining Polar Loop Aktivitätsarmband mit intelligenten Anleitungen. Bleib motiviert, indem du deine Aktivität rund um die Uhr ...
Read more