Understanding While loop in C

Understanding While loop in C

03 May 2024
Beginner
253 Views
6 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

While Loop: An Overview

The while Loop is an entry-controlled loop in C. This loop is used to iterate a part of the code while the given condition remains true. The test takes place before each iteration.

In this C Tutorial, we will explore more about while Loop which will include what is for loop in C, for loop in c programming example, and the syntax of for loop in C. First, we will see What are types of loops in C.

Read More - Top 50 C Interview Questions and Answers

Types of Loop in C

Let’s get into the three types of loops used in C programming.
  1. for loop
  2. while loop
  3. do while loop

But right now we are going to focus on only While Loop, So let's discuss it.

What is a While Loop?

  • While loop is nothing but a a pre-tested loop. 
  • It allows a part of the code to be executed n number of times depending upon a given boolean condition. 
  • The while loop is viewed as a repeating if statement. 
  • It is merely used in the case where the number of iterations is not known in advance.

Flowchart 

While Loop in C

Syntax

while(test condition){ 
//code to be executed 
} 
  • The test condition is checked, if false the loop will be skipped and the program will continue
  • If the test condition is true, the body of the loop executes
  • The update expression gets updated
  • Again the test condition is evaluated
  • The process repeats until the test condition becomes true.

Example: While loop in the CCompiler

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

Explanation:

  • In the above code, i is initialized to 1 before the start of the loop.
  • The test condition, i<=10 is evaluated. If true, the body of the loop executes.
  • If the condition becomes false in the beginning itself, the program control does not even enter the loop once.
  • The loop executes until i becomes 10.

Output

1
2
3
4
5
6
7
8
9
10

Infinite while loop:

  • In this loop test condition is incorrect.
  • And the updation statement is not present.

Example:

#include <stdio.h>

#include <stdlib.h>
 
int main()
{
    int Sch1= 1;
    int Sch2= 1;
 
    while (Sch1< 10) {
 
        Sch2 = Sch2+ 1;
        printf("Welcome to ScholarHat");
    }
    
    return 0;
}

Output

Welcome to ScholarHat
Welcome to ScholarHat
Welcome to ScholarHat
Conclusion:
So we explored the while loop in c, what is while loop in c, while loop in c programming example, and the syntax of the while loop in c.The While Loop in C has a broad range of applications, from loop-driven algorithms to iterative problem-solving. Take your proficiency in C programming to the next level by pursuing a C Programming Course, which will validate your expertise in loops and other important concepts.

FAQs

Q1. What is difference between for loop and while loop?

The for loop is used when the number of iterations is already known, However, execution is done in a while loop until the statement in the program is proved False.

Q2. What is in a while loop?

It repeats a section of code an unknown number of times until a specific condition is met.

Q3. What is loop in C?

It is used to iterate the statements or a part of the program several times.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Full Stack .Net Certification Training May 26 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training May 26 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training May 26 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Azure DevOps Certification Training Course Jun 01 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Data Structures and Algorithms Training Jun 04 TUE, FRI
Filling Fast
08:30PM to 10:00PM (IST)
Get Details
ASP.NET Core Certification Training Jun 14 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
ASP.NET Core (Project) Jun 22 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this