branching and boolean expressions
play

Branching and Boolean Expressions Roman Kontchakov / Carsten Fuhs - PowerPoint PPT Presentation

Software and Programming I Branching and Boolean Expressions Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline The if statement Comparing numbers and strings Nested branches Boolean variables and expressions Sections


  1. Software and Programming I Branching and Boolean Expressions Roman Kontchakov / Carsten Fuhs Birkbeck, University of London

  2. Outline The if statement Comparing numbers and strings Nested branches Boolean variables and expressions Sections 3.1–3.4, 3.7 Return Statement Section 5.4 SP1 2020-02 1

  3. Java Compilation and JRE compiler source bytecode HelloWorld.java javac HelloWorld.class java public static void running program main(String[] args) { . . . } Virtual Machine (VM) SP1 2020-02 2

  4. Python: n = "World" print("Hello, " + n + "!") My First Program 1 /* HelloWorld.java Purpose: printing a hello message on the screen 2 3 */ 4 public class HelloWorld { // each program is a class (week 6) 5 // almost everything in Java is an object 6 public static void main(String[] args) { 7 String n = "World"; 8 System.out.println("Hello, " + n + "!"); 9 } 10 11 } NB. watch out for semicolons — they are compulsory NB. names and reserved words are case-sensitive SP1 2020-02 3

  5. Python: def sq(x): Python code Methods A method is a named sequence of instructions method name parameter (type and name) public static int sq(int x) { type of return value Java code (sequence of instructions) } void means the method does not return any value Parameter values are supplied when a method is called The return value is the result that the method computes Method ≈ algorithm ≈ function (in Python) NB: until week 6, all methods will be public static SP1 2020-02 4

  6. Example 2: y = x 2 as a Method 1 public class PrintSquares { public static void main(String[] args) { 2 printSquare(7); 3 printSquare(9); 4 } 5 public static int sq(int x) { // x is a parameter 6 int y = x * x; // compute xˆ2 7 return y; // return the value 8 } 9 public static void printSquare(int n) { 10 System.out.println(n + "ˆ2=" + sq(n)); 11 } 12 7ˆ2=49 13 } the output: 9ˆ2=81 SP1 2020-02 5

  7. Method Call Stack main main main main main main printSquare( 7 ); printSquare(7); printSquare(7); printSquare(7); printSquare(7); printSquare(7); args args args args args args printSquare printSquare printSquare printSquare printSquare public static void printSquare(int n) { public static void printSquare(int n) { public static void printSquare(int n) { public static void printSquare(int n) { public static void printSquare(int n) { System.out.println(n + "ˆ2=" + sq(n) ); System.out.println(n + "ˆ2=" + sq(n) ); System.out.println(n + "ˆ2=" + sq(n) ); System.out.println(n + "ˆ2=" + sq(n) ); n n n n n 7 7 7 7 7 sq sq sq public static int sq(int x) { public static int sq(int x) { public static int sq(int x) { x x x 7 7 7 int y = x * x; int y = x * x; y y y 0 49 49 return y; } SP1 2020-02 6

  8. Method Call Stack 7ˆ2=49 7ˆ2=49 7ˆ2=49 7ˆ2=49 7ˆ2=49 7ˆ2=49 7ˆ2=49 7ˆ2=49 main main main main main main main main printSquare(7); printSquare(7); printSquare(7); printSquare(7); printSquare(7); printSquare(7); printSquare(7); printSquare(7); 9ˆ2=81 args args args args args args args printSquare(9); printSquare(9); printSquare(9); printSquare( 9 ); args printSquare(9); printSquare(9); printSquare(9); printSquare printSquare printSquare printSquare printSquare printSquare printSquare public static void printSquare(int n) { public static void printSquare(int n) { public static void printSquare(int n) { public static void printSquare(int n) { public static void printSquare(int n) { public static void printSquare(int n) { public static void printSquare(int n) { System.out.println(n + "ˆ2=" + sq(n) ); System.out.println(n + "ˆ2=" + sq(n) ); System.out.println(n + "ˆ2=" + sq(n) ); System.out.println(n + "ˆ2=" + sq(n) ); System.out.println(n + "ˆ2=" + sq(n) ); System.out.println(n + "ˆ2=" + sq(n) ); n n n n n n n 7 9 9 9 9 9 9 } } sq sq sq public static int sq(int x) { public static int sq(int x) { public static int sq(int x) { evaluated to 49 x x x evaluated to 81 9 9 9 int y = x * x; int y = x * x; y y y 81 81 0 return y; } SP1 2020-02 7

  9. Method Call Stack 7ˆ2=49 main printSquare(7); 9ˆ2=81 args printSquare(9); SP1 2020-02 8

  10. Return Statement The return statement (1) terminates a method call (2) yields the method result 1 public static double cubeVolume(double sideLength) { if (sideLength < 0) 2 return 0; 3 // more code 4 return sideLength * sideLength * sideLength; 5 6 } NB: if a method has no return value ( void ), then it can contain return; only (without any value) SP1 2020-02 9

  11. The if Statement The if statement allows a program to carry out different ac- tions depending on the nature of the data to be processed floor floor actual floor actual floor 1 Scanner s = new Scanner(System.in); 14 14 2 int floor = s.nextInt(); 14 14 15 15 13 13 11 11 12 12 3 int actualFloor; 12 12 9 9 10 10 4 if (floor > 13) { 11 11 7 7 8 8 actualFloor = floor - 1; 10 10 5 9 9 5 5 6 6 6 } 8 8 3 3 4 4 7 else { 7 7 1 1 2 2 actualFloor = floor; 6 6 8 5 5 9 } 4 4 10 System.out.println("Actual floor: " + 3 3 actualFloor); 11 2 2 SP1 2020-02 10 1 1

  12. Statement Blocks A block is a group of 0 or more statements between balanced { and } (in Python: a group of statements indented at the same level) A block can be used anywhere a single statement is allowed the following is equivalent to the code on p. 10, lines 3–9 1 int actualFloor; 2 if (floor > 13) actualFloor = floor - 1; 3 4 else actualFloor = floor; 5 SP1 2020-02 11

  13. The Else Branch is Optional The else branch is optional 1 int discountedPrice = originalPrice; 2 if (originalPrice > 100) discountedPrice = originalPrice - 10; 3 is equivalent to 1 int discountedPrice; 2 if (originalPrice > 100) discountedPrice = originalPrice - 10; 3 4 else discountedPrice = originalPrice; 5 SP1 2020-02 12

  14. If: Common Mistakes ; is a valid Java statement — it does nothing NB: do not put the semicolon after if(...) 1 int discountedPrice = originalPrice; 2 if (originalPrice > 100); { // empty statement in if // LOGICAL ERROR: this block is executed anyway 3 discountedPrice = originalPrice - 10; 4 5 } // it is, however, NOT a syntax (compile) error SP1 2020-02 13

  15. Types of Errors compile-time error : error in syntax (Java grammar) or type detected by the compiler: an error message is produced and no bytecode is generated int i = "string"; //incompatible types run-time error : the program is compiled and runs, but the JVM terminates execution when the error is encountered int i = 1/0; //ArithmeticException: / by zero //StringIndexOutOfBoundsException String s = "tomato".substring(0, -2); logical error : the program is compiled and runs, but the outputs are not as expected SP1 2020-02 14

  16. Constants the reserved word final ensures that the value of the variable never changes: final double BOTTLE VOLUME = 2; use names for constants and avoid “magic numbers” “all uppercase with words separated by ” is a coding convention Java does not check it! SP1 2020-02 15

  17. Comparing Numbers relational operators >, >=, <, <=, ==, != are applicable to int , double and other numerical datatypes NB: NOT ≥ but why not, e.g., => instead of >= ? (assignment operators, week 3) relational operators return boolean values ( true or false ) , which, for example, can be stored in boolean variables 1 int floor = 2; 2 int top = 12; 3 boolean over = floor > top; SP1 2020-02 16

  18. Integer Datatypes sign int 32-bit integers digits (31 bits) hexadecimal literals is 0 0x00000000 is 1 0x00000001 is -1 0xFFFFFFFF is 2,147,483,647 = Integer.MAX VALUE 0x7FFFFFFF is -2,147,483,648 = Integer.MIN VALUE 0x80000000 NB: do not use , in Java as a digits separator, use the underscore ( ) instead: e.g., Integer.MIN VALUE is -2 147 483 648 ? What is the value of Integer.MAX VALUE + 1 ? SP1 2020-02 17

  19. Integer Datatypes (2) int 32-bit integers from -2,147,483,648 to 2,147,483,647 sign digits (31 bits) 64-bit integers from -9,223,372,036,854,775,808 long to 9,223,372,036,854,775,807 sign digits (63 bits) NB: use suffix L for long literals: e.g., 4 000 000 000L sign short 16-bit integers from -32,768 to 32,767 digits (15 bits) sign digits byte 8-bit integers from -128 to 127 (7 bits) (Python 3 uses “arbitrary-precision” integers) SP1 2020-02 18

  20. IEEE Floating Point Numbers double : ± 2 . 23 × 10 − 308 to ± 1 . 80 × 10 308 with approx. 15 decimal digits 3 decimal digits (1000) ≈ 10 binary digits (1024) sign exponent fraction (52 bits) (11 bits) NB: double literals: 12.3 = 1.23e1 = 0. 123 e 2 = 123e-1 0 . 123 × 10 2 123 × 10 − 1 12 . 3 = 1 . 23 × 10 = = float : ± 1 . 18 × 10 − 38 to ± 3 . 4 × 10 38 with approx. 7 decimal digits sign exponent fraction (23 bits) (8 bits) NB: float should never be used for precise values, e.g., currency; use java.math.BigDecimal class instead SP1 2020-02 19

Recommend


More recommend