SlideShare a Scribd company logo
1 of 22
Chapter 4  Selection RECAP
IF, ELSE if(Control Statement) { Selection operation } Else { Selection operation }
IF ELSE SAMPLE int 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 SAMPLE int main(){ int marks;         printf("Enter Marks :"); scanf("%d",&marks);        if ( marks<=100 && marks>=80){printf("Your grade is A");}         else if ( marks<=79 && marks>=60){printf("Your grade is B");}            else if ( marks<=59 && marks>=40){printf("Your grade is C");}        else if ( marks<=39 && marks>=20){printf("Your grade is D");}        else if ( marks<=19 && marks>=0){printf("Your grade is E");}        else{printf("The exam mark must be between 0 and 100 inclusive.");}       return 0;        }
CHAP 4 SWITCH, WHILE LOOP, DO WHILE LOOP,
SWITCH CASE Format: Only Integer/  character switch( conditional expression/variable) {       case label1: C statement;                         break;       case label2: C statement; 		 break;       …       default:  pernyataanC; } value Exit from option Functioning like else
How To Convert this If Else Snippet to Switch?  	if ( input == 1) printf(“One”); 	else if (input == 2) printf(“Two”); 	else if (input == 3) printf(“Three”); 	else if (input == 4) printf(“Four”); 	else printf(“Five”);
Answer: switch(input)  { case 1: printf("One"); break; case 2: printf("Two");break; case 3: printf("Three");break; case 4: printf("Four");break; default:  printf("Five");break; }
IF-ELSE / SWITCH CASE Side by Side switch(input)  { case 1: printf("One"); break; case 2: printf("Two");break; case 3: printf("Three");break; case 4: printf("Four");break; default:  printf("Five");break; } if ( input == 1) printf(“One”); else if (input == 2) printf(“Two”); else if (input == 3) printf(“Three”); else if (input == 4) printf(“Four”); else printf(“Five”);
WHILE LOOP ,[object Object]
Format :IMPORTANT! 1. Counter 2. Counter initialization   3. Conditional expression testing 4. Increment/decrement of counter counter = 0;    while (conditional expression)    {       C  statement; counter++;     }
WHILE LOOP IMPORTANT ASPECTS 1. Counter Initialization ,[object Object]
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 ,[object Object]
Used to ensure the loop/repetition is occurred.
Example:                  		while ( I < 4)
WHILE LOOP IMPORTANT ASPECTS 3.Increment/decrement value of counter ,[object Object],	for the statements to repeat. ,[object Object]
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.
Example 1: void main() {   int nom1,nom2,nom3,nom4,nom5; printf(“Enter 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; inti; i =0;     while(i <5)     { printf(“nEnter one number:”); scanf(“%d”,&nom); i++;      } } counter condition increment WHILE LOOP CONVERT

More Related Content

What's hot

operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c languageshhanks
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in cvampugani
 
control stucture in c language
control stucture in c language control stucture in c language
control stucture in c language abdulahi45
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Conditional statements
Conditional statementsConditional statements
Conditional statementsNabishaAK
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)Way2itech
 

What's hot (19)

operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c language
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in c
 
7 decision-control
7 decision-control7 decision-control
7 decision-control
 
control stucture in c language
control stucture in c language control stucture in c language
control stucture in c language
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
3 flow
3 flow3 flow
3 flow
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Branching in C
Branching in CBranching in C
Branching in C
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)
 

Viewers also liked

Developing The Mongolian Wolf Spirit In Football
Developing The Mongolian Wolf Spirit In FootballDeveloping The Mongolian Wolf Spirit In Football
Developing The Mongolian Wolf Spirit In FootballMichele Ferraris
 
K2 atomic structure
K2 atomic structureK2 atomic structure
K2 atomic structureRathiga Sri
 
The Krass Family 2010
The Krass Family 2010The Krass Family 2010
The Krass Family 2010Mike Krass
 
Tarot: Card Interpretation
Tarot: Card InterpretationTarot: Card Interpretation
Tarot: Card InterpretationJohnna Russell
 
Businesssystempresentation101409 100107132037 Phpapp01
Businesssystempresentation101409 100107132037 Phpapp01Businesssystempresentation101409 100107132037 Phpapp01
Businesssystempresentation101409 100107132037 Phpapp01Rodan + Fields Dermatologists
 
Necesidades de comunicación e información en el paciente refworks uniform
Necesidades de comunicación e información en el paciente refworks uniformNecesidades de comunicación e información en el paciente refworks uniform
Necesidades de comunicación e información en el paciente refworks uniformMercedes Bueno
 
Ways of Looking 1
Ways of Looking 1Ways of Looking 1
Ways of Looking 1amandakane1
 
Wb2000 panorama action smash
Wb2000 panorama action smashWb2000 panorama action smash
Wb2000 panorama action smashchristianprey7
 
โครงงานครั้งที่........
โครงงานครั้งที่........โครงงานครั้งที่........
โครงงานครั้งที่........NattAA
 
Business Development Strategies for Lawyers
Business Development Strategies for LawyersBusiness Development Strategies for Lawyers
Business Development Strategies for LawyersEllenatLegalWritingPro
 
Eagle crest gruppo 5. ordinate
Eagle crest gruppo 5. ordinateEagle crest gruppo 5. ordinate
Eagle crest gruppo 5. ordinateFrancesca Capraro
 
Advert planning
Advert planningAdvert planning
Advert planningReganROFL
 
AIS - Workstations
AIS - WorkstationsAIS - Workstations
AIS - Workstationssincorvaia
 
Ud 0 final juani
Ud 0 final juaniUd 0 final juani
Ud 0 final juanijuanapardo
 
Ordenagailuaren zatiak
Ordenagailuaren zatiakOrdenagailuaren zatiak
Ordenagailuaren zatiakLorenna Suarez
 
Podcast tutorial
Podcast tutorialPodcast tutorial
Podcast tutorialMayci Neal
 
Ways of looking - Part 1
Ways of looking - Part 1Ways of looking - Part 1
Ways of looking - Part 1amandakane1
 
Tersigni_Ance_24052011
Tersigni_Ance_24052011Tersigni_Ance_24052011
Tersigni_Ance_24052011Rèdais
 
Photo app e sharing:beyond apple device
Photo app e sharing:beyond apple devicePhoto app e sharing:beyond apple device
Photo app e sharing:beyond apple deviceGreta Babbini
 

Viewers also liked (20)

Developing The Mongolian Wolf Spirit In Football
Developing The Mongolian Wolf Spirit In FootballDeveloping The Mongolian Wolf Spirit In Football
Developing The Mongolian Wolf Spirit In Football
 
K2 atomic structure
K2 atomic structureK2 atomic structure
K2 atomic structure
 
The Krass Family 2010
The Krass Family 2010The Krass Family 2010
The Krass Family 2010
 
Tarot: Card Interpretation
Tarot: Card InterpretationTarot: Card Interpretation
Tarot: Card Interpretation
 
Businesssystempresentation101409 100107132037 Phpapp01
Businesssystempresentation101409 100107132037 Phpapp01Businesssystempresentation101409 100107132037 Phpapp01
Businesssystempresentation101409 100107132037 Phpapp01
 
Necesidades de comunicación e información en el paciente refworks uniform
Necesidades de comunicación e información en el paciente refworks uniformNecesidades de comunicación e información en el paciente refworks uniform
Necesidades de comunicación e información en el paciente refworks uniform
 
Ways of Looking 1
Ways of Looking 1Ways of Looking 1
Ways of Looking 1
 
Wb2000 panorama action smash
Wb2000 panorama action smashWb2000 panorama action smash
Wb2000 panorama action smash
 
โครงงานครั้งที่........
โครงงานครั้งที่........โครงงานครั้งที่........
โครงงานครั้งที่........
 
Business Development Strategies for Lawyers
Business Development Strategies for LawyersBusiness Development Strategies for Lawyers
Business Development Strategies for Lawyers
 
Eagle crest gruppo 5. ordinate
Eagle crest gruppo 5. ordinateEagle crest gruppo 5. ordinate
Eagle crest gruppo 5. ordinate
 
Advert planning
Advert planningAdvert planning
Advert planning
 
AIS - Workstations
AIS - WorkstationsAIS - Workstations
AIS - Workstations
 
Ud 0 final juani
Ud 0 final juaniUd 0 final juani
Ud 0 final juani
 
Ordenagailuaren zatiak
Ordenagailuaren zatiakOrdenagailuaren zatiak
Ordenagailuaren zatiak
 
Podcast tutorial
Podcast tutorialPodcast tutorial
Podcast tutorial
 
Ways of looking - Part 1
Ways of looking - Part 1Ways of looking - Part 1
Ways of looking - Part 1
 
Tersigni_Ance_24052011
Tersigni_Ance_24052011Tersigni_Ance_24052011
Tersigni_Ance_24052011
 
Photo app e sharing:beyond apple device
Photo app e sharing:beyond apple devicePhoto app e sharing:beyond apple device
Photo app e sharing:beyond apple device
 
FASHION SHOW
FASHION SHOWFASHION SHOW
FASHION SHOW
 

Similar to Dti2143 chap 4 control statement part 2

Similar to Dti2143 chap 4 control statement part 2 (20)

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
Presentation1
Presentation1Presentation1
Presentation1
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Looping
LoopingLooping
Looping
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
 
Chapter06.PPT
Chapter06.PPTChapter06.PPT
Chapter06.PPT
 
control statement
control statement control statement
control statement
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
C tutorial
C tutorialC tutorial
C tutorial
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 

More from alish sha

T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9alish sha
 
July 2014 theory exam (theory)
July 2014 theory exam (theory)July 2014 theory exam (theory)
July 2014 theory exam (theory)alish sha
 
Accounting basic equation
Accounting basic equation Accounting basic equation
Accounting basic equation alish sha
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifahalish sha
 
It 302 computerized accounting (week 1) - sharifah
It 302   computerized accounting (week 1) - sharifahIt 302   computerized accounting (week 1) - sharifah
It 302 computerized accounting (week 1) - sharifahalish sha
 
What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)alish sha
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13alish sha
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13alish sha
 
Lab 5 2012/2012
Lab 5 2012/2012Lab 5 2012/2012
Lab 5 2012/2012alish sha
 
Purpose elaborate
Purpose elaboratePurpose elaborate
Purpose elaboratealish sha
 
Test 1 alish schema 1
Test 1 alish schema 1Test 1 alish schema 1
Test 1 alish schema 1alish sha
 
Lab 6 sem ii_11_12
Lab 6 sem ii_11_12Lab 6 sem ii_11_12
Lab 6 sem ii_11_12alish sha
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&aalish sha
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&aalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Attn list test
Attn list testAttn list test
Attn list testalish sha
 
Carry markdam31303
Carry markdam31303Carry markdam31303
Carry markdam31303alish sha
 

More from alish sha (20)

T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9
 
July 2014 theory exam (theory)
July 2014 theory exam (theory)July 2014 theory exam (theory)
July 2014 theory exam (theory)
 
Accounting basic equation
Accounting basic equation Accounting basic equation
Accounting basic equation
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifah
 
It 302 computerized accounting (week 1) - sharifah
It 302   computerized accounting (week 1) - sharifahIt 302   computerized accounting (week 1) - sharifah
It 302 computerized accounting (week 1) - sharifah
 
What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13
 
Lab 6
Lab 6Lab 6
Lab 6
 
Lab 5 2012/2012
Lab 5 2012/2012Lab 5 2012/2012
Lab 5 2012/2012
 
Purpose elaborate
Purpose elaboratePurpose elaborate
Purpose elaborate
 
Lab sheet 1
Lab sheet 1Lab sheet 1
Lab sheet 1
 
Test 1 alish schema 1
Test 1 alish schema 1Test 1 alish schema 1
Test 1 alish schema 1
 
Lab 6 sem ii_11_12
Lab 6 sem ii_11_12Lab 6 sem ii_11_12
Lab 6 sem ii_11_12
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&a
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&a
 
Final project
Final projectFinal project
Final project
 
Final project
Final projectFinal project
Final project
 
Attn list test
Attn list testAttn list test
Attn list test
 
Carry markdam31303
Carry markdam31303Carry markdam31303
Carry markdam31303
 

Dti2143 chap 4 control statement part 2

  • 1. Chapter 4 Selection RECAP
  • 2. IF, ELSE if(Control Statement) { Selection operation } Else { Selection operation }
  • 3. IF ELSE SAMPLE int 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); }
  • 4. IF, ELSE IF, ELSE If (Control Statement) { Selection operation/action } else if (Control Statement) { Selection operation/action } else { Selection operation/action }
  • 5. IF, ELSE IF, ELSE SAMPLE int main(){ int marks; printf("Enter Marks :"); scanf("%d",&marks); if ( marks<=100 && marks>=80){printf("Your grade is A");} else if ( marks<=79 && marks>=60){printf("Your grade is B");} else if ( marks<=59 && marks>=40){printf("Your grade is C");} else if ( marks<=39 && marks>=20){printf("Your grade is D");} else if ( marks<=19 && marks>=0){printf("Your grade is E");} else{printf("The exam mark must be between 0 and 100 inclusive.");} return 0; }
  • 6. CHAP 4 SWITCH, WHILE LOOP, DO WHILE LOOP,
  • 7. SWITCH CASE Format: Only Integer/ character switch( conditional expression/variable) { case label1: C statement; break; case label2: C statement; break; … default: pernyataanC; } value Exit from option Functioning like else
  • 8. How To Convert this If Else Snippet to Switch? if ( input == 1) printf(“One”); else if (input == 2) printf(“Two”); else if (input == 3) printf(“Three”); else if (input == 4) printf(“Four”); else printf(“Five”);
  • 9. Answer: switch(input) { case 1: printf("One"); break; case 2: printf("Two");break; case 3: printf("Three");break; case 4: printf("Four");break; default: printf("Five");break; }
  • 10. IF-ELSE / SWITCH CASE Side by Side switch(input) { case 1: printf("One"); break; case 2: printf("Two");break; case 3: printf("Three");break; case 4: printf("Four");break; default: printf("Five");break; } if ( input == 1) printf(“One”); else if (input == 2) printf(“Two”); else if (input == 3) printf(“Three”); else if (input == 4) printf(“Four”); else printf(“Five”);
  • 11.
  • 12. Format :IMPORTANT! 1. Counter 2. Counter initialization 3. Conditional expression testing 4. Increment/decrement of counter counter = 0; while (conditional expression) { C statement; counter++; }
  • 13.
  • 14. Must be declared as an integer
  • 15. Used to count amount of loops
  • 16. Can be initialized starting from any value.
  • 17.
  • 18. Used to ensure the loop/repetition is occurred.
  • 19. Example: while ( I < 4)
  • 20.
  • 21. 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.
  • 22. Example 1: void main() { int nom1,nom2,nom3,nom4,nom5; printf(“Enter 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; inti; i =0; while(i <5) { printf(“nEnter one number:”); scanf(“%d”,&nom); i++; } } counter condition increment WHILE LOOP CONVERT
  • 23. Determine the output of the following whileloop statement : a = 1; while ( a <= 5) { printf(“Number %d”,a); a++; }
  • 24. Use do..while statement Repetition will occurred if condition is TRUE Similar to while, must include 3 important things Format : counter = 0; do { C statement; counter++; } while (x < 5); Repeated statements DO-WHILE LOOP
  • 26. 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); } while do..while DO-WHILE to WHILE LOOP CONVERT
  • 27.
  • 28. Repeat statement until condition is FALSE
  • 29. Similar to while loop, must includes 3 important things
  • 30. Format :for (initial_value; conditional expression ;increment) { C statement; } Repeated statements
  • 31. FOR LOOP SAMPLE intbil, nilai = 3; for (bil = 5; bil >0; bil--) { if( bil % 2 == 1) printf(“ %d”, bil + nilai); }
  • 32. WHILE,DO-WHILE and FOR LOOP side by side
  • 33.
  • 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. 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