C while loop with Examples

C while loop with Examples

While loop in c programming; Through this tutorial, you will learn how to use while loop in C programming with examples.

While loop in C

  • While loop in c definition
  • 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 definition

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 
}

Explanation 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

See the following 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

Explanation of above 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

See the following 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 

Explanation above 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

See the following 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 

Explanation above 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.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *