![]() ![]() ![]() 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 OOP, this criticism is increased because of the existing object-oriented design patterns that replaces the switch function.ĭesign patterns are recommended practices that improve the quality of object-oriented programs.In this example, the condition of the while loop is n <= 10. The use of the switch statement in different languages has been criticized for creating a source code which violates the structured programming paradigm. Criticism of the use of the switch statement We also use the default clause to indicate that the selected option does not correspond to any valid option. In this case we use break in each case clause and only one option is executed. JOptionPane.showMessageDialog(null, option "Congratulations, you have chosen option 4") "Congratulations, you have chosen option 3") "Congratulations, you have chosen option 2") "Congratulations, you have chosen option 1") String option = JOptionPane.showInputDialog(menu.toString()) String menu = new String("Choose one option: 1, 2, 3 o 4 \n") This is a common case where our program makes the user choose from several options: The weekDay.toUpperCase () method converts all lowercase letters present in the String object to uppercase, so the comparison works even when we enter the day of the week in lowercase. We do not use the optional default clause. JOptionPane.showMessageDialog(null, "We have \"" daysLeft "\" to end the week") String weekDay = JOptionPane.showInputDialog("Enter the day of the week") In this example, we enter a day of the week in English and the program returns the list the days of the week we have left to finish the week. ![]() Example without break “Days of the week left” ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |