Home Show/Hide Menu
C Tutorials
➜ C Language
  - Features of C
  - First C Program
  - Compilation in C
  - C Character Set
  - Tokens
  - Keywords
  - Identifiers
  - Constants
  - Operators
  - Data Types
  - Variables
  - Type Casting
  - Comments
  - Input/Output
  - Escape Sequence
  - Programming Errors
➜ Control Statements
  - if Statement
  - if-else Statement
  - Nested if-else
  - else-if
  - switch Statement
  - Loop
  - while loop
  - do while loop
  - for loop
  - break
  - continue & goto
  - Nested Loop
  - Infinite Loop
➜ Functions
  - What is function
  - Call by Value & Reference
  - Recursive function
  - Storage Classes
➜ Array
  - 1-D Array
  - 2-D Array
  - Return an Array
  - Array to Function
➜ C Pointers
  - Pointers
  - Pointer to Pointer
  - Pointer Arithmetic
  - Dangling Pointers
  - sizeof() operator
  - const Pointer
  - void pointer
  - Dereference Pointer
  - Null Pointer
  - Function Pointer
  - Function pointer as argument
➜ Memory Management
  - Memory Layout of C
  - Dynamic memory
  - calloc()
  - malloc()
  - realloc()
  - free()
➜ Strings
  - gets() & puts()
  - String Functions
  - strlen()
  - strcpy()
  - strcat()
  - strcmp()
  - strrev()
  - strlwr()
  - strupr()
  - strstr()
➜ C Math
  - Math Functions
➜ Enum, Structure & Union
  - Enum
  - Structure
  - typedef
  - Array of Structures
  - Nested Structure
  - Structure Padding
  - Union
➜ File Handling
  - fprintf() fscanf()
  - fputc() fgetc()
  - fputs() fgets()
  - fseek()
  - EOF and feof()
  - fsetpos()
  - tmpfile()
  - rewind()
  - ftell()
  - lseek()
➜ Preprocessor
  - Preprocessor
  - Macros
  - #include
  - #define
  - #undef
  - #ifdef
  - #ifndef
  - #if
  - #else
  - #error
  - #pragma
➜ Command Line
  - Arguments

while loop in C Language

In C programming language, while loop is used to repeatedly run a block of statements until the control condition returns the result as false. The condition of the while loop is checked before the body of the loop is executed, hence it is called a pre-tested loop or entry-controlled loop.

Essential components required for a loop

  • In while loop, a expression is used to check the condition. The block of statements defined inside the while loop will execute repeatedly until the given condition returns false.
  • The condition will return 0, if it is true. The condition will return any non-zero number, if it is false.
  • In while loop, the conditional expression is mandatory and there can have more than one conditional expressions.
  • If while loop contains single statement, then the braces are optional.
  • A while loop may run without any statement. E.g. while(condition);
Syntax of while loop::-
while(condition)
{
       //Statement(s) to be executed repeatedly
}

// Program to Print Multiples of a Given Number
#include<stdio.h>
#include<conio.h>
void main()
{
	int no, mul, i=1;
	printf("Enter a number : ");
	scanf("%d",&no);
	while(i<=10)
	{
		mul=i*no;
		printf("\n%d X %d = %d",&i,&no, &mul);
		i++;
	}
	getch();
}

Output :

Enter a number : 5
1 X 5 = 5
2 X 5 = 10
3 X 5 = 15
4 X 5 = 20
5 X 5 = 25
6 X 5 = 30
7 X 5 = 35
8 X 5 = 40
9 X 5 = 45
10 X 5 = 50

Flow Chart of while Loop

ADVERTISEMENT