Menu Close

Articles

C: Repetitive statement

A repetitive statement is a block of code or a statement that can be repeated several times to perform one or more tasks. We must control the number of repetitions otherwise the program will run forever. Therefore repetitive statement is sometimes called “loop”.

There are 3 kind of repetitive statements in C:

  1. while: condition repetition
  2. do: post-conditional repetition
  3. for: value controlled repetition

The difference between these loops is the way we control the number of iterations. Next I will present 3 examples. You must read the comments to understand better the syntax:

While

The while loop will be executed as long as control condition is true and will stop when the condition become false. Remember true = 1 and false = 0. So the condition can be a logical or a numeric expression. If the condition never = 0 then we create an infinite loop.

Do

This loop will look very strange to you. I is designed very different than other C statements and somehow inconsistent. So pay attention to the example and read the comments below to grasp it.

The condition is specified after the block. Therefore this iteration is executed at least once regardless of condition value, unlike the previous case where the condition is evaluated before the first iteration.

For

For statement is controlled by a local variable x that belong to a specified discrete range of values. In next example first value is  5 and last value is 9. Value 10 is not printed.

Note: This repetitive statement is a convenient way to have all the control into one place for easy visualization. So the for loop does the same thing as while and do except that is more compact and readable. Variable “x” is local defined but it could be declared before the loop starts. In this case it will be available after the loop end like it is for “do” and “while”.

Alternative: There are other two ways to repeat execution of statements or blocks in C:

  1. Using GoTo statement
  2. Using recursive functions

Early exit

It is possible to force a loop to terminate before normal. This is called early exit. Usually the exit is trigger by an additional condition using statement: break

Example:

Shortcut

Sometimes it is useful to skip the rest of the block and start next iteration. This operation can simplify the code. To make a shortcut you can use statement: continue

Observe the new operator “++” that was first used in B language. This operator is equivalent to: x = x +1. It is used as increment operator to improve performance and code readability. In modern languages this is called “syntax sugar” and is considered a good thing. 

Notes:

  • You can use break and continue with all 3 kind of loops,
  • You can define a loop nested inside another loop,
  • You can use goto statement to break or continue nested loops.

Bad practice

It is possible to declare a variable inside a loop. Then the variable is not available outside of the loop. However in my opinion this is bad practice. The variable declarations should stay outside of the loop. In the next example the result is going to be unexpected:

Output:

Note: If you declare the variable static inside the loop then the result will be correct but still a bad practice. There is no visible advantage to declare a variable inside a loop, so don’t do this.

Next article: Functions & Pointers