Your SlideShare is downloading. ×
0
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Decision structures in_c lab3
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Decision structures in_c lab3

684

Published on

Published in: Education, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
684
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  1. Decision Structures in C Lab # 3
  2. The if Statement <ul><li>#include <stdio.h> #include <conio.h> void main(void) {     char ch;     ch = getch();     if(ch == 'y')     {         printf(&quot;You type c&quot;);     } } </li></ul>
  3. Multiple Statement with if <ul><li>if(condition) </li></ul><ul><li>    statement 1; </li></ul><ul><li>if(condition) </li></ul><ul><li>{ </li></ul><ul><li>    statement 1; </li></ul><ul><li>    statement 1; </li></ul><ul><li>} </li></ul>
  4. Nested if Statement <ul><li>if(condition) </li></ul><ul><li>    { </li></ul><ul><li>        if(condition) </li></ul><ul><li>         { </li></ul><ul><li>  </li></ul><ul><li>        } </li></ul><ul><li>     } </li></ul>
  5. The if-else Statement <ul><li>if(condition) </li></ul><ul><li>{ </li></ul><ul><li>       statement ;      </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>        statement ; </li></ul><ul><li>} </li></ul>
  6. Nested if-else statement <ul><li>if(condition) </li></ul><ul><li>{ </li></ul><ul><li>    if(condition) </li></ul><ul><li>     { </li></ul><ul><li>  </li></ul><ul><li>    } </li></ul><ul><li>    else </li></ul><ul><li>     { </li></ul><ul><li>  </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>    if(condition) </li></ul><ul><li>    { </li></ul><ul><li>  </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
  7. which if Gets the else <ul><li>if(condition) </li></ul><ul><li>    if(condition) </li></ul><ul><li>        printf(&quot;i am in if&quot;); </li></ul><ul><li>else </li></ul><ul><li>    printf(&quot;now in else&quot;) </li></ul>
  8. Logical Operators <ul><li>||        logical OR </li></ul><ul><li>&&     logical AND </li></ul><ul><li>!         logical NOT </li></ul>
  9.   <ul><li>if(condition || condition) </li></ul><ul><li>    if(x==1 || x==2) </li></ul><ul><li>if(condition && condition) </li></ul><ul><li>    if(x==1 && x==2)  </li></ul><ul><li>  </li></ul><ul><li>if(!condition) </li></ul><ul><li>    if(!(x<2)) </li></ul><ul><li>  </li></ul><ul><li>int flag = 0 </li></ul><ul><li>if(flag) false </li></ul><ul><li>if(!flag) true </li></ul>
  10. The break Statement <ul><li>while(condition) </li></ul><ul><li>{ </li></ul><ul><li>    if(condition) </li></ul><ul><li>     { </li></ul><ul><li>        break; </li></ul><ul><li>    } </li></ul><ul><li>} </li></ul>
  11. The switch statement <ul><li>  </li></ul><ul><li>char op; </li></ul><ul><li>switch(op) </li></ul><ul><li>    { </li></ul><ul><li>        case '+': </li></ul><ul><li>            printf(&quot;%c&quot;,ch); </li></ul><ul><li>            break; </li></ul><ul><li>     case '-': </li></ul><ul><li>            printf(&quot;%c&quot;,ch); </li></ul><ul><li>            break; </li></ul><ul><li>     default: </li></ul><ul><li>            printf(&quot;unknown&quot;); </li></ul><ul><li>     } </li></ul>
  12. Conditional Operator <ul><li>max = (num1 > num2) ? num1 : num2; </li></ul>
  13. Exercise <ul><ul><li>count characters and words in a phrase typed in. </li></ul></ul><ul><ul><li>draw a checkerboard on the screen </li></ul></ul><ul><ul><li>four function calculator (add, sub, mul and div) </li></ul></ul>
  14.   <ul><li>#include <stdio.h> #include <conio.h> void main(void) {     int charcnt = 0;     int wordcnt = 0;     char ch;     printf(&quot;Type in a phrase:n&quot;);     while( (ch = getch()) != 'r')     {         charcnt++;         if(ch == ' ')         {             wordcnt++;         }     }     printf(&quot;n character count is %d&quot;, charcnt);     printf(&quot;n Word count is %d&quot;, wordcnt+1); } </li></ul>
  15.   <ul><li>  </li></ul>

×