While loop

While loop in C programming language

While loop in C programming language

In this tutorial, We will discuss While loop in C programming language.

Typically, in programming languages, looping statements are used to perform repetitive statements until a specific condition is met in a block of code.

C program having three basic looping statements

  • for loop  in C
  • while loop in C
  • do-while loop in C

Here, we will learn about while loop in C.

While loop in Python

The syntax of While loop in C

Syntax

while(Test_expression){

//statement(s);

}

flow diagram of while loop

Wile loop in C

How while loop works in C language:

While loop in C programming language is used to execute a block of statements repeatedly until a given condition returns false. It is similar to for loop in C.

Initially, The initialization statements execute only once. text expression is evaluated by while loop.  If the test expression is true(Boolean expression makes only two output 1 or 0) flow of control enters inside the body to execute the code. Continuously, test expression is evaluated and executed until the test expression is false.

This process repeats until the test expression is false.

When the test expression is false. while loop stops the execution and flow of control immediately exits from the loop.

Example

program 1

Here, it is a simple example to display 1 to 10

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=1;//variable declaration
    //and initialization
    while(a<=10){//test expression
    printf("value of a :%d\n",a);
    //display statements
    a++;//increment statements
    }
    getch();
    return 0;
}

 

When the above code is compiled and executed, it produces the following result

value of a :1
value of a :2
value of a :3
value of a :4
value of a :5
value of a :6
value of a :7
value of a :8
value of a :9
value of a :10

the above program displays the positive numbers from 1 to 10 using while loop

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=1;
    while(i<=10){
    printf("%d\n",i*i);
    i++;
    }
    getch();
    return 0;
}

 

When the above code is compiled and executed, it produces the following result:

Square number of between 1 to 100
1
4
9
16
25
36
49
64
81
100
End the program

The above program displays square numbers between 1 to 100 using while loop

Program 3

Find factorial using while loop in C language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    long fact;
    printf("Enter the number for find factorials :");
    scanf("%d",&num);
    fact=1;
    while(num>0) //if num>0, loop is terminated
    {
        fact*=num;  //factorial=factorial*number
        --num;
    }
    printf("Factorial is := %ld", fact);
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following result:

Enter the number for find factorials: 6

Factorial is: 720

the above program  calculates factorial of given numbers using the while loop

The infinite while loop in C

while(true){ //it is always true
printf("This loop would never end");
}

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=5;
    while(i>=5){ //i value greater than initialized value
    printf("%d",i);
    i++;
    }
    return 0;
}

When the above code is compiled and executed, it produces the following result:

This loop would never end

 

While loop with an array in C language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int arr[]={45,67,75,34,65,89};
    int i=0;
    while(i<=5){
    printf("%d\n",arr[i]);
    i++;
    }
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following result:

45
67
75
34
65
89

the above program  displays elements of the array using the while loop

There are other Java language keywords that are similar to this post

Java While keyword

 

Know more about Array

Array in Programming languages

Similar post

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

 

 

Suggested for you

If statement in Java language

If statement in C++ language

If statement in C language

If statement in Python language

Nested if in Java language

Nested if in C language

Nested if in C++ language

Nested if in Python language

Two dim Array in C++ language

Three dim Array in C++ language

Single dim Array in Java language

Two dim Array in Java language

Three dim Array in Java language

Single dim Array in C language

Two dim Array in C language

Three dim Array in C language

The while keyword in Java programming language
While loop in Python programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago