Program Control : Repetition

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

Repetition : FOR

Syntax : 
for(exp1;exp2;exp3){ exp1 is initialization
statement1; exp2 is condition
statement2; exp3 is increment or decrement
………
}
Example :
for(int i =0;i<number;i++){
number1 = number1 + 2;
number2 = number2 / 2;
}

  • 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.

Repetition : WHILE

- While check first then do the statement

Repetition : DO WHILE

- Do the statement once then check the condition

NIM : 2201755781
Binus.ac.id
Skyconnectiva.com
Nama : Virnando Tan Wijaya

Reference

https://binus.ac.id/

Comments

Popular posts from this blog

Pointers and Arrays

Cloud Computing