LOOPING IN C-
PROGRAMMING
PRESENTED BY :
{
AFANJI PHILL MUKETE
BUWAGA RYAN ARREY
NWANKO FORGIVE
FUHNWIE HILLSON NGWASIRI
BALALA OLIVIER
1
OUTLINE
At the end this presentation, you should be able to:
Define a looping as concern C-programming
Why we loop in C-programming
Advantages of looping
Types of C loops
Essential components of a loop
Discuss on While loop and examples
Discuss on Do loop and examples
Discuss on the For loop and examples
Draw flowchart for the various types of looping in
C-programming
2
WHAT IS LOOPING IN C-
PROGRAMMING?
The looping can be defined as repeating the same
process multiple times until a specific condition
Satisfies or is true. The sequence of statements to be
executed is kept inside the curly bracket { }known as
loop body , condition is verified, and if it is found to
be true the loop body is executed again. And when
the condition check false, the loop body is not
executed
3
Why we loop in C-Programming ?
Looping simplifies the complex problems into the
easy ones.
Looping enables to alter the flow of the program so
that instead of writing the same code again and
again, we can execute the same code for a finite
number of times.
For example, if we need to print “UNIVERSITY OF
FOMIC POLYTECHNIC” 15-times then, we can use
the Printf once inside a loop which runs up to 15
iterations.
4
Advantages of looping in C-
Programming
It provides codes reusability.
Using loops we do not need to write the same
code again and again.
Using loops, we can access each elements
stored in the array so that the data can be
checked or used as part of the process. data
structure (array is a series of memory location
which holds a single item of data).
5
Types of C Loops
There are three types of loops in C language those
are given below;
While
Do while
For
6
Essential components of a loop
Counter
Initialization of the counter with initial value
Condition to check with the optimum value of the
counter
Statement(s) to be executed by iteration
Increment/decrement
7
While loop in C
The while loop in c is to be used in the scenario where
the block of statements is executed in the while loop until
the condition specified in the while loop is satisfied. It is also
called a pre-tested loop.
The syntax of while loop in c language is given below:
initialization;
While(condition)
{
block of the statements to be executed ;
increment;
8
}
Do-while loop in C
The do-while loop continues until a given condition
satisfies. It is also called post tested loop. It is used when it is
necessary to execute the loop at least once(mostly menu
driven programs).
The syntax of do-while loop in c language is given below;
do
{
code to be executed ;
}
9 While(condition);
For loop in C
• The for loop in C language is used to iterate the
statements or a part of the program several times. It
is frequently used to traverse the data structures like
the array and linked list.
• The syntax of for loop in c language is given below:
for(expression 1; Expression 2; Expression 3)
{
codes to be executed;
}
10
Expression 1 (Optional)
• Represents the initialization of the loop variable.
• More than one variable can be initialized.
Expression 2
• Expression 2 is a conditional expression. It checks for a
specific condition to be satisfied. If it is not, the loop is
terminated.
• Expression 2 can have more than one condition. However,
the loop will iterate until the last condition becomes false .
Other conditions will be treated as statements.
Expression 3
• Expression 3 is increment or decrement to update
the value
11 of the loop variable
Flowchart : For the or loop
12
Example of While loop print even numbers
1. //print even numbers
2. #include<stdio.h>
3. int main ( )
4. {
5. int n, i=0;
6. printf (“/n Enter a number : “) ;
{
7. scanf(“ % d” ,&n);
8. while ( i < = n )
9. {
10. if ( i % 2= =0)
11. printf (“/n%d” , i ) ;
12. i + +;
13. }
14. return 0
13
Example of a DO-while loop
14
Example of a For Loop in C
15
16