Learn C Language - Explanation of While Loop in C Programming
Programming Languages
Explanation of While Loop in C Programming
In this chapter, we will learn about the loop. Many programming languages support three types of loops. While loop, do while loop and for loop, in this chapter, we will focus more on the while loop. So if you want to write a simple program in C, like printing a variable, it's pretty simple. We have done this in the last chapters many times, right? What you will do is you will create a main statement, you will initialize the variable. Let's say it's I equal to one. You will use printf statement to display the value. So this is the program. It's output will be I equal to one.
Now suppose you want to print one to 9 like this. What will you do? Will you write the printf statement 9 times? No, it's too much work. What you will do is you will write a printf statement one time and you will execute it 9 times. Let's see how this is done in C language. Let's open our source code again. What we will do is we will put the printf statement inside while like this in the vial statement, we will write a condition that I is less than ten. Remember, we have used this condition in F and L statements. Here, we are telling our program to run print value statement till 9. But the value of I is only one, so we need to tell the program to increment the value. So what we will do is we will ask our program to increment the value of I after printf is executed like this. Here, we have initialize the variable I equal to one, this is called initialization.
Next, we have given the condition and after that, we have given the increment. Now, the only thing is print test statement. Instead of printf, we can write any logic that we want. So what we understand is for loop execution, three things are very important. First is the initialization. Second is the condition and third is increment. Just try to understand the program. First, computer reaches the part of initialization that is R equal to one next, it reaches the vile condition. In wild condition, it checks whether eyes less than ten, but currently the value of I is one. So one is definitely less than ten. The result is true. So it goes to the printf statement, and it prints the value of I that is one. In the next statement, it says R equals two, I plus one. So the value of I becomes two. Now what happens is it goes to the end of the bracket and again it jumps to the condition. In the condition, it checks where the eye is less than ten or not, but the value of I is to so again the condition is true. Since the condition is true, the system enters the loop again, and it prints the value of I that is to.
Now it enters the increment code. In increment, value of R becomes three. Now it again enters the condition. And in the condition, system checks that where the eye is less than ten or not. But again, the condition is true because three is less than ten. So what happens is it again enters the loop. Now, suppose we have done the printing till 9. After printing the value of 9, system goes to increment and the value of I becomes ten. Now it again checks the wild condition. Condition is I less than ten or not. But ten is definitely not less than ten. So the condition breaks and systems skips the while loop and goes to the next line. Here, the while loop ends.