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

Preview:

Citation preview

Control Structures in CControl Structures in C

Risanuri Hidayat, Ir., M.Sc.

ConditionConditionOperator Meaning Example

== Equal to count == 10

!= Not equal to flag != DONE

< Less than a < b

<= Less than or equal to <= LIMIT

> Greater than pointer > end_of_list

>= Greater than or equal to lap >= start

Control Structures in CControl Structures in C

These include – ifelse,– while, – do-while, – for, and a selection statement called– switch.

if-elseif-else

The if-else statement can exist in two forms: with or without the else. The two forms are:

if(expression)statement

orif(expression)

statement1else

statement2

if-elseif-else

Ifif (condition)

statement1;

else

statement2;

int a, b;

// ...

if(a < b) a = 0;

else b = 0;

Nested ifNested 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)

if-else-if Ladderif-else-if Ladder

Bentuknya:

if(condition)statement;

else if(condition)statement;

else if(condition)statement;...elsestatement;

if-else-if Ladderif-else-if Ladder

// Demonstrate if-else-if statements (IfElse.c).#include <stdio.h>main () {

int bulan = 4; // Aprilchar 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);}

switchswitch switch merupakan statement percabangan dengan banyak

cabang. Bentuknya seperti berikut:

switch (expression) {case value1:

// statement sequencebreak;

case value2:// statement sequencebreak;

.

.

.case valueN:

// statement sequencebreak;

default:// default statement sequence

}

switchswitch 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 switchNested 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 switchprintf("target is one");break;

} // switch(target)break;

case 2: // ...

IterationIteration

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

}

whilewhile

// 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-whiledo-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-whiledo-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

forfor

For merupakan statement loop yang paling sering digunakan dalam berbagai bahasa, termasuk C.

Berikut ini bentuk umumnya:

for(initialization; condition; iteration) {// body

}

forfor

// Demonstrate the for loop (loop.c).

#include <stdio.h>

main() {

int n;

for(n=10; n>0; n--)

printf("tick %d \n",n);

}

forfor

// 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 LoopsNested 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");

}}

JumpJump

C supports four jump statements: • break, • continue, • return• goto.

These statements transfer control to another part of your program.

breakbreak

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.

breakbreak

// 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.");

}

breakbreak

// 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.");

}

continuecontinue

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.

continuecontinue

// 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");

}}

returnreturn

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( ).

returnreturn

// 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.");

}

gotogoto

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:

gotogoto

// 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");}

}

Recommended