While loop in c langauge || Explain While loop with Example Program || While Loop Syntax

While loop in c langauge || Explain While loop with Example Program || While Loop Syntax

While loop Introduction


A while loop is a fundamental concept in programming that allows you to repeat a set of instructions as long as a certain condition is true. It is a powerful tool for creating programs that can perform tasks repeatedly without the need for repetitive code. In this blog, we will explore the while loop in C, including how it works and some examples of its use.

Syntax of the While Loop in C


The syntax of the while loop in C is as follows:

WHILE IN C


In this syntax, the "condition" is an expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the curly braces will be executed. After the code is executed, the condition is evaluated again. This process is repeated until the condition becomes false.

Flowchart of a While Loop


The following flowchart illustrates the process of a while loop: 

FLOW DIGRAM WHILE LOOP





In the flowchart, the first step is to evaluate the condition. If the condition is true, the code inside the loop is executed. After executing the code, the condition is evaluated again. This process continues until the condition becomes false. Once the condition becomes false, the loop exits, and the program continues executing from the next statement after the while loop.

Examples of While Loops in C


Let's look at some examples of while loops in C to understand how they work.

Example 1: Print Numbers 1 to 10

In this example, we will use a while loop to print the numbers 1 to 10: 

 

WHILE LOOP PROGRAM IN C




In this example, the while loop starts with the variable i initialized to 1. The loop will continue as long as i is less than or equal to 10. Inside the loop, we use the printf function to print the value of i. After printing the value, we increment the value of i by 1 using the i++ shorthand notation.

Example 2: Calculate the Sum of Numbers from 1 to 100


In this example, we will use a while loop to calculate the sum of numbers from 1 to 100:

WHILE LOOP

In this example, we start with the variable i initialized to 1 and the variable sum initialized to 0. Inside the while loop, we add the value of i to sum and increment the value of i by 1. This process continues until i is greater than 100. After the loop exits, we print the value of sum using the printf function.

 

 a powerful tool for creating programs that can perform tasks repeatedly without the need for repetitive code. It is essential to understand how it works and how to use it effectively when programming in C. By following the syntax and flowchart of the while loop, you can create programs that solve a wide range of problems.

When using while loops, it is essential to be mindful of the condition that you set. If the condition is never met, the loop will never exit, and your program will get stuck in an infinite loop. It is also important to remember to update the variables used in the condition inside the loop to ensure that the loop terminates eventually.

In summary, the while loop is a fundamental concept in programming that enables the creation of programs that perform tasks repeatedly. By following the syntax and flowchart of the while loop and being mindful of the condition and variables used in the loop, you can create powerful programs that solve a wide range of problems.

 

Watch Video

 

 

All C  Topics : -

  1. C Language Intro

  2. Structure of C Language 

  3. Variable & data Types 

  4. Printf and Scanf Function

  5. Operator in c Language 

  6. If Statement in c Language 

  7. If else and Nasted if in C

  8. Switch Case In C 

  9. While loop in C Language 

  10. Exit Control Loop (Do While)

  11. For Loop in c 

  12. C Language Pratice Program 

  13. Goto Statement in c Programming 

  14. Array in c

  15. Pointer in  c Language 

  16. String in c Language 
Post a Comment (0)
Previous Post Next Post