Let us now take a look at how the same problems can be solved via both, for and while loop.
Now we are going to take a look at some cases in which you can use the while loop for solving these problems, but you cannot use the for loop for these. Now, consider a situation in which you have a bucket and you have another container containing some water. You have a mug and you are supposed to take in some water from the container and pour it inside the bucket. So is there a way to solve it using the for loop? Well, if you recall, the for loop for loop is always of this type. For some variable is initialized here and then you take the variable till the final point. That is, you define the ending condition for that variable and you increment that variable. So this was the general template for writing a for loop. Now, since you don't know what is the size of the bucket or the size of the mug, do you know how many times you have to take in water from the mug and pour it inside the bucket? Well, if you don't know their volumes, then there is no way to determine in how many steps will the bucket be full. This clearly means that even though you might know the start point of this particular problem, but you don't know how many times you actually need to repeat this process. So in cases such as these, in which you don't actually know beforehand how many times you need to repeat some procedure, it is best to use the while loop for the same bucket. Example, we can initialize A where we can take in a variable called is full. Now, this is a Boolean variable called is full, which denotes whether the bucket is full or no. In the beginning the bucket is empty. So in the beginning my is full variable will be false.
Now, I can say that while is full is equal to is equal to false, that is, my bucket is not full yet. Then during that, keep on pouring more water. So this process is going to repeat till my bucket becomes full and my is full variable becomes true. When the is full variable becomes true, this while loop will automatically stop getting executed and we would have reached a stage where our bucket will be full. So in this case, we used a while loop because we didn't know how many times do I need to take in water from the mug and pour it inside the bucket. So basically, the underlying idea here is that the while loop can be used to repeat a certain procedure using some conditions where you have to evaluate whether some condition is true or false, then you can use the while loop there. However, in those cases, for loop cannot be used because there is no way to check for any boolean values in for loops. In for loops are useful when you have to perform some mathematical operations. For example, you know that your numbers are going to vary from one till 10 or you have to find the sum of all numbers from one to 1000. So you know that this process is going to repeat some finite number of times. So at each step you can tell the for loop where to begin, where to end and what to increment. However, in these cases, such as the booleans, you never know beforehand how many times the procedure is going to repeat. Similarly, let us take in another example here. Suppose I ask the user to input two numbers x and y. Now I say that while the user keeps on inputting x and y and while x is greater than y, then keep on printing that x is greater than y. So what happens is that suppose in the first iteration user inputs four, comma five, then this statement is going to get printed or no will depend on this while condition. So it will check whether four is greater than five. Since this is not true, it will not be printed. Then the next time user inputs two numbers, suppose the user inputs seven and two. Then in that case while x greater than y will be true. And so this statement is again going to get printed. So the computer keeps on taking in put from user input of two numbers and keeps on printing whether x is greater than y. So even this case comes under how we can solve this problem using the while loop.
The segment discusses the difference between using while loop and for loop in certain problem-solving situations.
For loops are useful in cases where a finite number of repetitions are known, such as mathematical operations or when numbers are within a specified range.
While loops are useful in situations where the number of repetitions is unknown, such as when dealing with Boolean conditions.
While loops are used to repeat a certain procedure until a specific condition is met.
An example of using a while loop is when pouring water from a mug into a bucket until the bucket is full.
Another example is when taking user input of two numbers and continuously printing if x is greater than y while x is greater than y.
Let us now take a look at cases where only a for or a while loop can be used.
In the next segment, you will learn how to code for loops in Java.