While loop in C programming language
- Home
- Loop
- While loop
- While loop in C programming language
- On
- By
- 0 Comment
- Categories: 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.
The syntax of While loop in C
Syntax
while(Test_expression){ //statement(s); }
flow diagram of while loop
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
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java language
Suggested for you
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