220 likes | 325 Views
Booleans and Selection Statements. Boolean Type. The type boolean only holds 2 possible values: true and false. It is declared as any other variable: boolean b = true; boolean c;. Comparison Operators for Numbers. Used to hold the value of comparisons: int x = 2; int y = 1;
E N D
Boolean Type • The type boolean only holds 2 possible values: true and false. • It is declared as any other variable: boolean b = true; boolean c;
Comparison Operators for Numbers • Used to hold the value of comparisons: int x = 2; int y = 1; boolean b = x < y; • Other operators:<, >, <=, >=, !=, ==
testScore < 80 testScore * 2 >= 350 30 < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius <= 359.99 Boolean Expressions • Do not stand alone as statements!
Boolean Operators • && (AND) – true only if both operands are true • || (OR) – true if either operand is true • ! (NOT) – true if the operand is not true • Examples:int x = 2;int y = 3;int z = 4;boolean b = x==2;boolean c = !b;boolean d = (y<4) && (z<3);boolean e = (y<4) || (z<3);boolean f = !d;
Example Program: Is the number even or odd? public boolean isEven(int myNum){ boolean even = (myNum%2) == 0; return even; }
Simple If Statement • New type of statement: if-statement • Only take a certain action if something is true. • if(BOOLEAN){ ACTIONS;}
Examples • int x = 4; if (x %2 == 0){ System.out.println(“x is even”);} • boolean b = (x%2 == 0);if (!b){ System.out.println(“x is odd”);}
If-Else • If – else statement • if (BOOLEAN){ ACTIONS 1}else{ ACTIONS 2} Only ACTIONS 1 or ACTIONS 2 get done. Always one, never both!
Practice int x = 2; int y = 4; int z = 3; if (x !=3){y = x+4; } if (z > y-1){ x = x*3; } else{ x = x/2; }
Conditionals • If and if-else statements are called conditional statements • They denote blocks of code that are only executed in a certain condition. • Conditionals can be put inside one another – this is called nesting or embedding conditionals.
Flow of Control • There is an order to program commands (usually top to bottom) • Some statements alter which commands get done next • Flow of Control refers to the order of commands. • Conditionals alter flow of control.
Example int n = 3; int d = 5; if (d > n+1){n = n+5; if (d >n){ d = d +1;}else{ n = 0;} } else{d = 0; } System.out.println(d); System.out.println(n);
Example n : 3 d : 5 int n = 3; int d = 5; if (d > n+1){n = n+5; if (d >n){ d = d +1;}else{ n = 0;} } else{d = 0; } System.out.println(d); System.out.println(n); 5>4? True
Example n : 3 d : 5 int n = 3; int d = 5; if (d > n+1){n = n+5; if (d >n){ d = d +1;}else{ n = 0;} } else{d = 0; } System.out.println(d); System.out.println(n); 5>4? True
Example n : 3 8 d : 5 int n = 3; int d = 5; if (d > n+1){n = n+5; if (d >n){ d = d +1;}else{ n = 0;} } else{d = 0; } System.out.println(d); System.out.println(n); 5>8? False!
Example n : 3 8 d : 5 int n = 3; int d = 5; if (d > n+1){n = n+5; if (d >n){ d = d +1;}else{ n = 0;} } else{d = 0; } System.out.println(d); System.out.println(n); 5>8? False!
Example n : 3 8 0 d : 5 int n = 3; int d = 5; if (d > n+1){n = n+5; if (d >n){ d = d +1;}else{ n = 0;} } else{d = 0; } System.out.println(d); System.out.println(n);
Else-if • Else-if : Way to say one of many if (BOOLEAN){ACTION } else if(BOOLEAN2){ACTION2 } else{ ACTION3 }
Example double x = -2; double y = 3; if (x>y){x = 0; } else if (x < 0){y = 6; } else if (y < 0){x = 7; } else{y = 1; x = 2; }
Braces • Braces are embedded as well. You must match them up. • Correct indentation helps. • Example: int n = 3; int d = 5; if (n > 0){ if (d > n+1){ n = n+5;} else{ n = 0; if (d >n){ d = d +1; }}}
String Equality • For primitives (not objects) use == if (x == 3){ • For Strings, use .equals String s = JOptionPane… if (s.equals(“Happy”)){