0% found this document useful (0 votes)
38 views4 pages

Practical Slot 4

The document discusses while loops in C programming. It explains that while loops are entry-controlled loops that check a condition before executing the loop body. An example while loop is provided that uses a counter variable to print the numbers 1 through 10. The loop initializes the counter at 1, checks if it is less than or equal to 10, prints the counter value, increments the counter, and repeats until the counter exceeds 10.

Uploaded by

627Gautam Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Practical Slot 4

The document discusses while loops in C programming. It explains that while loops are entry-controlled loops that check a condition before executing the loop body. An example while loop is provided that uses a counter variable to print the numbers 1 through 10. The loop initializes the counter at 1, checks if it is less than or equal to 10, prints the counter value, increments the counter, and repeats until the counter exceeds 10.

Uploaded by

627Gautam Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical Slot – 4: Fundamentals of Loop Control Structures.

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

Depending upon the position of a control statement in a program, looping


statement in C is classified into two types:

1. Entry controlled loop

2. Exit controlled loop

In an entry control loop in C, a condition is checked before executing the body of


a loop. It is also called as a pre-checking loop.

In an exit controlled loop, a condition is checked after executing the body of a


loop. It is also called as a post-checking loop.

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:

1. No termination condition is specified.

2. The specified conditions never meet.

The specified condition determines whether to execute the loop body or not.
'C' programming language provides us with three types of loop constructs:

1. The while loop

2. The do-while loop

3. The for loop

While Loop in C

A while loop is the most straightforward looping structure. While loop syntax in C
programming language is as follows:

Syntax of While Loop in C:


While (condition) {
statements;
}

It is an entry-controlled loop. In while loop, a condition is evaluated before


processing a body of the loop. If a condition is true then and only then the body of
a loop is executed. After the body of a loop is executed then control again goes
back at the beginning, and the condition is checked if it is true, the same process
is executed until the condition becomes false. Once the condition becomes false,
the control goes out of the loop.

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

...Program finished with exit code 0

Press ENTER to exit console.

Result:- We get know how to use while loop.

You might also like