×

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

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

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.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.