Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control statements Mostafa Abdallah

Similar presentations


Presentation on theme: "Control statements Mostafa Abdallah"— Presentation transcript:

1 Control statements Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

2 Agenda Conditional Statements  If.  If else.  Switch. 3-2

3 Conditional Statements 3-3

4 3-4 The order of statement execution through a method is linear: one statement after another in sequence. Some programming statements allow us to:  Decide whether or not to execute a particular statement.  Execute a statement over and over, repetitively. Conditional Statements

5 Continued. The Java conditional statements are the:  if statement.  if-else statement.  switch statement. 3-5

6 The if Statement 3-6

7 if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If condition is true: statement is executed. If condition is false: statement is skipped. The if Statement The if statement has the following syntax: 3-7

8 condition evaluated statement true false Logic of an if statement 3-8

9 A condition often uses one of Java's equality operators or relational operators, which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between ( == ) and ( = ). Boolean Expressions 3-9

10 An example of an if statement: Example of if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum);  First the condition is evaluated.  If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped.  Either way, the call to println is executed next. 3-10

11 Continued. What do the following statements do? if (top >= MAXIMUM) top = 0; Sets top to zero if the current value of top is greater than or equal to the value of MAXIMUM 3-11 Example of if Statement

12 Continued. What do the following statements do? if (total != stock + warehouse) inventoryError = true; Sets a flag to true if the value of total is not equal to the sum of stock and warehouse  Arithmetic operators is higher in precedence than the equality and relational operators. 3-12 Example of if Statement

13 Logical Operators Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR They all take boolean operands and produce boolean results 3-13

14 Logical Not This operation is also called logical negation or logical complement. If some boolean condition:  if a is true, then !a is false;  if a is false, then !a is true; Logical expressions shown using a truth table 3-14 a!a truefalse true

15 Logical AND The logical AND expression: a && b Is true if both a and b are true, and false otherwise 3-15 aba && b true false truefalse

16 Logical OR The logical OR expression: a || b is true if a or b or both are true, and false otherwise. 3-16 aba || b true falsetrue falsetrue false

17 Logical Operators Expressions that use logical operators can form complex conditions. if (total < MAX + 5 && !found) System.out.println ("Processing…");  All logical operators have lower precedence than the relational operators  Logical NOT has higher precedence than logical AND and logical OR. 3-17

18 Short-Circuited Operators The processing of logical AND and logical OR is short-circuited. If the left operand is sufficient to determine the result, the right operand is not evaluated.  This type of processing must be used carefully 3-18 if (count != 0 && total/count > MAX) System.out.println ("Testing…");

19 The if-else Statement 3-19

20 The if-else Statement An else clause can be added to an if statement to make an if-else statement.  If the condition is true, statement1 is executed; if the condition is false, statement2 is executed  One or the other will be executed, but not both 3-20 if ( condition ) statement1; else statement2;

21 Logic of an if-else statement 3-21 condition evaluated statement1 true false statement2

22 If-else Statement Example if (radius >= 0) { area = radius * radius * 3.14159; System.out.println("The area for the “ + “circle of radius " + radius + " is " + area); } else { System.out.println("Negative input"); } 3-22

23 Nested if Statements 3-23

24 The switch Statement 3-24

25 The switch Statement The switch statement provides another way to decide which statement to execute next. The switch statement evaluates an expression, then attempts to match the result to one of several possible cases. 3-25

26 The switch Statement Continued. Each case contains one value (a constant) and a list of statements. The flow of control transfers to statement associated with the first case value that matches. 3-26

27 Switch Syntax The general syntax of a switch statement is: 3-27 switch and case are reserved words switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } If expression matches value2, control jumps to here

28 Switch example An example of a switch statement: 3-28 switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; }

29 Switch rules A break statement is used as the last statement in each case's statement list. A break statement causes control to transfer to the end of the switch statement. If a break statement is not used, the flow of control will continue into the next case. 3-29

30 Switch rules Continued. A switch statement can have an optional default case. The default case has no associated value and simply uses the reserved word default. If the default case is present, control will transfer to it if no other case value matches. 3-30

31 Switch rules Continued. If there is no default case, and no other value matches, control falls through to the statement after the switch. The implicit boolean condition in a switch statement is equality 3-31

32 Switch rules Continued. The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int, long) or a char. It cannot be a boolean value or a floating point value (float or double). You cannot perform relational checks with a switch statement. 3-32

33 Questions

34 Thanks


Download ppt "Control statements Mostafa Abdallah"

Similar presentations


Ads by Google