Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Similar presentations


Presentation on theme: "Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1."— Presentation transcript:

1 Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1

2 Statements  Control flow statements regulate the order in which statements get executed.  Kinds of control flow statements:  Decision making:  if-then statement  if-then-else statement,  switch-case statement  Repetition/looping:  while statement  for statement  do-while statement  Branching statement:  break statement  continue statement  return statement 2

3  Three forms of repetition statements:  while statements  for statements  do-while statements 3 Repetition/Looping

4  Syntax: while (boolean_exp) statement; or while(boolean_exp){ statement1; statement2;.. } 4 boolean_exp statements true false while statements

5  Example: while(product <= 1000) product = 2*product; 5 product <= 1000 product=2*product; false true while statements

6  break  Exit from decision-making ( switch ) or repetition ( for, while dan do-while )  continue  Skips the current iteration of a for, while and do- while. 6 break dan continue

7 Example: int x = 1; while(x<=10){ System.out.println(x); x++; if (x>5) break; } 7 Exit from the loop break

8  Example: 8 int x; for(x=1; x<=10; x++) { if (x == 5) continue; System.out.println(x); } continue

9 Exercise 9

10  Input: Berapakah kue untuk si Rakus? …  Asumsi Misalkan jawaban pertanyaan di atas adalah 5  Maka output-nya: Si Rakus melihat 5 kue, memakan 1, menyisakan 4 Si Rakus melihat 4 kue, memakan 1, menyisakan 3 Si Rakus melihat 3 kue, memakan 1, menyisakan 2 Si Rakus melihat 2 kue, memakan 1, menyisakan 1 Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya 10

11 Submission Deadline: 21 October 2012 To: Subject: [PROGDAS SI…] Si Rakus Content: Nama: NIM: Kelas: Table Source code 11

12 for statements  You use the basic for statement to loop over a range of values from beginning to end. for (initialization-expression; loop-expression; update-expression) statement; //or for (initialization-expression; loop-expression; update-expression) { statement; … } 12

13 for (exp1; exp2; exp3) statement; exp1 : initialization-expression exp2 : loop-expression exp3 : update-expression The initialization-expression allows you to declare and/or initialize loop variables, and is executed only once. Then the loop-expression of boolean or Boolean type is evaluated and if it is true the statement in the body of the loop is executed. After executing the loop body, the update-expression is evaluated, usually to update the values of the loop variables, and then the loop-expression is reevaluated. This cycle repeats until the loop-expression is found to be false. The presence of exp1, exp2, and exp3 are optional. 13 for statements

14 14 exp1 exp3 statements exp2 true false exp1 exp3 statements exp2 for statements

15 15  Example: for (x=1; x <= 10; x++) System.out.println(“x =” + x); x = 1 x++ System.out.println(“x =” + x); x<=100 true false for statements

16  Examples: 16 int x; for(x=1; x<=10; x++) System.out.println(x); int x; for(x=10; x>=1; x--) System.out.println(x); for statements

17  exp1 and exp3 may consist of more than one expressions, separated by comma(s)  Examples: 17 int i,j; for(i=1, j=30; i

18  Infinite Loop for(;;){ … } Contoh: 18 int i = 0; for(;;){ i++; printf("%d ", i); if (i>=10) break; // break out of the loop } for statements

19  Equivalence of for and while: 19 exp1; while(exp2){ statement1; statement2; …. exp3 } exp1; while(exp2){ statement1; statement2; …. exp3 } for(exp1; exp2; exp3){ statement1; statement2; …. } for(exp1; exp2; exp3){ statement1; statement2; …. } equivalent while and for

20  Equivalence of for and while:  Contoh: 20 int x; for(x=1;x<=10;x++) System.out.println(x); exp1; while(exp2){ statement1; statement2; …. exp3 } exp1; while(exp2){ statement1; statement2; …. exp3 } for(exp1; exp2; exp3){ statement1; statement2; …. } for(exp1; exp2; exp3){ statement1; statement2; …. } equivalent while and for int x=1; while(x<=10){ System.out.println(x); x++; }

21 Exercise  Input: Berapakah kue untuk si Rakus? …  Asumsi Misalkan jawaban pertanyaan di atas adalah 5  Maka output-nya: Si Rakus melihat 5 kue, memakan 1, menyisakan 4 Si Rakus melihat 4 kue, memakan 1, menyisakan 3 Si Rakus melihat 3 kue, memakan 1, menyisakan 2 Si Rakus melihat 2 kue, memakan 1, menyisakan 1 Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya 21


Download ppt "Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1."

Similar presentations


Ads by Google