Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.

Similar presentations


Presentation on theme: "Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only."— Presentation transcript:

1 Conditional statements and Boolean expressions

2 The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only if a given condition is satisfied An if-statement makes sure that the correct condition is satisfied before a certain action is performed

3 The if-statement in Java (2) Syntax of the if-statement: The keyword if announces (to the Java compiler) that we started an if-statement A conditional clause ( CONDITION ) follows the keyword if Following the condition clause, you can write (only) one statement

4 The condition clause of the if- statement (1) The condition clause of the if-statement is an expression that evaluates to true or false Expressions that evaluates to true or false are known in Computer Science as: Boolean expressions Example: a < 0

5 The condition clause of the if- statement (2)

6 Multiple statements in the "then"- part of the if-statement (1) Block = a pair of "{" and "}" braces that groups components in a Java program together

7 Multiple statements in the "then"- part of the if-statement (2) Statement block = multiple statements grouped together by braces {.... } A statement block in Java is considered as one (single) statement

8 Representation techniques: Flow chart (1) A flow chart is a diagram that represents a computer algorithm. It uses a rectangular box to represent an assignment statement It uses a diamond box to represent a condition Lines are used to indicate the flow of the steps in the algorithm

9 Representation techniques: Flow chart (2)

10 Representation techniques: Structure diagram(1) A structure diagram is another type of diagram to represent a computer algorithm. It uses different shapes of boxes to represent assignment statements and conditions The structure diagram does not use lines

11 Representation techniques: Structure diagram(2)

12 The Boolean (logical) data type boolean The boolean data type is a built-in (primitive) data type of Java is used to represent the logical values There are 2 logical values: true and false Encoding scheme used in the boolean data type: 0 represents false 1 represents true uses 1 byte of memory (to store 0 or 1)

13 Boolean literals There are 2 boolean literals (= logical constants) in Java: These 2 words are keywords (reserved words) in Java

14 Defining boolean typed variables (1) Syntax to define an boolean typed variable: boolean NameOfVariable ; The keyword boolean announces the variable definition clause The NameOfVariable is an identifier which is the name of the variable. The variable definition clause is must be ended with a semi- colon ";“ A boolean typed variable can store true (1) or false (0)

15 Defining boolean typed variables (2)

16 Operations that return a boolean result 1. Compare operators: A compare operator compares 2 numerical expressions and return a Boolean result. 2. Logical operators: A logical operator compares 2 Boolean (logical) expressions and return a Boolean result

17 Compare operators (1) A compare operator will return the value true if the test is successful A compare operator will return the value false if the test is unsuccessful The same automatic conversion rules used for arithmetic operators apply for compare operators

18 Compare operators (3) you can assume that all compare operators have the same priority

19 Compare operators (4)

20 Logical operators (1)

21 Logical operators (2)

22 Logical operators (3)

23 Logical operators (4)

24 Programming trick: test if a number is between 2 numbers (1) The program prints "yes" when 10 ≤ a ≤ 20 It is illegal to use the comparison operator "<=" on a Boolean value and a number

25 Programming trick: test if a number is between 2 numbers (2)

26 Programming trick: test if a number is between 2 numbers (3)


Download ppt "Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only."

Similar presentations


Ads by Google