Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP Java 9-17-2015 Review If else.

Similar presentations


Presentation on theme: "AP Java 9-17-2015 Review If else."— Presentation transcript:

1 AP Java Review If else

2 Learning Objectives Be able to implement Java’s if and if..else into a program. Be able to evaluate boolean conditions.

3 What do you recall about…
Types in Java Math operations Getting information from the user Showing information on the screen Byte Code, Java Virtual Machine, Compiling Class Method JavaDocs

4 if sample number1 = input.nextInt(); // read first number from user
// Fig. 2.15: Comparison.java // Compare integers using if statements, relational operators // and equality operators. import java.util.Scanner; // program uses class Scanner public class Comparison { // main method begins execution of Java application public static void main( String args[] ) // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); int number1; // first number to compare int number2; // second number to compare System.out.print( "Enter first integer: " ); // prompt number1 = input.nextInt(); // read first number from user System.out.print( "Enter second integer: " ); // prompt number2 = input.nextInt(); // read second number from user if ( number1 == number2 ) System.out.printf( "%d == %d\n", number1, number2 ); if ( number1 != number2 ) System.out.printf( "%d != %d\n", number1, number2 ); if ( number1 < number2 ) System.out.printf( "%d < %d\n", number1, number2 ); if ( number1 > number2 ) System.out.printf( "%d > %d\n", number1, number2 ); if ( number1 <= number2 ) System.out.printf( "%d <= %d\n", number1, number2 ); if ( number1 >= number2 ) System.out.printf( "%d >= %d\n", number1, number2 ); } // end method main } // end class Comparison if sample

5 If If (condition) { Commands; }

6 Conditions Pascal Java Example = == (x==y) <> != (x!=y) >
>= (x>=y) <= (x<=y) AND && (x<y)&&(y<z) OR II (x>y)||(x<z)

7 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

8 Is it true or false? boolean test1 = 1 < 2;

9 *, /, % Multiplicative operators +, - Additive operators
Logical Operators Worksheet If x = -2, y = 5, z = 0 , and t = -4, what is the value of each of the following logical expressions? x + y < z + 1 x - 2 * y + y < z * 2 / 3 3 * y / 4 < 8 && y >= 4 t > 5 || z < 2 x * y < 10 || y * z < 10 (y + 2) / 3 > 3 && t < 0 x * 3 > 0 || y + 5 / t < 2 !(x > 0) !(x * t < 10) || y / x * 4 < y * 2 t > 5 || z < (y + 5) && y < 3 !(4 + 5 * y >= z - 4) && (z - 2 < 7) Write syntactically correct logical expressions for the following conditions: m is less than 100 n is positive and greater than m m is between 5 and 10 (inclusive) k is less than 1 or greater than 2 j and k are both negative 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

10 You can leave off the {} if there is only one command line.
If else You can leave off the {} if there is only one command line. if (condition) { commands; } else Commands;

11 System.out.println("Young 'en"); else if (age<20)
import java.util.Scanner; //Smith's if..else sample public class IfElseSample { public static void main(String[] args) { int age=0; //create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); System.out.println("Please enter your age"); age = input.nextInt(); if (age<=12) System.out.println("Young 'en"); else if (age<20) System.out.println("Teeny bopper"); else if (age < 65) System.out.println("Time for work"); System.out.println("prep for retirement"); } else System.out.println("Retire"); System.out.println("second career"); System.out.println("Pay off kids college");

12 Programs: Complete the following.
Modify the quadratic equation program to also calculate imaginary roots. If b2 – 4ac >= 0 then display the real roots Else show the imaginary roots Input three real numbers, and print the numbers in sorted order. Write a program that inputs a persons name, rate of pay, and the number of hours worked during the week. The program will calculate the pay for the person. For every hour of overtime work (over 40 hours in the week) the person gets paid 150 percent of the regular wage. Example: Input: Sue, $10.00 per hour rate, 45 hours Calculation: pay = 10* *10*5 = $475 Input a value representing a person’s percent score (example inputting 80 represents an 80% score), write a program that will output the corresponding letter grade. ‘A’ for , ‘B’ for 80 to 89, etc.


Download ppt "AP Java 9-17-2015 Review If else."

Similar presentations


Ads by Google