×

While Loop Syntax in C

What is Loop?

The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make up a loop in C. The control statement is a collection of conditions that tells the loop's body to keep running until a certain condition is met.

While Loop:

The while loop is an entry-controlled loop. Entry controlled loop means before starting the loop's body, the condition is verified. In other words, the condition won't ever execute if it is false.

Most repetitive operations are carried out using the while loop. It doesn't have initialization or update statements as a for loop does.

It requires three stages to accomplish.

  • Initialization of a variable
e.g.   int a=1;
  • condition
e.g. while(a<=6);
  • Variable increment or decrement
e.g. a++ or ++a , a-- or a=a-1;

while loop Syntax:

Initialization of a variable ;
while(condition)
{
//executed code
Statements;
Increment or decrement of a variable;
}

While loop Syntax Description:

  • Variable initialization:

Before starting the while loop, first of all, we initialize a variable with some value.

  • Condition:

Before the while loop's code block, the condition expression is assessed. To determine whether to continue the while loop or end it, a boolean expression is used. If the condition expression evaluates to true, the code block inside the while loop run; otherwise, the while loop gets terminated, and control is transferred to the following statement.

  • Statements:

Statements can be included in a while loop in any number, even zero. In C programming, curly braces are not required for a single statement contained within a while loop. The compiler will only execute the first statement if they are absent from several statements. Braces should always be worn continuously.

  • Variable Increment or Decrement:

The while loop will never come to an end if we don't increase or decrease the value of variable inside of it since otherwise, the condition will always be true. The loop's code will run endlessly as a result of this.

Flow Chart for while loop:

While Loop Syntax in C

The While loop initially looks for the condition.

  • The statements inside of it will be executed if the condition is True.
  • The value must then be incremented and decremented inside it using the Increment & Decrement Operator. For information on the functionality, please see the C article on the Increment and Decrement Operator.
  • After the value is increased, it will check once more time for the condition. The statements inside the condition shall be carried out so long as it is True.

Using the While Loop in C Programming:

#include <stdio.h >


int main()
{
  int x,i, sum=0;


  printf(" Please Enter any integer:\n ");
  scanf("%d", &i);
  x=1;


  while (x <= i)
   {
     sum = sum+x;
     x++;
   }


  printf("\n Value of sum is: %d ", sum); 
  return 0;
}

Explanation:

In above code, we have created three int data type variables, “x”, “i”, and “sum”.

 The user can enter an integer value lower than “i” with this software. This value will cause the compiler to add those values up to “i”.

The sum variable is initialized to zero in this while loop example, and the user will input any amount lower than “i”.

Next, the “x” variable will receive the user-entered value. The supplied “x” will then be checked against the requirement.

The amount is added to the sum if the condition is true. In every other case, the iteration will end.

The ++ operator increases the “x” value in the following line. When the expression returns False, the operation will keep repeating after incrementing.

Program Output :

While Loop Syntax in C

Another program for while loop:

#include<stdio.h>


int main()
{
	int x=1;
	while(x<=10)	
	{
		printf("%d\n",x);
		x++;		
	}
	return 0;
}


Code Explanation:

In the above code, we used a while loop to display a succession of numbers from 1 to 10.

  • We have set the value of the variable “x” to 1. The variable is initialized with the value one because we are printing from 1 to 10. Assign the number 0 during initialization if you wish to start printing at 0.
  • Given the condition (x=10) in a while loop, the body of the loop will be executed up to the point where “x” equals 10. Following that, the loop will be broken, and control will shift outside of it.
  • A print function and an increment operation are provided in the body of a loop so that the value printed per loop execution can be increased. Initially having the value 1, “x” will change to 2 after execution and then to 3 on the following run. The series will be printed on the console, and the loop will be broken whenever the value reaches 10.

 Program Output :

While Loop Syntax in C

Related Topics

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.