Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures in C Risanuri Hidayat, Ir., M.Sc.

Similar presentations


Presentation on theme: "Control Structures in C Risanuri Hidayat, Ir., M.Sc."— Presentation transcript:

1 Control Structures in C Risanuri Hidayat, Ir., M.Sc.

2 Condition OperatorMeaningExample ==Equal tocount == 10 !=Not equal toflag != DONE <Less thana < b <=Less than or equal to<= LIMIT >Greater thanpointer > end_of_list >=Greater than or equal tolap >= start

3 Control Structures in C These include –ifelse, –while, –do-while, –for, and a selection statement called –switch.

4 if-else The if-else statement can exist in two forms: with or without the else. The two forms are: if(expression) statement or if(expression) statement1 else statement2

5 if-else If if (condition) statement1; else statement2; int a, b; //... if(a < b) a = 0; else b = 0;

6 Nested if nested if adalah statement if yang targetnya adalahjuga if atau else. if (i == 10) { if (j < 20) a = b; if (k > 100) c = d; // this if is else a = c; // associated with this else } else a = d; // this else refers to if(i == 10)

7 if-else-if Ladder Bentuknya: if(condition) statement; else if(condition) statement; else if(condition) statement;. else statement;

8 if-else-if Ladder // Demonstrate if-else-if statements (IfElse.c). #include main () { int bulan = 4; // April char season[10]; if(bulan == 12 || bulan == 1 || bulan == 2) strcpy(season,"Salak"); else if(bulan == 3 || bulan == 4 || bulan == 5) strcpy(season,"Durian"); else if(bulan == 6 || bulan == 7 || bulan == 8) strcpy(season,"Mangga"); else if(bulan == 9 || bulan == 10 || bulan == 11) strcpy(season,"Jeruk"); else strcpy(season,"Mbuh"); printf("April adalah musim %s\n ",season); }

9 switch switch merupakan statement percabangan dengan banyak cabang. Bentuknya seperti berikut: switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break;. case valueN: // statement sequence break; default: // default statement sequence }

10 switch expression harus bertype byte, short, int, or char; // A simple example of the switch(switch.c) #include main() { int i; for(i=0; i<6; i++) switch(i) { case 0: printf("i is zero.\n"); break; case 1: printf("i is one.\n"); break; case 2: printf("i is two.\n"); break; case 3: printf("i is three.\n"); break; default: printf("i is greater than 3.\n"); } // switch } // main

11 Nested switch Kita dapat juga membuat statement switch di dalam switch yang lain switch(count) { case 1: switch(target) { // nested switch case 0: printf("target is zero"); break; case 1: // no conflicts with outer switch printf("target is one"); break; } // switch(target) break; case 2: //...

12 Iteration while while loop merupakan dasar looping di C. While akan mengulang statement jika kondisi yang disyaratkan benar. Bentuk statement while adalah: while(condition) { // body of loop }

13 while // Demonstrate the while loop (while.c). #include main() { int n = 10; while(n > 0) { printf("tick %d \n",n); n--; } // while } // main

14 do-while Sering kali dalam program kita membuat instruksi terlebih dahulu baru kemudian di-test hasilnya. Hal ini juga sering terjadi dalam looping. C mm-fasilitasi hal ini dengan do-while. Bentuknya sebagai berikut: do { // body of loop } while (condition);

15 do-while // Demonstrate the do-while loop (dowhile.c). #include main() { int n = 10; do { printf("tick %d \n",n); n--; } while(n > 0); } // main

16 for For merupakan statement loop yang paling sering digunakan dalam berbagai bahasa, termasuk C. Berikut ini bentuk umumnya: for(initialization; condition; iteration) { // body }

17 for // Demonstrate the for loop (loop.c). #include main() { int n; for(n=10; n>0; n--) printf("tick %d \n",n); }

18 for // Using the comma (comma.c) #include main() { int a, b; for(a=1, b=4; a<b; a++, b--) { printf("a = %d \n", a); printf("b = %d \n", b); }

19 Nested Loops Like all other programming languages, C allows loops to be nested. That is, one loop may be inside another. For example, here is a program that nests for loops: // Loops may be nested (nestedfor.c). #include main() { int i, j; for(i=0; i<10; i++) { for(j=i; j<10; j++) printf("."); printf("\n"); }

20 Jump C supports four jump statements: break, continue, return goto. These statements transfer control to another part of your program.

21 break In C, the break statement has two uses. –First, as you have seen, it terminates a statement sequence in a switch statement. –Second, it can be used to exit a loop.

22 break // Using break to exit a loop (break.c). #include main() { int i; for(i=0; i<100; i++) { if(i == 10) break; // terminate loop if i is 10 printf("i: %d \n", i); } printf("Loop complete."); }

23 break // Using break to exit a while loop (break2.c). #include main() { int i = 0; while(i < 100) { if(i == 10) break; // terminate loop if i is 10 printf("i: %d \n", i); i++; } printf("Loop complete."); }

24 continue continue go immediately to next iteration of loop In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.

25 continue // Demonstrate continue (continue.c). #include main() { int i; for(i=0; i<10; i++) { printf("%d ", i); if (i%2 == 0) continue; printf("\n"); }

26 return The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method. The following example illustrates this point. Here, return causes execution to return to the C, since it is the run-time system that calls main( ).

27 return // Demonstrate return (return.c). #include main() { int t = 1; printf("Before the return."); if(t==1) return; // return to caller printf("This won't execute."); }

28 goto It is possible to jump to any statement within the same function using goto. A label is used to mark the destination of the jump. goto label1; : label1:

29 goto // Using continue with a label (goto.c). #include main() { int i,j; for (i=0; i<10; i++) { for(j=0; j<10; j++) { if(j > i) { printf("\n"); goto outer; } printf(" %d", (i * j)); } outer: printf(".. outer..\n"); }


Download ppt "Control Structures in C Risanuri Hidayat, Ir., M.Sc."

Similar presentations


Ads by Google