Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Review if Online Time For loop Quiz on Thursday.

Similar presentations


Presentation on theme: "Java Review if Online Time For loop Quiz on Thursday."— Presentation transcript:

1 Java Review if Online Time For loop Quiz on Thursday.

2 Learning Objectives  Review if..else and conditions  Be able to read and write a program that uses a for loop.

3 Order of operations for comparisons () *, /, % Multiplicative operators +, - Additive operators, >=, <= Relational operators ==, != Then do any comparisons for equality and inequality && Logical and || Logical or = Assignment operator

4 Is it true or false? boolean test1 = 1 < 2; boolean test2 = 1 > 2; boolean test3 = 3.5 != 1; boolean test4 = 17*3.5 >= 67.0 - 42; boolean test5 = 9.8*54 <= 654; boolean test6 = 6*4 == 3*8; boolean test7 = 6*4 <= 3*8; boolean test8 = 6*4 < 3*8;

5 Logical Operators Worksheet If x = -2, y = 5, z = 0, and t = -4, what is the value of each of the following logical expressions? 1.x + y < z + 1 2.x - 2 * y + y < z * 2 / 3 3.3 * y / 4 = 4 4.t > 5 || z < 2 5.x * y < 10 || y * z < 10 6.(y + 2) / 3 > 3 && t < 0 7.x * 3 > 0 || y + 5 / t < 2 8.!(x > 0) 9.!(x * t < 10) || y / x * 4 < y * 2 10.t > 5 || z < (y + 5) && y < 3 11.!(4 + 5 * y >= z - 4) && (z - 2 < 7) Write syntactically correct logical expressions for the following conditions: 1.m is less than 100 2.n is positive and greater than m 3.m is between 5 and 10 (inclusive) 4.k is less than 1 or greater than 2 5.j and k are both negative 6.i is an even number Order of Operations () *, /, % Multiplicative operators +, - Additive operators, >=, <= Relational operators ==, != Then do any comparisons for equality and inequality && Logical and || Logical or = Assignment operator

6 Warm-up: T, F or Invalid a=3, b=7, c=10, d = -20  a<b  a<=b  a ==b  a !=b  a-b > c+d  a<b<c  a == b == c TT TT FF TT TT  Invalid  invalid

7 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”);

8 What does the following do? public class ForDryRun { public static void main(String[] args) { int a=0, b=3, c=10; for (int d=0; d<=b; d++) { a+= b; c = d % 2; System.out.printf("a = %d b = %d c = %d d = %d\n", a, b, c, d); } } }

9 For loop When do you use it? Semantics: Syntax: for (init ; continueTest; updateOp) { commands; } Example for (int count = 0;count<10;count = count + 1) { } int count = 0; Creates the count variable Sets count to 0 count<10 Boolean condition Like a while loop, while this is true, it will repeat the loop again. You can use &&, || in this condition count = count + 1 AFTER the loop is completed, this is executed

10 For loop Example // Fig. 5.2: ForCounter.java // Counter-controlled repetition with the for repetition statement. public class ForCounter { public static void main( String args[] ) { // for statement header includes initialization, // loop-continuation condition and increment for ( int counter = 1; counter <= 10; counter++ ) System.out.printf( "%d ", counter ); System.out.println(); // output a newline } // end main } // end class ForCounter counter only exists during the for loop. ?

11 Ways of incrementing a variable Increment/DecrementMeaning count++;count=count+1; count--;count=count-1; count+=4;count =count+4; count-=5;count=count-5; count*=2;count = count*2; count /=3;count = count/3; count %= 5;count = count % 5;

12 Your Turn  By hand write the code (No imports, No public static…) to find the total of 10 scores (integers) input from the user.  Push: Find the highest of these scores.

13 int score, total=0, highest = 0; Scanner input = new Scanner(System.in); for ( int counter = 1; counter <= 10; counter++ ) { System.out.println( "Please enter a score" ); score = input.nextInt(); total+=score; if (score>highest) highest = score; } System.out.println("Total = " + total); System.out.println("Highest = " + highest);

14 Multiple Initializers and Incrementers  Sometimes it's necessary to initialize several variables before beginning a for loop. Similarly you may want to increment more than one variable. Java lets you do this by placing a comma between the different initializers and incrementers like this:  for (int i = 1, j = 100; i < 100; i = i+1, j = j-1) { System.out.println(i + j); } You can't, however, include multiple test conditions, at least not with commas. The following line is illegal and will generate a compiler error. for (int i = 1, j = 100; i 0; i = i-1, j = j-1) { }

15 Programs if/for (At least one from each section)  For Program Options Enter a number: Output it’s factorial Enter a base and an exponent: Using a for loop calculate base^exponent using the for loop. (No Math.pow())  Combined Program options Enter 10 scores. Output the total, average, highest and lowest score. Enter 5 ages and calculate the number of teenagers in the list.


Download ppt "Java Review if Online Time For loop Quiz on Thursday."

Similar presentations


Ads by Google