Since the value of n is 1, the code in the body of the for loop is executed and thus the current value of n i.e. In the first iteration, the condition n <= 10 is checked. In the above example, initially n = 1 assigns the value 1 to n. There can be any expression here which you want to run after every iteration. In this example, the value of n increases by 1 every time the code in the body of the for loop executes. N++ → This is executed after the code in the body of the for loop has been executed. This is similar to the condition we use in a while loop which gets checked again and again. If it is false, the loop gets terminated. If the condition is true, the statements written in the body of the loop are executed. N <= 10 → This is a condition which is checked.
Here, the variable n is assigned the value 1. N = 1 → This step is used to initialize a variable and is executed first and only once.
The syntax of the for loop is given below. Let's go to our first example in which we printed the first 10 natural numbers using a while loop. So, now you know that we use loops to repeat a task multiple times. Thus, on checking the third time, the condition of the loop becomes false leading to the termination of the loop. So, again the body of the loop is executed, but this time the user entered the value of choice as 0, making choice equal to 0. In the second iteration, again the condition is true because the value of choice is 2.
After that, on asking the value of choice, the user entered 1, making choice equal to 1. The user first entered 4, which is even and so the message “Your number is even” gets printed. In the first iteration, the condition of the loop is true because the value of choice is 1, and hence the statements in the body of the loop get executed. After this, the user is again asked to enter a number - 1 if he wants to check more and 0 otherwise, and this entered number is assigned to the variable choice. In the body of the loop, it is checked if the number entered by the user is even or odd and the corresponding message is printed. In each iteration, if the condition of the loop is true, the statements in the body of the loop are executed. Since the condition of the loop is choice = 1, the loop will run until the value of choice becomes something other than 1. The next program prints the multiplication table of 12 using a while loop.Įnter a number to check odd or even 4 Your number is even Want to check more 1 for yes 0 for no 1 Enter a number to check odd or even 3 Your number is odd Want to check more 1 for yes 0 for no 0 I hope you checked all your numbers. The following animation will also help you to understand the working of the while loop. In the above example, there were ten iterations. Cool, right?Ĭhecking of the condition and executing the body of the loop makes one iteration. Similarly, you can also print the first 1000 natural numbers using the same number of lines of code. In this way, we printed the first 10 natural numbers without printing them separately one by one. Now, this time the condition n <= 10 becomes false and the loop gets terminated. 10 is printed and the value of n is increased to 11. Going on, when the value of n becomes 10, again the condition n <= 10 is true. 2 is printed and the value of n is increased to 3. This time also the condition n <= 10 is true because the value of n is 2. The second statement n++ increases the value of n by 1, making n equal to 2. The first statement (n) prints the value of n i.e. If the condition is true, then the statements of the loop are executed. While(n <= 10) → The condition n <= 10 of the loop is checked. The body of the loop consists of the statements (n) and n++. In this example, the condition of the while loop is n <= 10.