C Programming Lecture no 5 Control Statements Department

  • Slides: 32
Download presentation
C Programming Lecture no. 5 Control Statements ������� Department of Computer Science /32 310322

C Programming Lecture no. 5 Control Statements ������� Department of Computer Science /32 310322 C Programming

คำสง ในการควบคมโปรแกรมแ บบเงอนไข Department of Computer Science /32 310322 C Programming

คำสง ในการควบคมโปรแกรมแ บบเงอนไข Department of Computer Science /32 310322 C Programming

������� if. . . else ������ if / else ������ if <condition> statement 1;

������� if. . . else ������ if / else ������ if <condition> statement 1; else statement 2; condition = ���������� statement = ���������� if (score>80) printf (“Excellent”); 3 else printf (“Good”); Department of Computer Science 310322 C Programming

Flow Chart ������� Yes Condition Statement 2 Statement 1 Department of Computer Science No

Flow Chart ������� Yes Condition Statement 2 Statement 1 Department of Computer Science No 4 310322 C Programming

������� if. . . else if ������ if / else ������ if <condition 1>

������� if. . . else if ������ if / else ������ if <condition 1> statement 1; else if <condition 2> statement 2; else statement 3; ���� if (score>80) printf (“Very Good”); else if (score>70) printf (“Good”); else printf (“Standard”); Department of Computer Science 5 310322 C Programming

������� Switch ������ switch(variable) { case constant 1: statement 1; break; case constant 2:

������� Switch ������ switch(variable) { case constant 1: statement 1; break; case constant 2: statement 2; break; } variable = ������ constant = ��������� int ���� char ��������� Department of Computer Science 6 310322 C Programming

Flow Chart ��������� No Condition 1 Yes Statement 1 Condition 2 No Yes Condition

Flow Chart ��������� No Condition 1 Yes Statement 1 Condition 2 No Yes Condition 3 Statement 2 Yes Statement 3 Department of Computer Science No 7 Statement 4 310322 C Programming

��������������������� if <value 1 operand value 2> statement 1; else if <value 3 operand

��������������������� if <value 1 operand value 2> statement 1; else if <value 3 operand value 4 logical operand value 5 operand value 6> statement 2; else statement 3; value = ���������� operand = ����������� logical operand = 8 Department of Computer Science 310322 C Programming

�������������� if (score > 70) printf (“Grade B”); else ((score<=70) && (score >=60)) printf

�������������� if (score > 70) printf (“Grade B”); else ((score<=70) && (score >=60)) printf (“Grade C”); else printf (“Grade D”); Department of Computer Science 10 310322 C Programming

����������� for ( initial; condition; increment (decrement) ) statement; initial = ��������� condition =

����������� for ( initial; condition; increment (decrement) ) statement; initial = ��������� condition = ��������� increment (decrement) = ����������� statement = ��������� Department of Computer Science 13 310322 C Programming

��������� for (i=1; i<10; i++) printf (“%d”, i); ���� Department of Computer Science for

��������� for (i=1; i<10; i++) printf (“%d”, i); ���� Department of Computer Science for (i=10; i>=1; i--) printf (“%d”, i); 14 310322 C Programming

Flow Chart ������� for Initial Condition Yes Statement No Increment (Decrement) Department of Computer

Flow Chart ������� for Initial Condition Yes Statement No Increment (Decrement) Department of Computer Science 15 310322 C Programming

����������� while (condition) statement; condition = ����������� statement = ����������� Department of Computer Science

����������� while (condition) statement; condition = ����������� statement = ����������� Department of Computer Science 17 310322 C Programming

�������� while ���� i=1; while ( i<10 ) { printf (“%d”, i); i++; }

�������� while ���� i=1; while ( i<10 ) { printf (“%d”, i); i++; } Department of Computer Science 18 310322 C Programming

Flow Chart ������ while Initial Condition Yes Statement No Increment (Decrement) Department of Computer

Flow Chart ������ while Initial Condition Yes Statement No Increment (Decrement) Department of Computer Science 19 310322 C Programming

3. คำสงวนรอบแบบทตรวจสอบเงอนไขทหลง : do. . . while รปแบ บทวไป do statement; while (นพจนเงอนไข เชน

3. คำสงวนรอบแบบทตรวจสอบเงอนไขทหลง : do. . . while รปแบ บทวไป do statement; while (นพจนเงอนไข เชน ); num = 2 ; do { num++; printf(“Now no is %dn”, num); } while (num == 10) Department of Computer Science 20 310322 C Programming

������ do. . . while ������ do { statement; } while (condition); condition =

������ do. . . while ������ do { statement; } while (condition); condition = ����������� statement = ����������� Department of Computer Science 21 310322 C Programming

��������� do. . . while ���� i=1; do { printf(“%d”, i); i++; } while

��������� do. . . while ���� i=1; do { printf(“%d”, i); i++; } while ( i<10 ); Department of Computer Science 22 310322 C Programming

Flow Chart ������� do. . . while Initial Statement Increment (Decrement) Condition Yes Department

Flow Chart ������� do. . . while Initial Statement Increment (Decrement) Condition Yes Department of Computer Science 23 No 310322 C Programming

คำสงควบคมอน ๆ break, continue, goto และ labels Department of Computer Science /32 310322 C

คำสงควบคมอน ๆ break, continue, goto และ labels Department of Computer Science /32 310322 C Programming

������ break; ���� for (i=1; i<10; i++) { if (i == 5 ) break;

������ break; ���� for (i=1; i<10; i++) { if (i == 5 ) break; printf (“%d”, i); } Department of Computer Science 26 310322 C Programming

������ continue; ���� for (i=1; i<10; i++) { if (i = = 5 )

������ continue; ���� for (i=1; i<10; i++) { if (i = = 5 ) continue; printf (“%d”, i); } Department of Computer Science 27 310322 C Programming

������ goto label; statement 1; label : statement 2; label = ������� statement =

������ goto label; statement 1; label : statement 2; label = ������� statement = ��������� Department of Computer Science 29 310322 C Programming

������ goto ���� if (num == 5 ) goto end_work; printf (“num not equa

������ goto ���� if (num == 5 ) goto end_work; printf (“num not equa 5”); end_work: printf (“num equa 5”); Department of Computer Science 30 310322 C Programming

�������� ���� goto #include<stdio. h < main() }int sum, n ; for(n=1; n<10; n++)

�������� ���� goto #include<stdio. h < main() }int sum, n ; for(n=1; n<10; n++) if (n==5) goto part 1; else printf(“%dn”, n); part 1 : printf(“Interrupt with no. 5n”); } Department of Computer Science 31 310322 C Programming

END Department of Computer Science /32 310322 C Programming

END Department of Computer Science /32 310322 C Programming