Continue Code After While Loop C
Introduction:
We have looked at the Switch statement in our previous article, In today's article, We are going to look at the Loops in programming and While loop in C Language. We also look at the step-by-step walk-through of the while loop, along with the Infinite loops in C Programming.
Loops in Programming:
There will be times when you want to repeat a piece of code multiple times.
For example, if You want to print all even numbers from 1 to 1000 , You can go ahead and write 500 printf statements. But that is not ideal. In such cases, We can use the Loop .
Loop:
As the name suggests, Repeat the code block until you met some condition.
In the above case, You might maintain a counter and once the counter reaches 1000, You will stop the loop.
There are three loops in C Programming language.
- while loop
- do while loop
- for loop
We are going to discuss the while loop in this article.
While loop in C Language:
While loop is used to execute a set of statements repeatedly (in a loop). Until a condition is met.
Here is the syntax of the while loop
While loop Syntax:
while ( condition ) { // Statements to execute . . . . . . . . . . } |
How while loop works in C Language:
We have a Condition in the while loop, Which evaluates to either true or false
First, the while loop checks the condition if the condition is evaluated to true , Then the Code inside the while loop ( Body of While loop ) will be executed.
After executing the statements in the while loop, while checks the condition again, If the condition is evaluated true , then it again executes the statements inside the while body .
This process will continue until the condition becomes false .
Once the while condition becomes false , The While loop exit and control come out of the loop to execute the subsequent statements in the program.
📢 The Body of the while is executed until the condition becomes false
Flow Diagram of While loop in C language:
Example to demonstrate the while loop in C:
Let's write a C program to print the even numbers from 1 to 10 using the while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<stdio.h> int main ( ) { // Start the 'i' with 1 int i = 1 ; while ( i <= 10 ) { // Check if the 'i' is even number using modulus operator if ( i % 2 == 0 ) { printf ( "%d " , i ) ; } // we need to increment the 'i' i ++ ; } // new-line printf ( "\n" ) ; return 0 ; } |
save the program as the while-loop.c and run it using your compiler.
Program Output:
we used gcc compiler to compile the program.
$ gcc while - loop . c $ . / a . out 2 4 6 8 10 $ |
Program Explanation: While Loop Step-by-Step walk-through:
To print the even numbers from 1 to 10, We take a variable i and assign it the value 1 . Then we have our while condition which is i ≤ 10 ,
First Iteration:
At the start,
- The i will be 1 , So condition i ≤ 10 evaluated as true
- Body of while is executed
- Which checks if i is an even number using the modulus operator and prints i variable if it even , Our i value is 1 , So it is not an even number. So the number won't be printed on the console.
- After that, we are increasing the i by 1 , So our i variable becomes 2
Output so far : no - output -so-far
Second Iteration:
- As while is a loop, It will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 2 , So condition i ≤ 10 evaluated as true
- The body of while is executed
- Which checks if i is an even number using the modulus operator and prints i variable if it even , Our i value is 2 , So it is an even number. So the number 2 will be printed on the console.
- After that, we are increasing the i by 1 , So i variable becomes 3
Output so far: 2
Third Iteration:
- we will again go back to the condition and checks the condition ( i ≤ 10 )
- Now i is 3 , So condition 3 ≤ 10 evaluated as True
- The body of while is executed
- Which checks if i is an even number using the modulus operator and prints i variable if it even , Our i value is 3 , So it is not an even number. So the number is not printed on the console.
- After that, we are increasing the i by 1 , So i variable becomes 4
Output so far: 2
Fourth Iteration:
- While will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 4 , So condition 4 ≤ 10 is evaluated as True
- The body of while is executed.
- Which checks if i is an even number using the modulus and prints i variable if it even , Our i value is 4 , So it is an even number. So the number 4 will be printed on the console.
- After that, we are increasing the i by 1 , So i variable becomes 5
Output so far: 2 4
Fifth Iteration:
- While will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 5 , So condition 5 ≤ 10 is evaluated as True
- The body of while is executed
- Here we are checking if i is an even number using the modulus operator and printing the value of i , if it even , Our present i value is 5 , So it is not an even number. So the number 5 won't be printed on the console.
- After that, we are increasing the i by 1 , So the i variable becomes 6
Output so far: 2 4
Sixth Iteration:
- While will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 6 , So condition 6 ≤ 10 is evaluated as True
- The body of while is executed.
- Which checks if i is an even number using the modulus and prints i variable if it even , Our i value is 6 , So it is an even number. So the number 6 will be printed on the console.
- After that, we are increasing the i by 1 , So i variable becomes 7
Output so far: 2 4 6
Seventh Iteration:
- While will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 7 , So condition 7 ≤ 10 is evaluated as True
- The body of while is executed
- Here we are checking if i is an even number using the modulus operator and printing the value of i , if it even , Our present i value is 7 , So it is not an even number. So the number 7 won't be printed on the console.
- After that, we are increasing the i by 1 , So the i variable becomes 8
Output so far: 2 4 6
Eighth Iteration:
- While will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 8 , So condition 8 ≤ 10 is evaluated as True
- The body of while is executed.
- Which checks if i is an even number using the modulus and prints i variable if it even , Our i value is 8 , So it is an even number. So the number 8 will be printed on the console.
- After that, we are increasing the i by 1 , So i variable becomes 9
Output so far: 2 4 6 8
Ninth Iteration:
- While will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 9 , So while condition 9 ≤ 10 is evaluated as True
- The body of while is executed
- Here we are checking if i is an even number using the modulus operator and printing the value of i , if it even , Our present i value is 9 , So it is not an even number. So the number 9 won't be printed on the console.
- After that, we are increasing the i by 1 , So the i variable becomes 10
Output so far: 2 4 6 8
Tenth Iteration:
- While will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 10 , So condition 10 ≤ 10 is evaluated as True
- The body of while is executed.
- Which checks if i is an even number using the modulus and prints i variable if it even , Our i value is 10 , So it is an even number. So the number 10 will be printed on the console.
- After that, we are increasing the i by 1 , So i variable becomes 11
Output so far: 2 4 6 8 10
Eleventh Iteration:
- While will again go back to start and checks the condition ( i ≤ 10 )
- Now i is 11 , So condition 11 ≤ 10 is evaluated as False
Now the while loop will stop the iteration and Control comes out of the while loop.
Final Output : 2 4 6 8 10
📢 As you can see the while loop only exits if the condition becomes false
Example 2: While Loop to Calculate the sum up to given Number:
Let's write another program to calculate the sum of all numbers from 1 to the user-provided number.
We are going to use the while loop to iterate over the numbers from 1 to user-given number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /* Program : Sum of Numbers using while loop */ #include<stdio.h> int main ( ) { int num ; // Start the 'i' with 1 int counter = 1 ; // Initialize the 'sum' to zero int sum = 0 ; // Take the input from the user printf ( "Enter a Number: " ) ; scanf ( "%d" , & num ) ; while ( counter <= num ) { sum = sum + counter ; // sum += counter // you can use complex statement like this as well. // we need to increment the 'i' counter ++ ; } // print the sum printf ( "Sum of all numbers from 1 to %d is %d\n" , num , sum ) ; return 0 ; } |
Paste the above code and save it.
Program Output:
We are using the GCC compiler to compile the program. you can use your favorite compiler.
Here are the instructions to compile the program in Linux.
- https://sillycodes.com/compiling-c-program-in-linux-or-unix/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $ gcc while - loop - sum . c $ . / a . out Enter a Number : 10 Sum of all numbers from 1 to 10 is 55 $ . / a . out Enter a Number : 4 Sum of all numbers from 1 to 4 is 10 $ . / a . out Enter a Number : 100 Sum of all numbers from 1 to 100 is 5050 $ . / a . out Enter a Number : 1000 Sum of all numbers from 1 to 1000 is 500500 $ . / a . out Enter a Number : 50 Sum of all numbers from 1 to 50 is 1275 $ |
While loop in C language Example Program Explanation:
In the above program, We have asked a number from the user and stored in the num variable and then we used the while loop to iterate from 1 to the num .
We also took a counter variable, which is a control variable for our while loop, We increment this variable while iterating from 1 to num
We also have the sum variable, The default value of the sum is zero.
At each iteration, We add the value of the counter to the sum and we also increment the counter to next number (i.e increment by 1).
So the sum will become 0, 1, 3, 6, so on. Depending on the num the while loop stops.
For example, If the user given 4 as the num , Then
- In the first iteration, counter = 1 and the sum becomes 1 ( sum = sum + counter which is sum = 0 + 1 ).
- Second Iteration, counter = 2 and the sum becomes 3 ( sum = 1 + 2 )
- Third Iteration, counter = 3 and the sum becomes 6 ( sum = 3 + 3 )
- fourth Iteration, counter = 4 and the sum becomes 10 ( sum = 6 + 4 )
- Fifth Iteration, counter = 5 , which makes the while condition counter & lt ; = num i.e 5≤4 to fail. So while loop will be exited and control comes out of the while loop.
Infinite Loops in C Language:
Any while loop can become an Infinite loop if the while condition never becomes False . So the while loop will run forever.
💡 While loop has a counter, In the above case it is our variable counter . We need to make sure to increment or decrement the counter variable counter to meet our while condition. In the above case, we incremented the counter .
If you forgot to increment or decrement the counter variable, Then while condition won't become False, Which means while always returns True . This causes the body of the while loop to execute forever. This phenomenon is called Infinite Loop.
Here is an example program of an Infinite loop:
Infinite Loop Example in C Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* Program : Infinite loops in C Language */ #include<stdio.h> int main ( ) { int counter = 1 ; while ( counter <= 10 ) { printf ( "Counter value : %d \n" , counter ) ; // We forgot to increment the `counter` value. } return 0 ; } |
Infinite loop Program Output:
$ gcc infinite - loop . c $ . / a . out Counter value : 1 Counter value : 1 Counter value : 1 Counter value : 1 Counter value : 1 . . . . . . . . . Press Control + C $ |
We can create Infinite loops by not incrementing or decrementing the while control variable (i.e counter )
Here we have not incremented the counter value inside the while loop, So the counter value remained the 1 forever. So the while condition 1 & lt ; = 10 always returns True . So our while loop returns True forever.
If you execute the above program, You will see the Counter value : 1 prints continuously on the console.
Note: Above program runs continuously so to kill the program press the Control + c (If you are using a Linux terminal) or Close the output window (if you are using the Windows PC)
Avoiding the Infinite Loops in C Language:
To solve the above infinite loop problem, We need to increment the counter variable inside the loop.
Here is the modified code, which prevents the infinite loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* Program : Avoid Infinte loops */ #include<stdio.h> int main ( ) { int counter = 1 ; while ( counter <= 10 ) { printf ( "Counter value : %d \n" , counter ) ; // increment the `counter` value. counter = counter + 1 ; } return 0 ; } |
Program Output:
$ gcc no - infinite - loop . c $ . / a . out Counter value : 1 Counter value : 2 Counter value : 3 Counter value : 4 Counter value : 5 Counter value : 6 Counter value : 7 Counter value : 8 Counter value : 9 Counter value : 10 $ |
As you can see from the above code, we incremented the counter value inside the while loop. At each iteration, we increment the counter value by 1, which takes the counter value closer to 10 and finally makes the while condition counter <= 10 to False. and the while loop will exit and program control comes out of the while loop.
Where to use Infinite loops:
There are a few cases where you want to run your program forever.
For example, a Web Server can run forever to serve the requests from clients. In such cases, The web server needs to be online and running forever. In such cases, we use Infinite loops.
Conclusion:
We have discussed about the Loops in C programming language and specifically, we discussed about the While loop in C Language with example programs. We even looked at the step-by-step walk-through of the while loop. Finally, we looked at the Infinite loops and how infinite loops occur, and how we can avoid them.
- Decision making statements if and if…else in C
- Nested if else and if..else ladder in C-Language
- Switch Statement in C Language
Tutorials Index:
Pattern Programs:
dingesslormeaving.blogspot.com
Source: https://sillycodes.com/while-loop-in-c-language-with-example-programs/
0 Response to "Continue Code After While Loop C"
Post a Comment