For Loop

3,370 views
3,266 views

Published on

Published in: Technology, Business
1 Comment
7 Likes
Statistics
Notes
No Downloads
Views
Total views
3,370
On SlideShare
0
From Embeds
0
Number of Embeds
280
Actions
Shares
0
Downloads
11
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

For Loop

  1. 1. The wizards of For Loop A Lecture By Abdul Ghaffar Khan
  2. 2. ‘ C’ and Iterative Logic <ul><li>To apply the Iterative logic we have three control structures in ‘C’ Language as listed below, </li></ul><ul><ul><ul><li>for Loop </li></ul></ul></ul><ul><ul><ul><li>while loop </li></ul></ul></ul><ul><ul><ul><li>do while loop </li></ul></ul></ul>Ch – 8 For Loop By Abdul Ghaffar Khan
  3. 3. The for Loop: <ul><li>for loop is used when a process is to repeat a fixed number of times. The structure of a for loop is as follows </li></ul><ul><ul><ul><li>for(expression_l; expression_2; expression_3) </li></ul></ul></ul><ul><ul><ul><li>any_instruction; </li></ul></ul></ul><ul><li>Or </li></ul><ul><ul><ul><li>for(expression_l; expression_2; expression_3) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>body-of-IOOP </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>Ch – 8 For Loop By Abdul Ghaffar Khan
  4. 4. Order of execution <ul><li>When the loop is started normally a control variable is initialized by executing expression_l. </li></ul><ul><li>expression_2 is tested if it is false then control is transferred outside the loop otherwise do next step 3 </li></ul><ul><li>Body of loop is executed. </li></ul><ul><li>Value of control variable is altered by expression_3 </li></ul><ul><li>Steps (2) to (4) are repeated until condition becomes false </li></ul>Ch – 8 For Loop By Abdul Ghaffar Khan <ul><li>for(exl; ex2; ex3) </li></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> body-of-IOOP </li></ul></ul><ul><ul><li>} </li></ul></ul>
  5. 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. 6. Example 8.2 ( print a table ) <ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int n, p, i= 0; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter a number whose table is to print &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;, &n); </li></ul><ul><li>printf(&quot;Table of %d &quot;, n); </li></ul><ul><li>for (i=1; i<=12; ++i) </li></ul><ul><li>{ </li></ul><ul><li>p = i* n; </li></ul><ul><li>printf(&quot;%d * %4 = %d &quot; n, i, p ); . </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>Ch – 8 For Loop By Abdul Ghaffar Khan <ul><li>Output </li></ul><ul><li>Enter a number whose table is to print 6 </li></ul><ul><ul><li>6 * 1 = 6 </li></ul></ul><ul><ul><li>6 * 2 = 12 </li></ul></ul><ul><ul><li>6 * 3 =18 </li></ul></ul><ul><ul><li>6 * 4 = 24 </li></ul></ul><ul><ul><li>6 * 5 = 30 </li></ul></ul><ul><ul><li>6 * 6 = 36 </li></ul></ul><ul><ul><li>6 * 7 = 42 </li></ul></ul><ul><ul><li>6 * 8 = 48 </li></ul></ul><ul><ul><li>6 * 9 = 54 </li></ul></ul><ul><ul><li>6 * 10 = 60 </li></ul></ul><ul><ul><li>6 * 11 = 66 </li></ul></ul><ul><ul><li>6 * 12 = 72 </li></ul></ul>
  7. 7. Example 8.3 <ul><li>Write down a program to generate the following output </li></ul>Ch – 8 For Loop By Abdul Ghaffar Khan C8 lower left corner BC lower right corner
  8. 8. Example 8.3 <ul><ul><li>void main(void) { </li></ul></ul><ul><ul><li>int x, y; </li></ul></ul><ul><ul><li>clrscr(); . </li></ul></ul><ul><ul><li>printf (&quot;xc9&quot;); </li></ul></ul><ul><ul><li>for (x=1; x<72 ; ++x) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>printf(&quot;xcd&quot;) ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>printf(&quot;xBB &quot;); </li></ul></ul><ul><ul><li>// drawing vertical lines </li></ul></ul><ul><ul><li>for (x=1;x<10;++x) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>if (x==5) </li></ul></ul><ul><ul><li>printf(&quot;xBA Welcome To Learning C xBA &quot;); </li></ul></ul><ul><ul><li>printf(&quot;xBA xBA &quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul>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(); }
  9. 9. Multiple assignments in the for loop <ul><li>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. </li></ul><ul><li>for( assignment I, asslgnment2; Condition; Increment1, intement2) . </li></ul><ul><li>{ </li></ul><ul><li>body of loop </li></ul><ul><li>} </li></ul>Ch – 8 For Loop By Abdul Ghaffar Khan
  10. 10. Example 8.3 <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>int x, y; </li></ul><ul><li>clrscr(); </li></ul><ul><li>for(x = 1, y= 10; x<= 10; ++x, --y) </li></ul><ul><li>printf(&quot;x = %d Y = %d &quot;,x,y); </li></ul><ul><li>getch(); ; </li></ul><ul><li>} </li></ul>Ch – 8 For Loop By Abdul Ghaffar Khan <ul><li>Output: </li></ul><ul><ul><li>X= 1 Y = 10 </li></ul></ul><ul><ul><li>X= 2 Y = 9 </li></ul></ul><ul><ul><li>X= 3 Y = 8 </li></ul></ul><ul><ul><li>X= 4 Y = 7 </li></ul></ul><ul><ul><li>X=5 Y = 6 </li></ul></ul><ul><ul><li>X=6 Y = 5 </li></ul></ul><ul><ul><li>X=7 Y = 4 </li></ul></ul><ul><ul><li>X=8 Y =3 </li></ul></ul><ul><ul><li>X=9 Y =2 </li></ul></ul><ul><ul><li>X=10 Y =1 </li></ul></ul>
  11. 11. The break statement <ul><ul><li>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. </li></ul></ul>Ch – 8 For Loop By Abdul Ghaffar Khan <ul><ul><li>main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>for( exp_l ; exp_2; exp_3) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>statement1; </li></ul></ul><ul><ul><li>statement2; </li></ul></ul><ul><ul><li>if( condition) </li></ul></ul><ul><ul><li>beak; </li></ul></ul><ul><ul><li>statement3; </li></ul></ul><ul><ul><li>statement4; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>statement5; </li></ul></ul><ul><ul><li>statement6; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  12. 12. The continue statement <ul><li>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 </li></ul>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. 13. Nested for loop: <ul><li>When a for loop is completely embedded ( enclosed) within another for loop, the structure is called nested for loop, </li></ul>Ch – 8 For Loop By Abdul Ghaffar Khan for ( exp1 ; condition;exp2) { body-of-loop 1 for( exp3; condition2;exp4) { body-of-loop2 } }
  14. 14. Example 8.4 ( Factorials) <ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>int i,j; long fact; . </li></ul><ul><li>clrscr(); </li></ul><ul><li>for (i =1;i<=7;++i) </li></ul><ul><li>{ </li></ul><ul><li> fact=l. </li></ul><ul><li> for (j=i; j> I; --j) </li></ul><ul><li> { </li></ul><ul><li> fact *= j; </li></ul><ul><li> } </li></ul><ul><li>printf(&quot; %d! = %d &quot;,i,fact); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>Ch – 8 For Loop By Abdul Ghaffar Khan <ul><li>Output: </li></ul><ul><ul><li>1! = I </li></ul></ul><ul><ul><li>2! =2 </li></ul></ul><ul><ul><li>3! =6 </li></ul></ul><ul><ul><li>4! =24 </li></ul></ul><ul><ul><li>5! = 120 </li></ul></ul><ul><ul><li>6! = 720 </li></ul></ul><ul><ul><li>7! = 5040 </li></ul></ul>
  15. 15. Example 8.5 ( Stars) <ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>int I,j; </li></ul><ul><li>clrscr(); </li></ul><ul><li>for (i =1;i<=10;++i) </li></ul><ul><li>{ </li></ul><ul><li>for 0= 1; j<=i ; ++j) </li></ul><ul><li>{ </li></ul><ul><li>printf('*'); , </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; &quot;); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>Ch – 8 For Loop By Abdul Ghaffar Khan <ul><li>Output: </li></ul><ul><ul><li>* </li></ul></ul><ul><ul><li>** </li></ul></ul><ul><ul><li>*** </li></ul></ul><ul><ul><li>**** </li></ul></ul><ul><ul><li>***** </li></ul></ul><ul><ul><li>****** </li></ul></ul><ul><ul><li>******* </li></ul></ul><ul><ul><li>******** </li></ul></ul><ul><ul><li>********* </li></ul></ul><ul><ul><li>********** </li></ul></ul>

×