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
CHAPTER 4 Selection RECAP
IF, ELSE
if(Control Statement){Selection operation}
Else{Selection operation}
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);}
IF, ELSE IF, ELSE
If (Control Statement){Selection operation/action}
else if (Control Statement){Selection operation/action}
else{Selection operation/action}
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; }
CHAP 4SWITCH, WHILE LOOP, DO WHILE LOOP,
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
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”);
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;}
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;
}
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
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)
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.
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
Determine the output of the following while loop statement :
a = 1;while ( a <= 5){ printf(“Number %d”,a);
a++;}
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
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
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
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
int bil, nilai = 3;
for (bil = 5; bil >0; bil--){
if( bil % 2 == 1)
printf(“\n %d”, bil + nilai); }
FOR LOOP SAMPLE
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
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