Programming in Practical Engineering
Languages
Chapter 3 : Loops in C language
BURKINA INSTITUTE OF TECHNOLOGY
Electrical Engineering
(E.E)
Academic year : 2024-2025
Semester 3
11 octobre 2024
Course outline
1 Loops generalities in programming language
2 Types of loops
3 For loop
4 While loop
5 Do-while loop
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 2 / 28
Quote
A programming language is a convention for giving commands to a
computer. It’s not supposed to be obscure, weird and full of
subtle traps...
Dave Small
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 3 / 28
1. Loops generalities in programming
language
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 4 / 28
1. Loops generalities in programming language
1.1. Introduction
Loops in programming are used to repeat a block of code until
the specified condition is met.
A loop statement allows programmers to execute a statement or
group of statements multiple times without repetition of
code.
Loops can execute a block of code as long as a specified
condition is reached.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 5 / 28
1. Loops generalities in programming language
1.2. Loops advantages
It provides code reusability.
Using loops, we do not need to write the same code again and
again.
Using loops, we can traverse over the elements of data
structures (array or linked lists).
Loops are handy because they save time, reduce errors, and
they make code more readable.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 6 / 28
2. Types of loops
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 7 / 28
2. Types of loops
2.1. Loops types (1/3)
There are mainly two types of loops in C Programming:
Entry Controlled loops: In these loops the test condition is
checked before entering the main body of the loop.
Example: For Loop and While Loop
Exit Controlled loops In these loops the test condition is
evaluated at the end of the loop body. The loop body will
execute at least once, irrespective of whether the condition
is true or false.
Example: do-while Loop
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 8 / 28
2. Types of loops
2.1. Loops types (2/3)
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 9 / 28
2. Types of loops
2.1. Loops types (3/3)
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 10 / 28
3. for loop
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 11 / 28
3. for loop
3.1. for loop: introduction
for loop in C programming is a repetition control structure
that allows to write a loop that will be executed a specific
number of times.
for loop enables to perform n number of steps together in a
single line.
When you know exactly how many times you want to loop through
a block of code, use the for loop.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 12 / 28
3. for loop
3.2. for loop: syntax
1 for ( initialization ; test ; update )
2 {
3 // code block to be executed
4 }
1 # include < stdio .h >
2 int main () {
3 int n = 9;
4 for ( int i = 0; i < n ; i ++) {
5 printf ( " Body of for loop which will execute till n " ) ;
6 }
7 return 0;
8 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 13 / 28
3. for loop
3.3. for loop: explanation (1/2)
In for loop, a loop variable is used to control the loop.
Firstly we initialize the loop variable with some value, then
check its test condition.
If the statement is true then control will move to the body
and the body of for loop will be executed.
Steps will be repeated till the exit condition becomes true.
If the test condition will be false then it will stop.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 14 / 28
3. for loop
3.3. for loop: explanation (2/2)
Initialization: we assign a loop variable or loop counter to
some value. for example: int i=0 ;
Test: test conditions are performed. If the condition
evaluates to true then the loop body will be executed and
then an update of the loop variable is done. If the test
expression becomes false then the control will exit from the
loop. for example, i<9 ;
Update: After execution of the loop body loop variable is
updated by some value it could be incremented, decremented,
multiplied, or divided by any value.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 15 / 28
3. for loop
3.4. for loop: flow
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 16 / 28
4. while loop
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 17 / 28
4. while loop
4.1. while loop: introduction
While loop does not depend upon the number of iterations.
In for loop the number of iterations was previously known to
us but in the While loop, the execution is terminated on the
basis of the test condition.
If the test condition will become false then it will break
from the while loop else body will be executed.
The while loop loops through a block of code as long as a
specified condition is true.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 18 / 28
4. while loop
4.2. while loop: syntax
1 while ( condition ) {
2 /*
3 simplified syntax 1
4 code block to be executed
5 */
6 }
1 initialization ;
2
3 while ( test ) {
4 /*
5 complete syntax 2
6 body of the while loop
7 */
8
9 update ;
10 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 19 / 28
4. while loop
4.3. while loop: example
1 # include < stdio .h >
2 int main () {
3
4 // initialization
5 int i = 0;
6
7 // test
8 while ( i < 5) {
9 printf ( " % d \ n " , i ) ;
10
11 // update
12 i ++;
13 }
14 return 0;
15 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 20 / 28
4. while loop
4.4. while loop: flow
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 21 / 28
5. do-while loop
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 22 / 28
5. do-while loop
5.1. Introduction
The do-while loop is a variant of the while loop.
This loop will execute the code block once, before checking
if the condition is true, then it will repeat the loop as
long as the condition is true.
In the do-while loop, the loop body will execute at least
once irrespective of the test condition.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 23 / 28
5. do-while loop
5.2. do-while loop: syntax
1 do {
2 // code block to be executed
3 }
4 while ( condition ) ;
1 initialization ;
2 do {
3 // body of do - while loop
4
5
6 update ;
7
8 } while ( test ) ;
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 24 / 28
5. do-while loop
5.3. do-while loop: example
1 # include < stdio .h >
2 int main () {
3
4 // initialization
5 int i = 5;
6
7 do {
8 // loop body
9 printf ( " Hello Bit World \ n " ) ;
10
11 // update
12 i ++;
13
14 // test
15 } while ( i < 1) ;
16
17 return 0;
18 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 25 / 28
5. do-while loop
5.4. do-while loop: flow
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 26 / 28
6. do-while loop
6.1. Loop control statements
Loop control statements in C programming are used to change
execution from its normal sequence.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 27 / 28
==END==
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 28 / 28