Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

while loop in C ❮ Edit Details

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance.

Syntax of while loop in C language

The syntax of while loop in c language is given below:

  1. while(condition){  
  2. //code to be executed  
  3. }  

Flowchart of while loop in C

flowchart of c while loop


Example of the while loop in C language

Let's see the simple program of while loop that prints table of 1.

  1. #include<stdio.h>  
  2. int main(){    
  3. int i=1;      
  4. while(i<=10){      
  5. printf("%d \n",i);      
  6. i++;      
  7. }  
  8. return 0;  
  9. }    

Output

1
2
3
4
5
6
7
8
9
10

Program to print table for the given number using while loop in C

  1. #include<stdio.h>  
  2. int main(){    
  3. int i=1,number=0,b=9;    
  4. printf("Enter a number: ");    
  5. scanf("%d",&number);    
  6. while(i<=10){    
  7. printf("%d \n",(number*i));    
  8. i++;    
  9. }    
  10. return 0;  
  11. }   

Output

Enter a number: 50
50
100
150
200
250
300
350
400
450
500
Enter a number: 100
100
200
300
400
500
600
700
800
900
1000

Properties of while loop

  • A conditional expression is used to check the condition. The statements defined inside the while loop will repeatedly execute until the given condition fails.
  • The condition will be true if it returns 0. The condition will be false if it returns any non-zero number.
  • In while loop, the condition expression is compulsory.
  • Running a while loop without a body is possible.
  • We can have more than one conditional expression in while loop.
  • If the loop body contains only one statement, then the braces are optional.

Example 1

  1. #include<stdio.h>  
  2. void main ()  
  3. {  
  4.     int j = 1;  
  5.     while(j+=2,j<=10)  
  6.     {  
  7.         printf("%d ",j);   
  8.     }  
  9.     printf("%d",j);  
  10. }  

Output

3 5 7 9 11

Example 2

  1. #include<stdio.h>  
  2. void main ()  
  3. {  
  4.     while()  
  5.     {  
  6.         printf("hello Javatpoint");   
  7.     }  
  8. }  

Output

compile time error: while loop can't be empty	

Example 3

  1. #include<stdio.h>  
  2. void main ()  
  3. {  
  4.     int x = 10, y = 2;  
  5.     while(x+y-1)  
  6.     {  
  7.         printf("%d %d",x--,y--);  
  8.     }  
  9. }  

Output

infinite loop 

Infinitive while loop in C

If the expression passed in while loop results in any non-zero value then the loop will run the infinite number of times.

  1. while(1){  
  2. //statement  
  3. }