While Loop in C Programming with Examples

While loop in c programming; In this tutorial, i am going to show you what is while loop in c programming and how to use while loop in C programming with examples.

While Loop in C Programming with Examples

Here, i will teach you the list of things about while loop in c programming with examples; is as follows:

  • While loop in c
  • Syntax of while loop in c
  • Example 1 – C program to print 1 to 10 numbers using while loop
  • Example 2 – C program to print even numbers from 1 to 10 using while loop
  • Example 3 – C program to print odd numbers from 1 to 10 using while loop

While loop in c

In while loop, a condition is evaluated before processing a body of the loop. If a condition is met then and only then the body of a loop is executed. And It is also known as an entry-controlled loop.

Syntax of while loop in c

The syntax of while loop in c; as shown below:

while (testExpression) {
  // the body of the loop 
}

Illustration of above given c while loop syntax

  • The while loop test the testExpression inside the parentheses ().
  • If testExpression is true, statements inside the body of while loop are executed. Then, testExpression is evaluated again.
  • The process goes on until testExpression is evaluated to false.
  • If testExpression is false, the loop terminates (ends).

Example 1 – C program to print 1 to 10 numbers using while loop

Let’s create c program to print 1 to 10 number using while loop; as shown below:

// Print numbers from 1 to 10
#include <stdio.h>
int main() {
  int i = 1;
    
  while (i <= 10) {
    printf("%d\n", i);
    ++i;
  }
  return 0;
}

Output

1
2
3
4
5
6
7
8
9
10

Illustration of above given c program to print 1 to 10 number using while loop

  • Initialized i variable with value 1.
  • If the condition returns true then the statements inside the body of while loop are executed else control comes out of the loop.
  • This process goes on until i becomes 10. Then, the test expression i <= 10 will be false and the loop terminates.

Example 2 – C program to print even numbers from 1 to 10 using while loop

Let’s create c program to print even numbers from 1 to 10 using while loop; as shown below:

#include <stdio.h>
int main()
{
	//assign initial value 
	//from where we want to print the numbers
	int i = 1;
	//variable to store limit /N
	int n = 10;
	//print statement
	printf("Even Numbers from 1 to 10 \n");
	//while loop, that will print numbers 
	while(i <= n)
	{
		//Here is the condition to check EVEN number
		if(i%2==0)
			printf("%d ",i);
		
		// increasing loop counter by 1
		i++;
	}
	return 0;
}

Output

Even Numbers from 1 to 10:
2 4 6 8 10 

Illustration of above given C program to print even numbers from 1 to 10 using while loop

  • Initialized i variable with value 1.
  • Initialized n variable with value 10.
  • Iterate body of while loop until the condition met true else terminate while loop exection.
  • Inside while loop body; use i%2 ==0 test condition to find even number from 1 to n and print it.

Example 3 – C program to print odd numbers from 1 to 10 using while loop

Let’s create c program to print odd numbers from 1 to 10 using while loop; as shown below:

#include <stdio.h>
int main()
{
	//assign initial value 
	//from where we want to print the numbers
	int i = 1;
	//variable to store limit /N
	int n = 10;
	//print statement
	printf("Odd Numbers from 1 to 10 \n");
	//while loop, that will print numbers 
	while(i <= n)
	{
		//Here is the condition to check ODD number
		if(i%2!=0)
			printf("%d ",i);
		
		// increasing loop counter by 1
		i++;
	}
	return 0;
}

Output

Odd Numbers from 1 to 10 
1 3 5 7 9 

Illustration of above given C program to print odd numbers from 1 to 10 using while loop

  • Initialized i variable with value 1.
  • Initialized n variable with value 10.
  • Iterate body of while loop until the condition met true else terminate while loop exection.
  • Inside while loop body; use i%2 !=0 test condition to find odd number from 1 to n and print it.

More C Programming Tutorials

Leave a Comment