pd controlflow repetition ia ub

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

Upload: yahya

Post on 10-Nov-2015

222 views

Category:

Documents


6 download

DESCRIPTION

just support

TRANSCRIPT

  • Pemrograman DasarControl Flow Statements:Repetition/Looping

    PTIIK - UB*

  • StatementsControl 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 statementRepetition/looping: while statementfor statementdo-while statementBranching statement:break statementcontinue statementreturn statement*

  • Three forms of repetition statements:while statementsfor statementsdo-while statements

    *Repetition/Looping

  • Syntax:while (boolean_exp) statement;orwhile(boolean_exp){ statement1; statement2; ..}*boolean_expstatementstruefalsewhile statements

  • Example:while(product
  • 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.*break dan continue

  • Example:

    int x = 1; while(x5) break; }*Exit from the loopbreak

  • Example:*int x; for(x=1; x
  • Exercise*

  • ExerciseInput:Berapakah kue untuk si Rakus? AsumsiMisalkan jawaban pertanyaan di atas adalah 5Maka output-nya:Si Rakus melihat 5 kue, memakan 1, menyisakan 4Si Rakus melihat 4 kue, memakan 1, menyisakan 3Si Rakus melihat 3 kue, memakan 1, menyisakan 2Si Rakus melihat 2 kue, memakan 1, menyisakan 1Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya

    *

  • SubmissionDeadline: 21 October 2012To: [email protected]: [PROGDAS SI] Si RakusContent: Nama: NIM: Kelas: Table Source code *

  • for statementsYou 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; }*

  • for (exp1; exp2; exp3)statement;

    exp1: initialization-expressionexp2: 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.*for statements

  • *exp1exp3statementsexp2for statements

  • *Example: for (x=1; x
  • Examples:

    *int x;for(x=1; x=1; x--) System.out.println(x);for statements

  • exp1 and exp3 may consist of more than one expressions, separated by comma(s)Examples:*

    int i,j; for(i=1, j=30; i

  • Infinite Loopfor(;;){ }

    Contoh:

    * int i = 0; for(;;){ i++; printf("%d ", i); if (i>=10) break; // break out of the loop } for statements

  • Equivalence of for and while:

    *exp1; while(exp2){ statement1; statement2; . exp3 } for(exp1; exp2; exp3){ statement1; statement2; . } equivalentwhile and for

  • Equivalence of for and while:

    Contoh:*

    int x; for(x=1;x

  • ExerciseInput:Berapakah kue untuk si Rakus? AsumsiMisalkan jawaban pertanyaan di atas adalah 5Maka output-nya:Si Rakus melihat 5 kue, memakan 1, menyisakan 4Si Rakus melihat 4 kue, memakan 1, menyisakan 3Si Rakus melihat 3 kue, memakan 1, menyisakan 2Si Rakus melihat 2 kue, memakan 1, menyisakan 1Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya

    *

    **