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:
  1. Initialization
  2. Condition - if the condition false, directly exit the operation
  3. Statement(s)
  4. Counter
  5. 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);

}

Note:
# boolean expression: expression that only have true or false value

The sequence of while operation:

  1. Boolean expression - if false then directly exit the operation
  2. Statement(s)
  3. Return to beginning
Example:




3. DO-WHILE

Syntax:
do
{
statement(s);
}

while ( <boolean expression> );

The sequence of do-while operation:

  1. Statement(s)
  2. Boolean expression - if false directly exit the operation
  3. Return to the beginning

Example:




Comments

Popular Posts