1 / 15

Flow of Control

Flow of Control. Flow of Control. The Flow of control is the order in which a program performs actions . Up to this point, the order has been sequential . A branching statement chooses between two or more possible actions .

dustin
Download Presentation

Flow of Control

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. Flow of Control

  2. Flow of Control • The Flow of control is the order in which a program performs actions. • Up to this point, the order has been sequential. • Abranching statementchooses between two or more possible actions. • Note that a loop statement repeats an action until a stopping condition occurs.

  3. if-else statement (Cont.) • It is a branching statement that chooses between two possible actions. • Syntax: if (Boolean_Expression) Statement_1 else Statement_2 • Example:

  4. if-else statement (Cont.)

  5. More one if-else • To include multiple statements in a branch, enclose the statements in braces. if (count < 3) { total = 0; count = 0; } • Omitting else: • The value of a booleanexpression is either true or false, for instance, count < 3.

  6. Java Comparison Operators

  7. Compound Boolean Exp. • Boolean expressions can be combined using “and”, i.e., &&. (sub_expression_1) && (sub_expression_2) • if ((score > 0) && (score <= 100))not allowed if (0 < score <= 100) • The larger expression is true only when both of the smaller expressions are true. • Boolean expressions can be combined using the “or”, i.e., ||. (sub_expression_1) || (sub_expression_2) • if ((quantity > 5) || (cost < 10)) • The larger expression is true when either of the smaller expressions is true or bothof the smaller expressions are true.

  8. Negating a Boolean Exp. • A boolean expression can be negated using the "not”, i.e., !. !(boolean_expression) • (a || b) && !(a && b) • which is the exclusive or • Avoiding the negation operator:

  9. Java Logical Operators

  10. Using Equality • Appropriatefor determining if two integers or characters have the same value. • if (a == 3)where a is an integer type. • Not appropriate for determining if two floating points values are equal. Use < and some appropriate tolerance. • if (abs(b - c) < epsilon)where b, c, epsilonare floating point types. • Not suitablefor defining if 2 objects have the same value. • if (s1 == s2), where s1 and s2 refer to strings, determines only if s1 and s2 refer the a common memory location. • (s1 == s2) is false, if s1 and s2 refer to strings with identical sequences of characters, but stored in different memory locations.

  11. Equality For Objects • To test the equality for objects of String, use method equals. s1.equals(s2) or s2.equals(s1) • To test for equality ignoring case, use equalsIgnoreCase. ("Hello".equalsIgnoreCase("hello")) • Syntax: String.equals(Other_String) String.equalsIgnoreCase(Other_String)

  12. Lexicographic Order • The lexicographic order is similar to alphabetical order, but is it based on the order of the characters in the ASCII table, a.k.aunicode, (see page 932). • All the digits come before all the letters. • The uppercase letters come before the lowercase letters. • Strings consisting of alphabetical characters can be compared using method compareTo and method toUpperCase or method toLowerCase.

  13. Lexicographic Order (Cont.) • Example. String s1 = "Hello"; String lowerS1 = s1.toLowerCase(); String s2 = "hello"; if (lowerS1.compareTo(s2) == 0) System.out.println("Equal!"); • Syntax: String_1.compareTo(String_2) • MethodcompareToreturns • a negative number if String_1 precedes String_2 • zero if the two strings are equal • a positive number of String_2 precedes String_1.

More Related