Your Turn
- Write program code to complete the provided tasks.
- While these problems can be solved using other loop types, use a
for loop to solve them.
- Run the code in the Java Playground to see if your code generates the desired result.
- Make necessary changes until you have satisfied the tasks.
Practice 1: Even Numbers
- Write a
for loop that will print all the even numbers between 2 and 20.
- Try the code in the Java Playground.
Practice 2: I'll Take a Raise
- An employee's current salary is $50,000. The employee will get a $2,000 raise each year for 5 years.
- Write a
for loop that will print the year and the salary for each year.
- Try the code in the Java Playground.
Practice 3: Birthday Count Down
- Write a
for loop to count down the days until a birthday, starting 10 days before. It should print: days until your birthday!
- Add an
if statement so that when there is only 1 day left it says: 1 day until your birthday! instead of days.
- Try the code in the Java Playground.
Practice 4: Movie Times
- Movies start every 2 hours starting at 1PM and ending at 9PM.
- Write a
for loop to print the starting time of movie showings.
- Try the code in the Java Playground.
Practice 5: Modify the while Loop to for Loop
- Run the provided code segment in the Java Playground.
- Re-write it to be a
for loop instead.
- Run the new code in the Java Playground to ensure you obtain the same functionality.
Resources
Practice: Evaluate Expressions that Contain for Loops