Dti2143 chap 4 control statement part 2
-
Upload
alish-al-shahab -
Category
Documents
-
view
851 -
download
6
Transcript of Dti2143 chap 4 control statement part 2
![Page 1: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/1.jpg)
CHAPTER 4 Selection RECAP
![Page 2: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/2.jpg)
IF, ELSE
if(Control Statement){Selection operation}
Else{Selection operation}
![Page 3: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/3.jpg)
IF ELSE SAMPLEint main()
{int age;printf("please enter payment amount ");scanf("%d",&payment);
if(payment>50){payment=payment-10;}
else{payment=payment;}
printf("please pay %d",payment);}
![Page 4: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/4.jpg)
IF, ELSE IF, ELSE
If (Control Statement){Selection operation/action}
else if (Control Statement){Selection operation/action}
else{Selection operation/action}
![Page 5: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/5.jpg)
IF, ELSE IF, ELSE SAMPLEint main(){ int marks; printf("\nEnter Marks :"); scanf("%d",&marks); if ( marks<=100 && marks>=80){printf("\nYour
grade is A");} else if ( marks<=79 && marks>=60){printf("\nYour
grade is B");} else if ( marks<=59 && marks>=40){printf("\nYour
grade is C");} else if ( marks<=39 && marks>=20){printf("\nYour
grade is D");} else if ( marks<=19 && marks>=0){printf("\nYour
grade is E");} else{printf("\nThe exam mark must be between 0
and 100 inclusive.");} return 0; }
![Page 6: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/6.jpg)
CHAP 4SWITCH, WHILE LOOP, DO WHILE LOOP,
![Page 7: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/7.jpg)
Format:
switch( conditional expression/variable){ case label1: C statement; break; case label2: C statement;
break; … default: pernyataanC;}
Only Integer/ character
Functioning like else
value Exit from option
SWITCH CASE
![Page 8: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/8.jpg)
How To Convert this If Else Snippet to Switch?
if ( input == 1) printf(“\nOne”);
else if (input == 2) printf(“\nTwo”);
else if (input == 3) printf(“\nThree”);
else if (input == 4) printf(“\nFour”);
else printf(“\nFive”);
![Page 9: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/9.jpg)
Answer:
switch(input) {case 1:
printf("\nOne"); break;case 2:
printf("\nTwo");break;case 3:
printf("\nThree");break;case 4:
printf("\nFour");break;default:
printf("\nFive");break;}
![Page 10: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/10.jpg)
IF-ELSE / SWITCH CASE Side by Side
if ( input == 1) printf(“\nOne”);else if (input == 2) printf(“\nTwo”);else if (input == 3) printf(“\nThree”);else if (input == 4) printf(“\nFour”);else printf(“\nFive”);
switch(input)
{
case 1:
printf("\nOne"); break;
case 2:
printf("\nTwo");break;
case 3:
printf("\nThree");break;
case 4:
printf("\nFour");break;
default:
printf("\nFive");break;
}
![Page 11: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/11.jpg)
Statement will be repeated if the condition is TRUE.Format :
counter = 0; while (conditional expression) { C statement; counter++; }
IMPORTANT!1. Counter2. Counter initialization 3. Conditional expression testing4. Increment/decrement of counter
WHILE LOOP
![Page 12: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/12.jpg)
WHILE LOOP IMPORTANT ASPECTS1. Counter Initialization
• 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’;
2. Conditional Expression
• Logical (>,<,==,!=,<=,>=) symbol is used• Used to ensure the loop/repetition is occurred.• Example:
while ( I < 4)
![Page 13: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/13.jpg)
WHILE LOOP IMPORTANT ASPECTS
3. Increment/decrement value of counter• Used to increase/ decrease the value of counter
for the statements to repeat.• If not, infinite loop will occured• Example:
I ++;++a;y--;
:: increment is used if the initial value of counter is less while decrement is used if the initial value of
counter is high.
![Page 14: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/14.jpg)
void main(){ int nom1,nom2,nom3,nom4,nom5;
printf(“\nEnter one number :”); scanf(“%d”,&nom1); printf(“\ nEnter one number :”); scanf(“%d”,&nom2); printf(“\ nEnter one number :”); scanf(“%d”,&nom3); printf(“\ nEnter one number :”); scanf(“%d”,&nom1); printf(“\ nEnter one number :”); scanf(“%d”,&nom5);}
void main(){ int nom; int i; i =0; while(i <5) { printf(“\ nEnter one number :”); scanf(“%d”,&nom); i++; }}
counter
condition
increment
Example 1:
WHILE LOOP CONVERT
![Page 15: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/15.jpg)
Determine the output of the following while loop statement :
a = 1;while ( a <= 5){ printf(“Number %d”,a);
a++;}
![Page 16: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/16.jpg)
Use do..while statementRepetition will occurred if condition is TRUESimilar to while, must include 3 important thingsFormat :
counter = 0; do { C statement; counter++; } while (x < 5); Repeated statements
DO-WHILE LOOP
![Page 17: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/17.jpg)
DO-WHILE/WHILE DIFFERENCES
While loop do..while loop
Loop is tested before repetition can be done while ( a < 4) { ……..
}
Loop is tested after repetition is done do {
} while ( a<4);
Possibility of repeated loop is 0 Possibility of repeated loop is 1
![Page 18: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/18.jpg)
Example : Converting the following do..while loop to while loop
i = 2;j = 1;do{ i++; printf(“ i x j = %d”, i * j);} while (i < 8);
i= 2;j = 1;while (i < 8){ i++; printf(“ i x j = %d”, i * j);}
do..while while
DO-WHILE to WHILE LOOP CONVERT
![Page 19: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/19.jpg)
Use for loopRepeat statement until condition is FALSESimilar to while loop, must includes 3 important thingsFormat :
for (initial_value; conditional expression ;increment) { C statement; }
Repeatedstatements
FOR LOOP
![Page 20: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/20.jpg)
int bil, nilai = 3;
for (bil = 5; bil >0; bil--){
if( bil % 2 == 1)
printf(“\n %d”, bil + nilai); }
FOR LOOP SAMPLE
![Page 21: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/21.jpg)
while.. do..while for
I=0;while (I < 5){ I++;}
I=0;do{I++;}while(I <5)
for (I=0;I < 5; I++){
}
WHILE,DO-WHILE and FOR LOOP side by side
![Page 22: Dti2143 chap 4 control statement part 2](https://reader035.fdocuments.us/reader035/viewer/2022062704/555868bad8b42aaa7e8b49e5/html5/thumbnails/22.jpg)
Selecting between while, do while and for loops
A ‘for’ loop is used when the number of times the loop is executed is well known
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
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