Three Types of Loops in C Language

Three Types of Loops in C Language

# C Language Programming #

According to the basic knowledge we have learned in C language programming, it is not difficult to implement each step separately. However, since we often need to use this repetitive design structure (known as a loop structure), C language provides loop statements to simplify and standardize loop structure programming.

In C language, there are three types of loop statements: the for statement, the while statement, and the do-while statement.

Three Types of Loops in C Language

#01

For Statement

The general format of the for statement is:

for ( expression1; expression2; expression3 )

{

Loop body

}

Explanation:

Expression1 is called the “initialization expression” and is used to assign an initial value to the loop variable;

Expression2 is called the “loop control expression” and indicates the loop condition;

Expression3 is called the “increment expression” (if the increment is negative, it is the decrement expression) and is used to update the value of the loop variable;

The loop body represents the sequence of statements that need to be executed repeatedly; when there is only one statement in the loop body, the “{}” can be omitted.

Three Types of Loops in C Language

#02

While Statement

The general format of the while statement is:

while(expression)

{

Loop body

}

Explanation:

The “expression” specifies the loop condition, also known as the “loop control expression”.

Three Types of Loops in C Language

#03

Do-While Statement

The general format of the do-while statement is:

do

{

Loop body;

} while (expression);

Explanation:

The expression describes the loop condition.

Three Types of Loops in C Language
Three Types of Loops in C Language

Comparison of Three Loop Statements:

1. The characteristics of the while statement and for statement are “check first, execute later”, while the do-while statement is “execute first, check later”.

2. The while statement and do-while statement only specify the loop condition without clearly stating how the loop will end, so the loop variable should be initialized before entering the loop structure, and the loop body should contain statements that can change the loop variable’s value to ensure the program ends the loop normally.

3. The for statement assigns an initial value to the loop variable in expression1 and automatically executes expression3 after each execution of the loop body, updating the loop variable’s value in a timely manner, allowing the loop to end normally without manual control in the loop body.

4. The while statement can be seen as a special form of the for statement that lacks expression1 and expression3: for( ; expression2 ; ){ … }

5. The for, while, and do-while loop statements can replace each other.

—END—

Editor: Li Xinhui

Image source from the internet

Final Review: Liu Xinhui, Li Yuxin

Three Types of Loops in C Language

Scan to follow us

Leave a Comment