In this article while loop in C we give the information about while is an entry-controlled loop statement. The test-condition is evaluated and if the condition is true, the main part of the loop is executed.
While loop in C
Looping Statements in C:
Introduction
In C programming, a loop is a sequence of instructions that is repeated continuously until a certain condition is reached. That is, loops are used to repeat a block of code.
Being able to have a program repeatedly execute a block of code is one of the most basic yet useful tasks in programming.
The looping process, in general, involves the following four steps:
1. Setting and initialization of condition variables.
2. Execution of statements in the loop.
3. Test for the specified value of the condition variable to operate the loop.
4. Increasing or updating the condition variable.
The test can be either to determine if the specified time loop has been repeated or to determine if a specific condition has been met.
The C language provides three structures for performing loop operations.
1. While Loop
2. Do…While Loop
3. For…Loop
The While statement is the simplest of all the looping structures in C. We have used while in many of our earlier programs.
While loop flowchart in c:
While Loop Syntax in C:
while (test condition)
{
Body of the loop.
}
While is an entry-controlled loop statement. The test-condition is evaluated and if the condition is true, the main part of the loop is executed. After the body is executed, the test-condition is re-evaluated and if it is true, the body is re-executed.
This process of body repetition continues until the test-situation is finally false and transferred out of the control loop. After exiting, the program continues with the statement following the main part of the loop.
The main part of the loop can contain one or more statements. Braces are only required if there are two or more statements in the body. However, it is better to use braces even if there is only one statement in the body.
// Display Sum of First 10 Natural Numbers
void main()
{
int p=1, n_sum=0;
while(p<=10)
{
n_sum=n_sum+p;
p++;
}
printf(“\n Display 1st ten natural number sum = %d”,n_sum);
}
O/P:-
Display 1st ten natural number sum = 55
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining