0% found this document useful (0 votes)
11 views

While Loop

selectionstructure

Uploaded by

Mary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
11 views

While Loop

selectionstructure

Uploaded by

Mary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
5/14/24, 8:03 AM while loop in C - GeeksforGeeks while loop in C Last Updated : 07 May, 2023 The while Loop is an entry-controlled loop in C programming language. This loop can be used to iterate a part of code while the given condition remains true. Syntax The while loop syntax is as follows: while (test expression) { // body consisting of multiple statements Example The below example shows how to use a while loop in a C program Cc // C program to demonstrate while loop #include int main() { // Initialization of loop variable int i = 05 // setting test expression as (i < 5), means the loop 7/ will execute till i is less than 5 while (i <5) { // loop statements printf ("GeeksforGeeks\n"); // updating the loop variable iets + return @; hntps:nww.geekstorgecks orgicawhileloop/7eef=bp 18 5/14/24, 8:03 AM while loop in C - GeeksforGeeks Output GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks while Loop Structure The while loop works by following a very structured top-down approach that can be divided into the following parts: LT is not part of while loop syntax but it is essential when we are using some variable in the test alization: In this step, we initialize the loop variable to some initial value. Initialization expression 2. Conditional Statement: This is one of the most crucial steps as it decides whether the block in the while loop code will execute. The while loop body will be executed if and only the test condition defined in the conditional statement is true. hntps:inww.geekstorgecks orgicanhiteloopi7eef=Ibp 218 sare, 503 aM vii loo in C= GeestorGeoks 3. Body: It is the actual set of statements that will be executed till the specified condition is true. It is generally enclosed inside { } braces. 4. Updation: It is an expression that updates the value of the loop variable in each iteration. It is also not part of the syntax but we have to define it explicitly in the body of the loop. Flowchart of while loop in C While loop Start Test Condition? False Execute loop body While loop ends Working of while Loop We can understand the working of the while loop by looking at the above flowchart: hntps:inww.geekstorgecks orgicanhiteloopi7eef=Ibp 38 5/14/24, 8:03 AM while loop in C - GeeksforGeeks 1. STEP 1: When the program first comes to the loop, the test condition will be evaluated. 2. STEP 2A: If the test condition is false, the body of the loop will be skipped program will continue. 3. STEP 2B: If the expression evaluates to true, the body of the loop will be executed. 4, STEP 3: Afier executing the body, the program control will go to STEP 1. This process will continue till the test expression is true. Infinite while loop An infinite while loop is created when the given condition is always true. It is encountered by programmers in when: + The test condition is incorrect. + Updation statement not present. Example // © program to demonstrate an infinite while loop #include int main() { // Initialization int gfgl = 1; int gfg2 = 1; // ‘gfgi' is the Check/Test statement, which means that // the while loop will iterate till the conditions // satiate while (gfgi < 10) ¢ // *efg2' is the body statements efg2 = gfg2 + 15 printf("GeeksforGeeks to Infinity"); + // Return statement to tell that everything executed hntps:nww-geekstorgecks orgic-nhiteloop/7eef=bp 48 sr1424, 803M ile loop in C- GecktorGacks // safely return @; hntps:inww.geekstorgecks orgicanhiteloopi7eef=Ibp 58

You might also like