Repetition
In C language, there are three repetition or looping operation:
- for
- while
- do-while
1. FOR
Syntax:
for ([initialization] ; [condition] ; [counter])
{
statement(s);
}
Note:
# condition : the condition to keep going or to stop
e.g.: i < 10 , meaning that when i is no longer less than ten, it will not continue and go out from the operation
# counter : it can be increment, decrement or also any addition or subtraction of a variable
e.g.: i++ or ++i , both are the same meaning in this operation ; i+=2 , meaning i will be increased by two for every end of the operation
# [...] means it can be either filled or not
The sequence of for operation:
- Initialization
- Condition - if the condition false, directly exit the operation
- Statement(s)
- Counter
- Return to the beginning
# Infinite Loop: there are no stop condition
# Nested Loop: for inside for
Example:
2. WHILE
Syntax:
while( <boolean expression> )
{
statement(s);
}
Syntax:
while( <boolean expression> )
{
statement(s);
}
Note:
# boolean expression: expression that only have true or false value
The sequence of while operation:
- Boolean expression - if false then directly exit the operation
- Statement(s)
- Return to beginning
Example:
3. DO-WHILE
Syntax:
do
{
statement(s);
}
while ( <boolean expression> );
The sequence of do-while operation:
Example:
do
{
statement(s);
}
while ( <boolean expression> );
The sequence of do-while operation:
- Statement(s)
- Boolean expression - if false directly exit the operation
- Return to the beginning
Example:




Comments
Post a Comment