Your SlideShare is downloading. ×
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
การควบคุมภาษา C
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

การควบคุมภาษา C

621

Published on

การใช้

การใช้

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
621
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Chapter 3C Language Control Mr.Warawut Khangkhan Twitter: http://twitter.com/awarawut Facebook: http://www.facebook.com/AjWarawut E-Mail: awarawut@hotmail.com Mobile: 083-0698-410
  • 2. Contents Decision / Selection Loop / Repetition Break and Continue Statement Mr.Warawut Khangkhan Chapter 3 C Language Control 2
  • 3. DECISION / SELECTION Mr.Warawut Khangkhan Chapter 3 C Language Control 3
  • 4. Decision / Selection if then if then else Nested if switch Mr.Warawut Khangkhan Chapter 3 C Language Control 4
  • 5. if thenFormat: if (expression) statement; Mr.Warawut Khangkhan Chapter 3 C Language Control 5
  • 6. if thenFormat: if (expression) { statement-1; statement-2; … } Mr.Warawut Khangkhan Chapter 3 C Language Control 6
  • 7. Example if1.c#include <stdio.h>#include <stdlib.h>int main( ) { int i; scanf(“%d”, &i); if (i > 0) printf(“a positive number was enteredn”); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 7
  • 8. Example if2.c#include <stdio.h>#include <stdlib.h>int main( ) { int i; scanf(“%d”, &i); if (i < 0) { printf(“a negative number was enteredn”); i = -i; } system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 8
  • 9. if then else Mr.Warawut Khangkhan Chapter 3 C Language Control 9
  • 10. if then elseFormat: if (expression) statement-true; else statement-false; Mr.Warawut Khangkhan Chapter 3 C Language Control 10
  • 11. if then else Mr.Warawut Khangkhan Chapter 3 C Language Control 11
  • 12. if then elseFormat: if (expression) { statement-1; statement-2; … } else { statement-3; statement-4; … } Mr.Warawut Khangkhan Chapter 3 C Language Control 12
  • 13. Example ifthen1.c#include <stdio.h>#include <stdlib.h>int main( ) { int i; scanf(“%d”, &i); if (i > 0) printf(“a negative number was enteredn”); else printf(“a negative number was enteredn”); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 13
  • 14. Nested if Mr.Warawut Khangkhan Chapter 3 C Language Control 14
  • 15. Nested ifFormat: if (expression-1) statement-1; else if (expression-2) statement-2; else if (expression-3) statement-3; else statement-4; Mr.Warawut Khangkhan Chapter 3 C Language Control 15
  • 16. Example nestedif.c#include <stdio.h>#include <stdlib.h>int main( ) { int score; printf(“Enter score : “); scanf(“%d”, &score); if (score > 79) printf(“Very Goodn”); else if (score > 49) printf(“Goodn”); else printf(“Badn”);} Mr.Warawut Khangkhan Chapter 3 C Language Control 16
  • 17. switch Mr.Warawut Khangkhan Chapter 3 C Language Control 17
  • 18. switchFormat: switch (expression) { case const-expr.1: statement-1; break; case const-expr.2: statement-2; break; case const-expr.3: statement-3; break; … [default: statement-default;] } Mr.Warawut Khangkhan Chapter 3 C Language Control 18
  • 19. Example switch1.c#include <stdio.h>#include <stdlib.h>int main( ) { char ch; printf(“Enter char : “); ch = getchar( ); switch (ch) { case a: case A: printf(“Additionn”); break; case s: case S: printf(“Subtractn”); break; case q: case Q: printf(“Quitn”); break; default: printf(“Other Charactern”); } system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 19
  • 20. Example switch2.c#include <stdio.h>#include <stdlib.h>int main( ) { int n = ?; swicth (n) { case 2: n++; case 4: n++; break; case 6: n++; break; case 8: n++; default: n++; } printf(“N = %dn”, n); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 20
  • 21. LOOP / REPETITION Mr.Warawut Khangkhan Chapter 3 C Language Control 21
  • 22. Loop / Repetition while loop do ... while loop for loop Mr.Warawut Khangkhan Chapter 3 C Language Control 22
  • 23. while loopFormat: while (expression) statement; Mr.Warawut Khangkhan Chapter 3 C Language Control 23
  • 24. while loopFormat: while (expression) { statement-1; statement-2; … } Mr.Warawut Khangkhan Chapter 3 C Language Control 24
  • 25. Example while1.c#include <stdio.h> while (num <= 10) {#include <stdlib.h> printf(“%dn”, num); num++;int main( ) { } int num = 1; while (num <= 10) printf(“%dn”, num++); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 25
  • 26. Example while2.c#include <stdio.h>#include <stdlib.h>int main( ) { int num = 1; int sum = 0; while (num <= 10) { sum += num; // sum = sum + num; num++; // num = num + 1; } printf(“Summary of 1-10 = %dn”, sum); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 26
  • 27. do ... while loopFormat: do statement; while (expression); Mr.Warawut Khangkhan Chapter 3 C Language Control 27
  • 28. do ... while loopFormat: do { statement-1; statement-2; … } while (expression); Mr.Warawut Khangkhan Chapter 3 C Language Control 28
  • 29. Example dowhile1.c#include <stdio.h> do {#include <stdlib.h> printf(“%dn”, num);int main( ) { num++; int num = 1; } while (num <= 10); do printf(“%dn”, num++); while (num <= 10); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 29
  • 30. Example dowhile2.c#include <stdio.h>#include <stdlib.h>int main( ) { int num = 1; int sum = 0; do { sum += num; // sum = sum + num; num++; // num = num + 1; } while (num <= 10); printf(“Summary of 1-10 = %dn”, sum); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 30
  • 31. for loopFormat: for (expr.1; expr.2; expr.3) statement;or for (expr.1; expr.2; expr.3) { statement-1; statement-2; … } Mr.Warawut Khangkhan Chapter 3 C Language Control 31
  • 32. Example for1.c#include <stdio.h> for (num = 1; num <= 10; ) {#include <stdlib.h> printf(“%dn”, num); num++;int main( ) { } int num; for (num = 1; num <= 10; num++) printf(“%dn”, num); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 32
  • 33. Example for2.c#include <stdio.h>#include <stdlib.h>int main( ) { int num = 1; for ( ; num <= 10; ) { printf(“%dn”, num; num++; } system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 33
  • 34. Example for3.c#include <stdio.h>#include <stdlib.h>int main( ) { int num; int sum = 0; for (num = 1; num <= 10; num++) { sum += num; // sum = sum + num; } printf(“Summary of 1-10 = %dn”, sum); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 34
  • 35. Change with for loop to while loopfor (expr.1; expr.2; expr.3) statement; 1 2 expr.1; 4 3 while (expr.2) { statement; expr.3; } Mr.Warawut Khangkhan Chapter 3 C Language Control 35
  • 36. Change with for loop to do .. whileloopfor (expr.1; expr.2; expr.3) statement; 1 2 expr.1; 4 3 do { statement; expr.3; } while (expr.2); Mr.Warawut Khangkhan Chapter 3 C Language Control 36
  • 37. Change with while loop todo .. while loopexpr.1;while (expr.2) { 1 statement; 2 expr.3; } 3 expr.1; do { 4 statement; expr.3; } while (expr.2); Mr.Warawut Khangkhan Chapter 3 C Language Control 37
  • 38. BREAK AND CONTINUESTATEMENT Mr.Warawut Khangkhan Chapter 3 C Language Control 38
  • 39. Break and Continue Statementbreak statement FF F Fcontinue statement F F while, do..while for F F F Mr.Warawut Khangkhan Chapter 3 C Language Control 39
  • 40. Example break.c#include <stdio.h>#include <stdlib.h>int main( ) { int num; for (num = 1; num <= 10; num++) if (num == 5) break; else printf(“%d”, num); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 40
  • 41. Example continue.c#include <stdio.h>#include <stdlib.h>int main( ) { int num; for (num = 1; num <= 10; num++) if (num == 5) continue; else printf(“%d”, num); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 3 C Language Control 41

×