Dti2143 chap 4 control statement part 2

22
CHAPTER 4 Selection RECAP

Transcript of Dti2143 chap 4 control statement part 2

Page 1: Dti2143 chap 4 control statement part 2

CHAPTER 4 Selection RECAP

Page 2: Dti2143 chap 4 control statement part 2

IF, ELSE

if(Control Statement){Selection operation}

Else{Selection operation}

Page 3: Dti2143 chap 4 control statement part 2

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

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

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

CHAP 4SWITCH, WHILE LOOP, DO WHILE LOOP,

Page 7: Dti2143 chap 4 control statement part 2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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