While loop in C programming language

While loop in C programming language

In this tutorial, we will discuss the While loop in C programming language

In the C Programming Language, the while is used to evaluates test condition and executes the statement(s) until the Boolean condition becomes true. When the condition becomes false, control exit the loop and goes to the next statement after the while.

Syntex
whileTest_Expression)
{
//Body of while
//statements

}

flow diagram

How while loops works

Initially, the while loop evaluates the test expression inside the parenthesis

if the test expression is true, codes inside the body of while loop are executed and the test expression is evaluated again until the test expression becomes false

when the test expression becomes false the loop control exits from the loop and loop terminated

Program 1

This program displays positive numbers of 1 to 10

 

The above code is executed, it produces the following result

1
2
3
4
5
6
7
8
9
10

 

Program 2

while loop using logical or operator

logical OR (||) operator executes when anyone become true
The above code is executed, it produces the following result
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

 

Infinite loop

When value >=5, the loop would never end.
The above code is executed, it produces the following result
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
……..
………
ctr+c – to end the execution
Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.