Practice makes better. Repetition is a good thing. Repetition is a good thing. Repetition is a good thing. Repetition is a good thing.
WAIT!!! Why type “Repetition is a good thing,” four times when we can code a better result? How about printing the phrase 100 times instead?
Loops simplify repetitive tasks!
forloops that accomplish the following tasks:
- Print the numbers 0 - 20, one number per line.
- Print only the ODD values from 3 - 29, one number per line.
- Print the EVEN numbers 12 down to -14 in descending order, one number per line.
- Print the numbers 50 down to 20 in descending order, but only if the numbers are multiples of 3.
Initialize two variables to hold the string
'LaunchCode'and the array
[1, 5, 'LC101', 'blue', 42], then construct
forloops to accomplish the following tasks:
- Print each element of the array to a new line.
- Print each character of the string—in reverse order—to a new line.
forloop that sorts the array
[2, 3, 13, 18, -5, 38, -10, 11, 0, 104]into two new arrays:
- Define an empty
evensarray to hold the even numbers and an
oddsarray for the odd numbers.
- In the loop, determine if each number is even or odd, then put that
odds, as appropriate.
- Print the arrays to confirm the results. Print
- Define an empty
To practice with
while loops, open up
Define three variables for the LaunchCode shuttle—one for the starting fuel level, another for the number of astronauts aboard, and the third for the altitude the shuttle reaches.
whileloops to do the following:
- Prompt the user to enter the starting fuel level. The loop should continue until the user enters a positive value greater than 5000 but less than 30000.
- Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry by having the loop continue until the user enters an integer from 1 - 7.
- Use a final loop to monitor the fuel status and the altitude of the shuttle. Each iteration, decrease the fuel level by 100 units for each astronaut aboard. Also, increase the altitude by 50 kilometers. (Hint: The loop should end when there is not enough fuel to boost the crew another 50 km, so the fuel level might not reach 0).
After all the loops complete, output the result with the phrase,
The shuttle gained an altitude of ___ km.
- If the altitude is 2000 km or higher, add “Orbit achieved!”
- Otherwise add, “Failed to reach orbit.”