Program Control: Repetition
Sub Topics :
Sub Topics :
Program
Control - Repetition:
–Repetition Definition
–For
–While
–Do-While
–Repetition Operation
–Break vs Continue
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
–while
–do-while
For
Syntax:
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
While
Syntax
:
while (exp) statements;
or:
while(exp){
statement1;
statement2;
…..
}
Do-While
Syntax
:
do{
< statements >;
} while(exp);
Keep executing while exp is true
exp evaluation done after executing
the statement(s)
Repetition Operation
In while operation, statement block of
statements may never be executed at all if exp value is false
In do-while on the other hand statement block
of statements will be executed min once
To end the repetition, can be done
through several ways:
-Sentinel
-Question, should the repetition continue?
Break vs
Continue
break:
–ending
loop (for, while and do-while)
–end
the switch
operation
continue:
skip all the rest of statements (subsequent to
the skip statement) inside a repetition, and continue normally to the next loop
Summary
Repetition
is a condition which is one or more instruction repeated for certain amount of
time
3
types of repetition/looping in C:
-for
-do while
-while
2201760945
Skyconnectiva
Kevin Orlando Sutanto
-for
-do while
-while
2201760945
Skyconnectiva
Kevin Orlando Sutanto
Komentar
Posting Komentar