LOOPS 1
LOOPS 1
Overview:
Content
• Loops
• While Loop
Objectives:
• Explain the meaning of loops
• Explain the loop constructs in C
• Create a program with while loops
Each chapter in this module contains a major lesson involving the use of Flowchart and its purpose.
The units are characterized by continuity, and are arranged in such a manner that the present unit is
related to the next unit. For this reason, you are advised to read this module. After each unit, there are
exercises to be given. Submission of task given will be every Monday during your scheduled class hour.
Loops
You may encounter situations when a block of code needs to be executed several number of times. In
general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution
paths.
A loop statement allows us to execute a statement or group of statements multiple times
C programming language provides the following types of loops to handle looping requirements.
while Loop
A while loop in C programming repeatedly executes a target statement as long as a given condition is
true.
Syntax:
The syntax of a while loop in C programming language is:
while(condition)
{
} statement(s);
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any nonzero value. The loop iterates while the condition is true. When the
condition becomes false, the program control passes to the line immediately following the loop.
Example:
When the above code is compiled and executed, it produces the following result:
Example:
#include <stdio.h>
int main()
{
int i, n;
return 0;
}
Output:
int main()
{
int i, start;
printf("Enter starting value:
"); scanf("%d", &start); i =
start;
while(i>=1)
{
printf("%d\n", i);
i--;
}
return 0;
}
Output: