Lecture 4 conditionalsAndLoops_4up

Lecture 4 conditionalsAndLoops_4up - Flow of Control CMPT...

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

View Full Document Right Arrow Icon
CMPT 125: Lecture 4 Conditionals and Loops Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 17, 2009 1 Flow of Control The order in which statements are executed is called the fow oF control . Unless otherwise specifed, the fow oF control is linear, that is, statements are executed in the order in which they occur. Java begins a program on the frst statement oF the main method. A change in the fow 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Lecture 4 conditionalsAndLoops_4up - Flow of Control CMPT...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online