Computer programming lab_2

503 views
412 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
503
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Computer programming lab_2

  1. 1. Computer Programming  Lab-2 Loops
  2. 2.   <ul><ul><li>The for Loop </li></ul></ul><ul><ul><li>The while Loop </li></ul></ul><ul><ul><li>The do while Loop </li></ul></ul><ul><ul><li>Exercise </li></ul></ul>
  3. 3. for Loop      <ul><li>// forloop </li></ul><ul><li>/* </li></ul><ul><li>prints numbers  </li></ul><ul><li>from 0 to 9 </li></ul><ul><li>*/ </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>    int int_count = 0; </li></ul><ul><li>     </li></ul><ul><li>    for (int_count=0; int_count<10; int_count++) </li></ul><ul><li>     { </li></ul><ul><li>        printf(&quot;count = %dn&quot;, int_count); </li></ul><ul><li>    } </li></ul><ul><li>} </li></ul>
  4. 4.   <ul><li>  </li></ul>Expression Name Purpose count=0 Initialize expression initialize loop variables count<0 Test expression Tests loop variables count++ Increment expression Increment loop variables
  5. 5. An ASCII <ul><li>  </li></ul><ul><li>char ch; </li></ul><ul><li>ch = getch(); </li></ul><ul><li>printf(&quot;character = %c and ASCII of character = %d&quot;ch,ch); </li></ul><ul><li>0 to 255 characters </li></ul><ul><li>  every character of keyboard is of 8 bit </li></ul>
  6. 6. Drawing a Line <ul><li>printf(&quot;%c&quot;, 'xDB'); </li></ul>
  7. 7. while Loop <ul><li>// while loop </li></ul><ul><li>/* </li></ul><ul><li>    prints number from 0 to 9 </li></ul><ul><li>*/ </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>    int int_count = 0; </li></ul><ul><li>    int int_total = 0; </li></ul><ul><li>    while(count < 10) </li></ul><ul><li>    { </li></ul><ul><li>        total = total + count; </li></ul><ul><li>        printf(&quot;count=%d, total=%d&quot;,count++, total); </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
  8. 8.   <ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>    int int_count = -1; </li></ul><ul><li>    char char_ch = 'a'; </li></ul><ul><li>    while(ch != 'r' ) </li></ul><ul><li>     { </li></ul><ul><li>        ch = getch(); </li></ul><ul><li>         int_count++; </li></ul><ul><li>     }  </li></ul><ul><li>    printf(&quot;character count is %d&quot;, int_count); </li></ul><ul><li>} </li></ul>
  9. 9. do while Loop <ul><li>// doloop </li></ul><ul><li>/* </li></ul><ul><li>    prints numbers from 0 to 9 </li></ul><ul><li>*/ </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>    int int_count = 0; </li></ul><ul><li>    int int_total = 0; </li></ul><ul><li>    do </li></ul><ul><li>    { </li></ul><ul><li>         int_total = int_total + 1; </li></ul><ul><li>        printf(&quot;count=%d, total=%dn&quot;,int_count++, int_total)  </li></ul><ul><li>    } </li></ul><ul><li>    while(int_count < 10) </li></ul><ul><li>} </li></ul>
  10. 10. Exercise <ul><ul><li>write a program that prints the squares of all the numbers from 1 to 20; </li></ul></ul><ul><ul><li>write a program that count characters until a period (.) is typed. </li></ul></ul><ul><ul><li>write a program create a factorial of given number </li></ul></ul><ul><ul><li>write a program which count word in a sentence </li></ul></ul><ul><ul><li>write a program which count any given character from given string </li></ul></ul><ul><ul><li>create a game guess letter </li></ul></ul><ul><ul><ul><li>lets you guess a letter and uses do loop to ask if new game wanted </li></ul></ul></ul><ul><ul><li>create a graphics box of 10*10(10 rows and 10 column) </li></ul></ul>

×