KEMBAR78
For Loop | PPT
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
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
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 }
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 \n&quot;, n); for (i=1; i<=12; ++i) { p = i* n; printf(&quot;%d * %4 = %d\n&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
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
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\n&quot;);  // drawing vertical lines  for (x=1;x<10;++x) { if (x==5) printf(&quot;\xBA\t\t\tWelcome To Learning C\t\t\t\t\xBA\n&quot;); printf(&quot;\xBA\t\t\t\t\t\t\t\t\t\xBA\n&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\n&quot;) ; getch(); }
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
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\tY = %d\n&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
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; }
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; }
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 } }
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;\n%d! = %d\n&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
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;\n&quot;);  }  getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output: *  ** *** **** ***** ****** ******* ******** ********* **********

For Loop

  • 1.
    The wizards ofFor Loop A Lecture By Abdul Ghaffar Khan
  • 2.
    ‘ C’ andIterative 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
  • 3.
    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
  • 4.
    Order of executionWhen 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 }
  • 5.
    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
  • 6.
    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 \n&quot;, n); for (i=1; i<=12; ++i) { p = i* n; printf(&quot;%d * %4 = %d\n&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
  • 7.
    Example 8.3 Writedown a program to generate the following output Ch – 8 For Loop By Abdul Ghaffar Khan C8 lower left corner BC lower right corner
  • 8.
    Example 8.3 voidmain(void) { int x, y; clrscr(); . printf (&quot;\xc9&quot;); for (x=1; x<72 ; ++x) { printf(&quot;\xcd&quot;) ; } printf(&quot;\xBB\n&quot;); // drawing vertical lines for (x=1;x<10;++x) { if (x==5) printf(&quot;\xBA\t\t\tWelcome To Learning C\t\t\t\t\xBA\n&quot;); printf(&quot;\xBA\t\t\t\t\t\t\t\t\t\xBA\n&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\n&quot;) ; getch(); }
  • 9.
    Multiple assignments inthe 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
  • 10.
    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\tY = %d\n&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
  • 11.
    The break statementbreak 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; }
  • 12.
    The continue statementContinue 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; }
  • 13.
    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 } }
  • 14.
    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;\n%d! = %d\n&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
  • 15.
    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;\n&quot;); } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output: * ** *** **** ***** ****** ******* ******** ********* **********