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.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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