Lecture 4 conditionalsAndLoops_4up

Lecture 4 conditionalsAndLoops_4up - Flow of Control CMPT...

Info icon This preview shows pages 1–3. Sign up to view the full content.

CMPT 125: Lecture 4 Conditionals and Loops Tamara Smyth, [email protected] School of Computing Science, Simon Fraser University January 17, 2009 1 Flow of Control The order in which statements are executed is called the flow of control . Unless otherwise specified, the flow of control is linear, that is, statements are executed in the order in which they occur. Java begins a program on the first statement of the main method. A change in the flow of control can be accomplished by: invoking a method conditional statements loops or some combination. CMPT 125: Conditionals and Loops, Lecture 4 2 Conditional Statements A conditional statement allow us to choose which statement will be executed next. Java has three conditional statements: 1. if-else 2. else if 3. switch These statements allow us to decide which statement to execute next based on a condition , that is, the outcome of a boolean expression. CMPT 125: Conditionals and Loops, Lecture 4 3 Boolean Expressions Recall that a boolean is a data type that evaluates to either true or false. A boolean statement also evaluates to true or false, and may consist solely of a single variable, or a larger statement involving a logical operator. boolean hungry = false; //hungry is a boolean if (hungry) System.out.println(’’Let’s go eat!’’); else system.out.println(’’Let’s go for a drink instead.’’); The if statement allows a program to choose whether to execute a particular statement. if (num > 20) System.out.println(’’Count is over 20.’’); else system.out.println(’’Count is less than or equal to 20.’’); CMPT 125: Conditionals and Loops, Lecture 4 4
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Evaluating Boolean Expressions Boolean expressions are evaluated using: equality operators: == test whether two values ARE equal != test whether two values ARE NOT equal relational operators: > test whether one value is greater than another < test whether one value is less than another >= greater than or equal <= less than or equal logical operators: ! logical NOT && logical AND || logical OR Logical operators take boolean operands CMPT 125: Conditionals and Loops, Lecture 4 5 Logical NOT The logical NOT is unary, and has only two possible values: true or false. boolean hungry = false; if (!hungry) system.out.println(’’Let’s go for a drink instead.’’); OUTPUT: Let’s go for a drink instead. The ! is used to perform the logical NOT, or logical complement , that is, it returns the opposite value. boolean hungry = true; if (!hungry) System.out.println(’’Let’s go for a drink instead.’’); else System.out.println(’’Let’s go eat.’’); OUTPUT: Let’s go eat. CMPT 125: Conditionals and Loops, Lecture 4 6 Logical AND, OR Logical AND, OR are binary operators that use two operands, both of which are boolean.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.
  • Summer '08
  • Boolean Algebra, Foreach, Do while loop

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern