Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C Language while statement with example

Posted on August 23, 2016January 29, 2020

Table of Contents

    • C Language while statement with example
  • While loop
  • How while loops works
    • program 1
    • Nested while loop

C Language while statement with example

In this article, we will discuss The C language while loop
In this post, we learn how to use while loop in C language

generally, in programming languages, the looping statement is used to perform the block of code until the condition is satisfied.

C program has 3 looping statement

  • for loop
  • while loop
  • do-while loop
here we can clearly understand while loop in C language

While loop

Syntax
 
while(test expression) {
   statement(s);
}

How while loops works

first the initialization statements are executed only once

Then the test expression is evaluated. When the test expression becomes false. The control comes out from the loop and wile loop is terminated.
However, when the test expression evaluated to true, statements inside the body of  while loop are executed.
finally, the updating statement(increment/decrement) is updated

then again the test expression is evaluated
This process happening on until the test expression becomes false.

C Language while statement with example
Syntax


Flow diageam While loop in C language

C Language while statement with example
flow diagram

 

program 1

#include <stdio.h>
#include <stdlib.h>
int main()
{
    //local variable definition
    int a=1;
    //while loop execution
    while(a<10)
    {
        printf(“value of a :%dn”,a);  //display statement
        a++;                                       // increment statement
    }
    return 0;
}
When the above code is executed, it produces the following result

 This program displays natural numbers from 1 to 9

 

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i=1,sum=0;
    printf("Enter a number for find sum\n");
    scanf("%d",&num);
    while(i<=num){
        sum+=i;   //sum=sum+i
        i++;
    }

    printf("Sum of natural numbers 1 to %d is: %d",num,sum);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a number for find sum
100
sum of natural numbers 1 to 100 is: 5050

 

Nested while loop

while(condition) {

   while(condition) {
      statement(s);
   }

   statement(s);

}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=0;
    int j=0;
    while(i<=10)
    {
        printf(“*”);
        printf(“n”);
j=0;
    while(j<=10)
    {
        printf(“*”);
        j++;
    }
    i++;

}


}

we can print a rectangular pattren using two while loop at the above program

When the above code is executed, it produces the following result

 

While Loop in Java      while Loop in C++     While Loop In C

Do-while Loop in Java    Do-while Loop C++      Do-while loop in C 

For Loop in C++       For Loop in C         For loop in java

If condition C++       If condition in Java   If condition in C

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes