1 / 22

Booleans and Selection Statements

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;

julian-best
Download Presentation

Booleans and Selection Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Booleans and Selection Statements

  2. 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;

  3. Comparison Operators for Numbers • Used to hold the value of comparisons: int x = 2; int y = 1; boolean b = x < y; • Other operators:<, >, <=, >=, !=, ==

  4. 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!

  5. 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;

  6. Example Program: Is the number even or odd? public boolean isEven(int myNum){ boolean even = (myNum%2) == 0; return even; }

  7. Simple If Statement • New type of statement: if-statement • Only take a certain action if something is true. • if(BOOLEAN){ ACTIONS;}

  8. 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”);}

  9. If-Else • If – else statement • if (BOOLEAN){ ACTIONS 1}else{ ACTIONS 2} Only ACTIONS 1 or ACTIONS 2 get done. Always one, never both!

  10. 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; }

  11. 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.

  12. 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.

  13. 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);

  14. 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

  15. 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

  16. 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!

  17. 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!

  18. 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);

  19. Else-if • Else-if : Way to say one of many if (BOOLEAN){ACTION } else if(BOOLEAN2){ACTION2 } else{ ACTION3 }

  20. 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; }

  21. 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; }}}

  22. String Equality • For primitives (not objects) use == if (x == 3){ • For Strings, use .equals String s = JOptionPane… if (s.equals(“Happy”)){

More Related