Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means some condition does not exist. if(score > 95) highscore = true; Here, highscore is a flag indicating that the score is above 95. Right now, we don t have any situations where these are terribly useful, but for now, just know we can and will use them. The if- if Statement Sometimes you need to be able to test a series of conditions You can do this with the if- if statement General form: if (BooleanExpression1) statement or block 1 if(booleanexpression2) statement or block 2 statement or block 3 If BooleanExpression1 is true, then statement or block 1 is executed. If BooleanExpression1 is false, then BooleanExpression2 is tested. If BooleanExpression2 is true, then statement or block 2 is executed. If BooleanExpression2 is false, then statement or block 3 is executed.
Note: You can have as many if clauses as is needed.
if- if Example import java.util.scanner; public class IfElseIfStatement { public static void main(string[] args) { int testscore; Scanner keyboard = new Scanner(System.in); System.out.print("Enter your numeric test score: "); testscore = keyboard.nextint(); if (testscore < 60) System.out.println("Your grade is an F."); if (testscore < 70) System.out.println("Your grade is a D."); if (testscore < 80) System.out.println("Your grade is a C."); if (testscore < 90) System.out.println("Your grade is a B."); if (testscore < 101) System.out.println("Your grade is an A."); System.out.println("Invalid Grade"); //end main and class
Nested if Statements Nesting is enclosing one structure inside of another. A block in Java can contain any valid Java code, this includes other if statements: if(booleanexpression1) { if(booleanexpression2) { statement3; statement4; statement1; statement2; If BooleanExpression1 is true and BooleanExpression2 is true, what is executed? statement1, statement2, statement3, statement4 If BooleanExpression1 is true and BooleanExpression2 is false, what is executed? statement3, statement4 If BooleanExpression1 is false, what is executed? Nothing Nested if Statements Example import java.util.scanner; public class NestedIfStatements { public static void main(string[] args) { double salary, yearsonjob; Scanner keyboard = new Scanner(System.in); System.out.print("Enter your annual salary: "); salary = keyboard.nextdouble(); System.out.print("Enter the number of years at your current job: "); yearsonjob = keyboard.nextdouble();
Logical Operators if (salary >= 30000) { if (yearsonjob >= 2) System.out.println("You qualify for a loan."); System.out.println("You must have been at your current job " + "for at least 2 years to qualify."); { System.out.println("You must earn at least $30,000 per year " + "to qualify."); Java provides logical operators. The binary logical operators combine two boolean expressions into one. The unary logical operator switches the value of a boolean expression. Operator Meaning Kind && AND Binary OR Binary! NOT Unary Binary logical operators have lower precedence than relational operators (they will be evaluated after) NOT has the same precedence as negation. Logical Operator Truth Tables Truth Tables show the result of a logical expression based on the values of the operands.
Logical Operator Practice 2 > 3 && 4 < 5 2 < 3 && 4 < 5 2 > 3 4 < 5 2 > 3 4 > 5!(2 > 3) Logical AND Example import java.util.scanner; public class LogicalAndOperator { public static void main(string[] args) { char walks, quacks; Scanner keyboard = new Scanner(System.in); System.out.print("Does it walk like a duck (Y or N)? "); walks = keyboard.nextline().charat(0);
System.out.print("Does it quack like a duck (Y or N)? "); quacks = keyboard.nextline().charat(0); if(walks == 'Y' && quacks == 'Y') System.out.println("It's proabably a duck."); System.out.println("It's proabably NOT a duck.");