290 likes | 448 Views
Control Structures in C. Risanuri Hidayat, Ir., M.Sc. Condition. Control Structures in C. These include ifelse , while , do-while , for , and a selection statement called switch . if-else. The if-else statement can exist in two forms: with or without the else . The two forms are:
E N D
Control Structures in C Risanuri Hidayat, Ir., M.Sc.
Control Structures in C • These include • ifelse, • while, • do-while, • for, and a selection statement called • switch.
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
if-else • If if (condition) statement1; else statement2; int a, b; // ... if(a < b) a = 0; else b = 0;
Nested if • nested if adalahstatement 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)
if-else-if Ladder • Bentuknya: if(condition) statement; else if(condition) statement; else if(condition) statement; . . . else statement;
if-else-if Ladder // Demonstrate if-else-if statements (IfElse.c). #include <stdio.h> 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); }
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 }
switch • expression harus bertype byte, short, int, or char; // A simple example of the switch(switch.c) #include <stdio.h> 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
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: // ...
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 }
while // Demonstrate the while loop (while.c). #include <stdio.h> main() { int n = 10; while(n > 0) { printf("tick %d \n",n); n--; } // while } // main
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);
do-while // Demonstrate the do-while loop (dowhile.c). #include <stdio.h> main() { int n = 10; do { printf("tick %d \n",n); n--; } while(n > 0); } // main
for • For merupakan statement loop yang paling sering digunakan dalam berbagai bahasa, termasuk C. • Berikut ini bentuk umumnya: for(initialization; condition; iteration) { // body }
for // Demonstrate the for loop (loop.c). #include <stdio.h> main() { int n; for(n=10; n>0; n--) printf("tick %d \n",n); }
for // Using the comma (comma.c) #include <stdio.h> main() { int a, b; for(a=1, b=4; a<b; a++, b--) { printf("a = %d \n", a); printf("b = %d \n", b); } }
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 <stdio.h> main() { int i, j; for(i=0; i<10; i++) { for(j=i; j<10; j++) printf("."); printf("\n"); } }
Jump • C supports four jump statements: • break, • continue, • return • goto. • These statements transfer control to another part of your program.
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.
break // Using break to exit a loop (break.c). #include <stdio.h> 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."); }
break // Using break to exit a while loop (break2.c). #include <stdio.h> 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."); }
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.
continue // Demonstrate continue (continue.c). #include <stdio.h> main() { int i; for(i=0; i<10; i++) { printf("%d ", i); if (i%2 == 0) continue; printf("\n"); } }
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( ).
return // Demonstrate return (return.c). #include <stdio.h> main() { int t = 1; printf("Before the return."); if(t==1) return; // return to caller printf("This won't execute."); }
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:
goto // Using continue with a label (goto.c). #include <stdio.h> 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"); } }