BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C – while loop in C programming with example

Last Updated: September 23, 2017 by Chaitanya Singh | Filed Under: c-programming

A loop is used for executing a block of statements repeatedly until a given condition returns false. In the previous tutorial we learned for loop. In this guide we will learn while loop in C.

C – while loop

Syntax of while loop:

while (condition test)
{
      //Statements to be executed repeatedly 
      // Increment (++) or Decrement (--) Operation
}

Flow Diagram of while loop

C while loop

Example of while loop

#include <stdio.h>
int main()
{
   int count=1;
   while (count <= 4)
   {
	printf("%d ", count);
	count++;
   }
   return 0;
}

Output:

1 2 3 4

step1: The variable count is initialized with value 1 and then it has been tested for the condition.
step2: If the condition returns true then the statements inside the body of while loop are executed else control comes out of the loop.
step3: The value of count is incremented using ++ operator then it has been tested again for the loop condition.

Guess the output of this while loop

#include <stdio.h>
int main()
{
     int var=1;
     while (var <=2)
     {
        printf("%d ", var);
     }
}

The program is an example of infinite while loop. Since the value of the variable var is same (there is no ++ or – operator used on this variable, inside the body of loop) the condition var<=2 will be true forever and the loop would never terminate.

Examples of infinite while loop

Example 1:

#include <stdio.h>
int main()
{
     int var = 6;
     while (var >=5)
     {
        printf("%d", var);
        var++;
     }
   return 0;
}

Infinite loop: var will always have value >=5 so the loop would never end.

Example 2:

#include <stdio.h>
int main()
{
    int var =5;
    while (var <=10)
    {
       printf("%d", var);
       var--;
    }
    return 0;
}

Infinite loop: var value will keep decreasing because of –- operator, hence it will always be <= 10.

Use of Logical operators in while loop

Just like relational operators (<, >, >=, <=, ! =, ==), we can also use logical operators in while loop. The following scenarios are valid :

while(num1<=10 && num2<=10)

-using AND(&&) operator, which means both the conditions should be true.

while(num1<=10||num2<=10)

– OR(||) operator, this loop will run until both conditions return false.

while(num1!=num2 &&num1 <=num2)

– Here we are using two logical operators NOT (!) and AND(&&).

while(num1!=10 ||num2>=num1)

Example of while loop using logical operator

In this example we are testing multiple conditions using logical operator inside while loop.

#include <stdio.h>
int main()
{
   int i=1, j=1;
   while (i <= 4 || j <= 3)
   {
	printf("%d %d\n",i, j);
	i++;
	j++;
   }
   return 0;
}

Output:

1 1
2 2
3 3
4 4
❮ PreviousNext ❯

Top Related Articles:

  1. Conditional (Ternary) Operator in C with Examples
  2. Arithmetic Operators in C with Examples
  3. C – Pointer to Pointer (Double Pointer) with example
  4. C Program Structure – First C Program
  5. Assignment Operators in C with Examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Muhammad Rehan says

    November 1, 2015 at 7:05 PM

    How any language is created?
    Is it created in Low level language like Machine Language (Binary or OS,DOS) or SOMETHING else?????????

    Reply
    • Waqas says

      August 1, 2021 at 9:11 AM

      It is written in high level language c c++ gave cobol pascal python etc all are high level language low level lengage are machine language understand only by machine and in the form of binary number 0 or 1
      Thanks

      Reply
  2. Elham says

    December 5, 2015 at 8:57 PM

    Hi
    please write an axamplee with both while and if
    tnx

    Reply
    • shreyansh says

      January 22, 2016 at 6:38 AM

      if statement is use to define condition , if condition holds true the statement will be executed otherwise not.
      e.g.
      if(age>18)
      {
      printf(“you can vote”);
      }

      on the other hand while statement is being used for loop operation for example
      printing numbers form 1 to 10.
      i=1;
      while(i<=10)
      {
      printf("%d",i);
      i++
      }

      Reply
  3. Yadav_S says

    April 4, 2017 at 12:25 PM

    Can we use while continue break and for in one program can you give an example?

    Reply
  4. sajal sharma says

    September 9, 2018 at 6:18 AM

    your explanation is terrific . I am sure that any beginner will definitely learn easily from your website.
    I have doubt regarding while loop and my question is

    CAN we use COMMA( , ) in while loop
    ex:
    while( i>5 , j>4 )

    Reply
    • NotNick says

      January 16, 2022 at 3:09 PM

      Yes, You can use commas.

      Reply
  5. Sakshi says

    February 10, 2022 at 4:13 PM

    Plz tell me how to use of the only while loop and logical and to display star pattern left angle triangle up to 5 .

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap