0% found this document useful (0 votes)
6 views

Loops

Uploaded by

ibrahim kori
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Loops

Uploaded by

ibrahim kori
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Content

 Objective
 Introduction
 Types of looping
while
do-while
for

 Assessment metric
 Conclusion
 References
What is Loop?

In computer science, a loop is a programming structure that


repeats a sequence of instructions until a specific condition is
met.
OBJECTIVES
Course Objective
 Understand the terminology used in
basic computer
programming
 It stresses the strengths of C, which provide students with
the means of writing efficient, maintainable, and portable
code.
 write, compile and debug programs in C language.
 Increase the ability to learn new programming languages
Topic Objective
 Understand the basics of looping.
 To use the while, do-while and for repetition statement to
execute statements in a program repeatedly.
INTRODUCTION
Statements in a program are executed one after the other
ex: statement 1;
statement 2;
:
statement n;

Sometimes, the user want to execute a set of statements


repeatedly.
Loop statements are used to repeat the execution
of statement or blocks.

Iteration of a loop: the number of times the body of loop is


executed.

 Two types of loop structure are:


Pretest : Entry - controlled loop
Posttest : Exit – controlled loop
Pretest Vs. Posttest
Pretest : Condition is tested before each iteration to check
if loops should occur.

Posttest : Condition is tested after each iteration to check


if loop should continue (at least a single iteration occurs).

Statements
Conditio
n
Evaluate
d
true
false
true Conditio
n
Statements Evaluate
d

false
TYPES OF
LOOP
 while loop
 do-while loop
 for loop
while Loop
It has a loop condition only that is tested before each
iteration to decide whether to continue or terminate the
loop.
The body of a while loop will execute zero or
more times
Syntax:
while (<condition>){
<statement/block>;
}
Example : Flow diagram

int i=0;
while(i<3){ Conditio
n
Evaluate
printf(“Hello\n”); d

false
true
i++;
Statements

}
Output
: Hello
Hello
Hello
do…while Loop
Do while has a loop condition only that is tested after
each iteration to decide whether to continue with next
iteration or terminate the loop.

Syntax:
do{
<statement/block>;
}while(condition);
Example:
int i=0; Flow diagram
do{
Printf (“Hello\n”);
i++;
Statements
} while (i<3);
true

Output:
Conditio
n
Evaluate
d

Hello false

Hello

Hello
for
Loop
for loop has three parts:

 Initializer is executed at start of loop.

 Loop condition is tested before iteration to


decide
whether to continue or terminate the loop.

 Increment is executed at the end of each loop iteration.


Syntax:
for( [initialize]; [condition]; [incrementor] )
{
<statement/block>;
}

Flow diagram
initialization

Conditio
n
Evaluate
d

true false
Statements

increament
Example:
for(i=0; i<3; i++)
{
printf(“Hello\n”);
}

Output:
Hello

Hello
ASSESSMENT METRIC

 What is looping? List the types of looping.

 Explain the while loop with an example.

 Give the difference between while and do-while loops

 Explain the syntax of for loop with an example

 List out the difference between while and for loop. And also
explain the do-while loop.
CONCLUSION
Importance of loops in any programming language is
immense, they allow us to reduce the number of lines in a
code, making our code more readable and efficient.

You might also like