Program Control: Repetition

Program Control: Repetition
Philip kwandynata
2201736366
BINUS


Repetition Definition
        Pengulagan satu atau lebih pada suatu kondisi tertentu.

          Repetition/looping can use 3 types of operation=
-        - for
-        - while
-        - do-while

              for explanation
 
        Syntax:
for(exp 1; exp2; exp3) statement;
or:
for(exp1; exp2; exp3){
                        statement1;
                        statement2;
                        …….
 }

exp1 :  initialization
exp2 :  conditional
exp3 :  increment or decrement 

exp1, exp2 and exp3 are optional
exp1 and exp3 can consist of several expression separated with comma

        Flow Chart of for




        Sample of for with flow chart
for (x=1; x <= 10; x++) printf(“%d\n”, x);




 
        Infinite Loop
Loop with no stop condition can use “for-loop” by removing all parameters (exp1, exp2, exp3). To end the loop use break.

        Nested Loop
Loop in a loop. The repetition operation will start from the inner side loop.


















int x, y;
for (x=1;x<=5;x++)
     for (y=5; y>=1; y--)
           printf(”%d %d ”,x,y);
 


FOR C ==>














for (int x=1;x<=5;x++)
     for (int y=5; y>=1; y--)
           printf(”%d %d ”,x,y);
 

FOR C++ ==>













 



Comments