Repetition

Definition
  • One or more instruction repeated for certain amount of time
  • Number of repetition can be predefined (hard-coded in program) or defined later at run time
  • Repetition/looping operation:
           – for
         – while
         – do-while


       For 

for(exp1; exp2; exp3) statement;
or:
for(exp1; exp2; exp3){
  statement1;
  statement2;
  …….
 }
exp1 :  initialization
exp2 :  conditional
exp3 :  increment or decrement
exp1, exp2 and exp3 are optional


in that picture, we use repetition "For" for 

print the number of the case. the first 

variable is for how many we want to input and then the second one we input the

number we want to print.




While





this code is for while variable t is not 

20, it will print "this is not 20th line"

until t is 20 it will print "this is 20th 

line".




















while (exp) statements;

or:

while(exp){

statement1;

statement2;

…..

}




Do While






this code will keep print the number 

until t is not less than 11





















do{

< statements >;

} while(exp);

Keep executing while exp is true

exp evaluation done after executing the statement(s)

Komentar