Chapter 4 <br />Selection RECAP<br />
IF, ELSE<br />if(Control Statement)<br />{<br />Selection operation<br />}<br />Else<br />{<br />Selection operation<br />...
IF ELSE SAMPLE<br />int main()<br />{<br />int age;<br />printf("please enter payment amount ");<br />scanf("%d",&payment)...
IF, ELSE IF, ELSE<br />If (Control Statement)<br />{<br />Selection operation/action<br />}<br />else if (Control Statemen...
IF, ELSE IF, ELSE SAMPLE<br />int main(){<br />int marks;        <br />printf("nEnter Marks :");<br />scanf("%d",&marks);<...
CHAP 4<br />SWITCH, WHILE LOOP, DO WHILE LOOP, <br />
SWITCH CASE<br />Format:<br />Only Integer/ <br />character<br />switch( conditional expression/variable)<br />{<br />    ...
How To Convert this If Else Snippet to Switch?<br /> 	if ( input == 1)<br />printf(“nOne”);<br />	else if (input == 2)<br ...
Answer:<br />switch(input)<br /> {<br />case 1:<br />printf("nOne"); break;<br />case 2:<br />printf("nTwo");break;<br />c...
IF-ELSE / SWITCH CASE Side by Side<br />switch(input)<br /> {<br />case 1:<br />printf("nOne"); break;<br />case 2:<br />p...
WHILE LOOP<br /><ul><li>Statement will be repeated if the condition is TRUE.
Format :</li></ul>IMPORTANT!<br />1. Counter<br />2. Counter initialization  <br />3. Conditional expression testing<br />...
WHILE LOOP IMPORTANT ASPECTS<br />1. Counter Initialization<br /><ul><li>Counter is a variable
Must be declared as an integer
Used to count amount of loops
Can be initialized starting from any value.
Example : x = 0; a= 1; I = 3;proceed=‘y’;lagi!=‘n’;</li></ul>2. Conditional Expression<br /><ul><li>Logical (>,<,==,!=,<=,...
Used to ensure the loop/repetition is occurred.
Example:                  </li></ul>		while ( I < 4)<br />
WHILE LOOP IMPORTANT ASPECTS<br />3.Increment/decrement value of counter<br /><ul><li>Used to increase/ decrease the value...
Example:</li></ul>		I ++;<br />		++a;<br />		y--;<br />:: increment is used if the initial  value of counter is<br /> less...
Example 1:<br />void main()<br />{   int nom1,nom2,nom3,nom4,nom5;<br />printf(“nEnter one number :”);<br />scanf(“%d”,&no...
Upcoming SlideShare
Loading in …5
×

Dti2143 chap 4 control statement part 2

986 views

Published on

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

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

No notes for slide

Dti2143 chap 4 control statement part 2

  1. 1. Chapter 4 <br />Selection RECAP<br />
  2. 2. IF, ELSE<br />if(Control Statement)<br />{<br />Selection operation<br />}<br />Else<br />{<br />Selection operation<br />}<br />
  3. 3. IF ELSE SAMPLE<br />int main()<br />{<br />int age;<br />printf("please enter payment amount ");<br />scanf("%d",&payment);<br />if(payment>50)<br />{<br />payment=payment-10;<br />}<br />else<br />{<br />payment=payment;<br />}<br />printf("please pay %d",payment);<br />}<br />
  4. 4. IF, ELSE IF, ELSE<br />If (Control Statement)<br />{<br />Selection operation/action<br />}<br />else if (Control Statement)<br />{<br />Selection operation/action<br />}<br />else<br />{<br />Selection operation/action<br />}<br />
  5. 5. IF, ELSE IF, ELSE SAMPLE<br />int main(){<br />int marks; <br />printf("nEnter Marks :");<br />scanf("%d",&marks);<br /> if ( marks<=100 && marks>=80){printf("nYour grade is A");} <br /> else if ( marks<=79 && marks>=60){printf("nYour grade is B");} <br /> else if ( marks<=59 && marks>=40){printf("nYour grade is C");}<br /> else if ( marks<=39 && marks>=20){printf("nYour grade is D");}<br /> else if ( marks<=19 && marks>=0){printf("nYour grade is E");}<br /> else{printf("nThe exam mark must be between 0 and 100 inclusive.");}<br /> return 0;<br /> }<br />
  6. 6. CHAP 4<br />SWITCH, WHILE LOOP, DO WHILE LOOP, <br />
  7. 7. SWITCH CASE<br />Format:<br />Only Integer/ <br />character<br />switch( conditional expression/variable)<br />{<br /> case label1: C statement;<br /> break;<br /> case label2: C statement;<br /> break;<br /> …<br /> default: pernyataanC;<br />}<br />value<br />Exit from option<br />Functioning like else<br />
  8. 8. How To Convert this If Else Snippet to Switch?<br /> if ( input == 1)<br />printf(“nOne”);<br /> else if (input == 2)<br />printf(“nTwo”);<br /> else if (input == 3)<br />printf(“nThree”);<br /> else if (input == 4)<br />printf(“nFour”);<br /> else<br />printf(“nFive”);<br />
  9. 9. Answer:<br />switch(input)<br /> {<br />case 1:<br />printf("nOne"); break;<br />case 2:<br />printf("nTwo");break;<br />case 3:<br />printf("nThree");break;<br />case 4:<br />printf("nFour");break;<br />default: <br />printf("nFive");break;<br />}<br />
  10. 10. IF-ELSE / SWITCH CASE Side by Side<br />switch(input)<br /> {<br />case 1:<br />printf("nOne"); break;<br />case 2:<br />printf("nTwo");break;<br />case 3:<br />printf("nThree");break;<br />case 4:<br />printf("nFour");break;<br />default: <br />printf("nFive");break;<br />}<br />if ( input == 1)<br />printf(“nOne”);<br />else if (input == 2)<br />printf(“nTwo”);<br />else if (input == 3)<br />printf(“nThree”);<br />else if (input == 4)<br />printf(“nFour”);<br />else<br />printf(“nFive”);<br />
  11. 11. WHILE LOOP<br /><ul><li>Statement will be repeated if the condition is TRUE.
  12. 12. Format :</li></ul>IMPORTANT!<br />1. Counter<br />2. Counter initialization <br />3. Conditional expression testing<br />4. Increment/decrement of counter<br />counter = 0;<br /> while (conditional expression)<br /> {<br /> C statement;<br />counter++;<br /> }<br />
  13. 13. WHILE LOOP IMPORTANT ASPECTS<br />1. Counter Initialization<br /><ul><li>Counter is a variable
  14. 14. Must be declared as an integer
  15. 15. Used to count amount of loops
  16. 16. Can be initialized starting from any value.
  17. 17. Example : x = 0; a= 1; I = 3;proceed=‘y’;lagi!=‘n’;</li></ul>2. Conditional Expression<br /><ul><li>Logical (>,<,==,!=,<=,>=) symbol is used
  18. 18. Used to ensure the loop/repetition is occurred.
  19. 19. Example: </li></ul> while ( I < 4)<br />
  20. 20. WHILE LOOP IMPORTANT ASPECTS<br />3.Increment/decrement value of counter<br /><ul><li>Used to increase/ decrease the value of counter</li></ul> for the statements to repeat.<br /><ul><li>If not, infinite loop will occured
  21. 21. Example:</li></ul> I ++;<br /> ++a;<br /> y--;<br />:: increment is used if the initial value of counter is<br /> less while decrement is used if the initial value of counter <br /> is high.<br />
  22. 22. Example 1:<br />void main()<br />{ int nom1,nom2,nom3,nom4,nom5;<br />printf(“nEnter one number :”);<br />scanf(“%d”,&nom1);<br />printf(“ nEnter one number:”);<br />scanf(“%d”,&nom2);<br />printf(“ nEnter one number:”);<br />scanf(“%d”,&nom3);<br />printf(“ nEnter one number:”);<br />scanf(“%d”,&nom1);<br />printf(“ nEnter one number:”);<br />scanf(“%d”,&nom5);<br />}<br />void main()<br />{ int nom;<br />inti;<br />i =0;<br /> while(i <5)<br /> {<br />printf(“ nEnter one number:”);<br />scanf(“%d”,&nom);<br />i++;<br /> }<br />}<br />counter<br />condition<br />increment<br />WHILE LOOP CONVERT <br />
  23. 23. Determine the output of the following whileloop statement :<br />a = 1;<br /> while ( a <= 5)<br /> { printf(“Number %d”,a);<br /> a++;<br /> }<br />
  24. 24. Use do..while statement<br />Repetition will occurred if condition is TRUE<br />Similar to while, must include 3 important things<br />Format :<br />counter = 0;<br />do<br /> { C statement;<br /> counter++;<br /> } while (x < 5);<br />Repeated statements<br />DO-WHILE LOOP <br />
  25. 25. DO-WHILE/WHILE DIFFERENCES <br />
  26. 26. Example : Converting the following do..while loop to while loop<br />i = 2;<br />j = 1;<br />do<br />{ i++;<br />printf(“ i x j = %d”, i * j);<br />} while (i < 8);<br />i= 2;<br />j = 1;<br />while (i < 8)<br />{ i++;<br />printf(“ i x j = %d”, i * j);<br />}<br />while<br />do..while<br />DO-WHILE to WHILE LOOP CONVERT <br />
  27. 27. FOR LOOP<br /><ul><li>Use for loop
  28. 28. Repeat statement until condition is FALSE
  29. 29. Similar to while loop, must includes 3 important things
  30. 30. Format :</li></ul>for (initial_value; conditional expression ;increment)<br /> { <br /> C statement;<br /> }<br />Repeated<br />statements<br />
  31. 31. FOR LOOP SAMPLE<br />intbil, nilai = 3;<br /> for (bil = 5; bil >0; bil--)<br /> { <br /> if( bil % 2 == 1)<br />printf(“n %d”, bil + nilai); <br /> }<br />
  32. 32. WHILE,DO-WHILE and FOR LOOP side by side<br />
  33. 33. Selecting between while, do while and for loops<br /><ul><li>A ‘for’ loop is used when the number of times the loop is executed is well known
  34. 34. A ‘while’ loop is used when the number of times the loop gets executed is not known and the loop should not be executed when the condition is initially false
  35. 35. A ‘do while’ loop is used when the number of times the loop gets executed is not known and the loop should be executed even when the condition is initially false</li>

×