Java Review if with Dry Run Take a








- Slides: 8
Java Review if with Dry Run Take a look at the for loop.
Learning Objectives o o Be able to read a program that uses an if. Be able to know when and how to use a for loop.
Dry run the following int x = 20, y = 15, z; if (x<y) z = 10; else z = 5; System. out. println(z); int x = 2; if (3 / x == 1) System. out. println(“Equal”); else System. out. println(“Not equal”);
What does the following do? public class For. Dry. Run { public static void main(String[] args) { int a=0; for (int c=0; c<=3; c = c+1) { a = a + 2; System. out. println(a) } } }
For loop int count = 0; Creates the count variable Sets count to 0 When do you use it? Semantics: count<10 Syntax: Boolean condition for (init ; continue. Test; update. Op) When this is true, it will repeat the { loop again. commands; You can use &&, || in this condition } Example for (int count = 0; count<10 ; count = count + 1) { count = count + 1 } AFTER the loop is completed, this is executed Can also use count++, count--, count = count+2, count+=3, …
Check for understanding: Dry run for (int count = 0; count<5; count++) { System. out. println(“Hello World”); } for (int count = 5; count>0; count--) { System. out. println(count); }
Check for understanding: Dry Run int total = 0; int times = 4; for (int looper = 0; looper<times ; looper++) { total = total + count; } System. out. println(“The total is “+ total);
For loop first program o Stars n n Input: Nothing Output: Use a for loop to show 10 stars (*) o o Push: User inputs the number of stars to show Push: Input the number of rows of stars and columns of stars, then the program shows the grid based on the user input using a for loop inside a for loop (nested) Using a for loop for the repeated phrases, write a program that will display the words to “Baby Shark”. Using a for loop, enter 10 test scores and calculate the total score n Push: Find the average and lowest score.








