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
Here, we will learn about while loop in C.
Syntax
while(Test_expression){ //statement(s); }
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
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
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java language
If statement in Python 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
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…