while loop in C

Get Prepinsta Prime

Get all 200+ courses offered by Prepinsta

Never Miss an OffCampus Update

Get OffCampus Updates on Social Media from PrepInsta

Follow us on our Media Handles, we post out OffCampus drives on our Instagram, Telegram, Discord, Whatsdapp etc.

Get Hiring Updates
Amazon,Google,Delottie & 30+companies are hiring ! Get hiring Updates right in your inbox from PrepInsta

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get PrepInsta Prime Subscription

Get access to all the courses that PrepInsta offers, check the out below -

Companies

TCS, Cognizant, Delloite, Infosys, Wipro, CoCubes, KPMG, Amazone, ZS Associates, Accenture, Congnizant & other 50+ companies

Programming

Data Structures, Top 500 Codes, C, C++, Java Python & other 10+ subjects

Skills

Full Stack Web Development, Data Science, Machine Learning, AWS Cloud, & other 10+ skills and 20+ projects

OffCampus Updates

Never Miss OffCampus Updates

Get OffCampus Update from PrepInsta

Follow us on our Social Media Handles

Introduction of while loop in C

while loop is defined as the conditional statement which allows to run the blocks of code in a defined number or infinite number of times.

Basically the while loop is used to run the run in repetitive times until the condition is true.

While loop in C

Syntax:

while(condition)
{
// initialization blocks of Code // updation }

Example 1

Run

#include <stdio.h>
int main()
{ int i=1; //intialization
while(i<=5)
{ printf("PrepInsta\n"); i++; //updation }
return 0; }

Output:

PrepInsta
PrepInsta
PrepInsta
PrepInsta
PrepInsta

Working of while loop

Initialization:

  • It is the starting statement of the while loop which accepts the initialization of expression.

Conditional Statement:

  • A conditional statement determines whether the block of code will be executed or not. 
  • When the condition is satisfied, the loop will end. It will continue to test the condition and check until the block of code under the while loop is satisfied.

Body:

  • It is the main part of the code which needs to be executed in every iteration for performing the given condition.
  • It refers to a collection of statements which includes variable functions etc.

Updation:

  • It is the statement of the while loop which includes the increment and decrement expression according to the conditional statement.

Example 2

Run

#include <stdio.h>
int main() { int i = 1; //initialization
while (i <= 10) { printf("%d\n", i); i++; //updation }
return 0; }

Output:

1
2
3
4
5
6
7
8
9
10

Comments