Practical Slot 4
Practical Slot 4
Practical 1.1
Aim:- To demonstrate use of while loop.
Theory:- Looping Statements in C execute the sequence of statements many
times until the stated condition becomes false. A loop in C consists of two parts, a
body of a loop and a control statement. The control statement is a combination of
some conditions that direct the body of the loop to execute until the specified
condition becomes false. The purpose of the C loop is to repeat the same code a
number of times.
Types of Loops in C
The control conditions must be well defined and specified otherwise the loop will
execute an infinite number of times. The loop that does not stop executing and
processes the statements number of times is called as an infinite loop. An infinite
loop is also called as an "Endless loop." Following are some characteristics of an
infinite loop:
The specified condition determines whether to execute the loop body or not.
'C' programming language provides us with three types of loop constructs:
While Loop in C
A while loop is the most straightforward looping structure. While loop syntax in C
programming language is as follows:
After exiting the loop, the control goes to the statements which are immediately
after the loop. The body of a loop can contain more than one statement. If it
contains only one statement, then the curly braces are not compulsory. It is a
good practice though to use the curly braces even we have a single statement in
the body.
In while loop, if the condition is not true, then the body of a loop will not be
executed, not even once. It is different in do while loop which we will see shortly.
Algorithm:- Step 1: The variable count is initialized with value 1 and then it has
been tested for the condition.
Step 2: If the condition returns true then the statements inside the body of while
loop are executed else control comes out of the loop.
Step 3: The value of count is incremented using ++ operator then it has been
tested again for the loop condition.
Flowchart:-
Program:- #include<stdio.h>
int main(){
int i=1;
while(i<=10)
printf("%d ",i);
i++;
return 0;
Output:- 1 2 3 4 5 6 7 8 9 10