C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - While Loop

C Programming

In this tutorial we will learn about while loop in C programming language.

When we want to perform a certain set of tasks repeatedly like printing "Hello World" for 100,000 times, we can accomplish this mammoth task in two ways.

The naive approach:

We can either write printf("Hello World\n"); statement for 100,000 times.

The smart approach:

Or, we can take help of while loop which will execute the printf("Hello World\n"); code 100,000 times for us.

Syntax of a while loop.

while (condition) {
  //while block code...
}

We use the while keyword to create a while loop. And the code inside the while-block keeps executing as long as the condition is satisfied.

If the condition is not satisfied then we ignore the while loop.

In the following example we will print "Hello World" 10 times using the while loop.

#include <stdio.h>
int main(void)
{
  //variable
  int count = 1;
  
  //loop
  while (count <= 10) {
    printf("Hello World\n");
    
    //update
    count++;
  }
  
  printf("End of code\n");
  return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
End of code

So, in the above code we have a variable count which is initially set to 1. We are using this to loop 10 times.

Next we have the while loop. The condition in the above code is count <= 10 which means we will execute the code inside the while loop as long as count is less than or equal to 10. So, when the value of count will become greater than 10 then we will jump out of the while loop.

Inside the while loop we have the printf statement which prints "Hello World".

Then we are increasing the value of count by 1 using the increment ++ operator.

On reaching the end of the while-block we recheck the condition and if it is still valid we rerun the loop otherwise, we come out of the loop.

In the following example we will print multiplication table of 2 using the while loop.

#include <stdio.h>
int main(void)
{
  //variable
  int product, count = 1;
  
  //loop
  while (count <= 10) {
    product = 2 * count;
    printf("2 x %d = %d\n", count, product);
    
    //update
    count++;
  }
  
  printf("End of code\n");
  return 0;
}

Output

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
End of code

In the above code we have a count variable which is initially set to 1 and will update till 10. We also have a product variable to help us in storing the multiplication result.

The condition for the while loop is count <= 10 so, the loop will execute as long as count is less than or equal to 10. Once the value of count becomes greater than 10, we will jump out of the loop.

Inside the loop we first find the product of 2 * count which gives us 2, 4, 6 and so on.

Then we have the printf statement which prints the product.

Finally we update the value of count by 1 using the increment ++ operator.

Infinite loop

In the following example the while loop will keep printing the message "Hello World" till the program runs out of memory. This is because the condition is always satisfied so, we don't jump out of the loop.

#include <stdio.h>
int main(void)
{
  //variable
  int count = 1;
  
  //loop
  while (count > 0) {
    printf("Hello World\n");
    
    //update
    count++;
  }
  
  printf("End of code\n");
  return 0;
}

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT