Create a for loop that runs however many times the user wants it to. Its only job is to count up from 0 (the highest number should be one less than the number the user entered). Each number should be on a different line. Next, in the same program after the for loop from step 1, create a do while loop that asks the user for another number. If the number is greater than 50, the do while loop can end. If the number is 50 or less, the loop should continue to run, asking the user to input a number. (Consider using an if/else statement inside of your do while loop to accomplish this.)
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
This program will do multiple things using loops to get you ready for more complex problems. Please use the looping structures requested to complete this part of your lab.
- Create a for loop that runs however many times the user wants it to. Its only job is to count up from 0 (the highest number should be one less than the number the user entered). Each number should be on a different line.
- Next, in the same program after the for loop from step 1, create a do while loop that asks the user for another number. If the number is greater than 50, the do while loop can end. If the number is 50 or less, the loop should continue to run, asking the user to input a number. (Consider using an if/else statement inside of your do while loop to accomplish this.)
Sample output: (Comments should not print. They are there for clarification.)
Enter a number: //Part 1 using for loop
>>>6
0
1
2
3
4
5
Enter a number: //Part 2 using do while loop
>>>20
Enter a number:
>>>35
Enter a number:
>>>52
Goodbye.
in java
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images