Boolean Expressions & the ‘if’ Statement

Slides:



Advertisements
Similar presentations
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Advertisements

1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Logical Operators and Conditional statements
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements
Decision Structures and Boolean Logic
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3: Program Statements
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Control statements Mostafa Abdallah
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
CprE 185: Intro to Problem Solving (using C)
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Conditionals & Boolean Expressions
Topics The if Statement The if-else Statement Comparing Strings
Conditionals & Boolean Expressions
Logical Operators & Truth Tables.
Chapter 3: Program Statements
Conditionals and Loops
The ‘while’ Statement September 27, 2006
Chapter 3: Program Statements
CS139 October 11, 2004.
Logic of an if statement
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Conditionals and Loops
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Comparing Data & the ‘switch’ Statement
Outline Software Development Activities
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Conditionals and Loops
Boolean Expressions & the ‘if’ Statement
CprE 185: Intro to Problem Solving (using C)
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Boolean Expressions & the ‘if’ Statement September 22, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev © 2004 Pearson Addison-Wesley. All rights reserved

HW4 is out Due next Friday It is shorter than normal (programs only) Electronic submission only No class next Friday © 2004 Pearson Addison-Wesley. All rights reserved

Chapter 5 Sections 5.1 – 5.2

Flow of Control Unless specified otherwise, 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 These decisions are based on boolean expressions (or conditions) that evaluate to true or false The order of statement execution is called the flow of control © 2004 Pearson Addison-Wesley. All rights reserved

Method Control Flow If the called method is in the same class, only the method name is needed myMethod(); myMethod compute © 2004 Pearson Addison-Wesley. All rights reserved

Method Control Flow The called method is often part of another class or object obj.doIt(); main doIt helpMe helpMe(); © 2004 Pearson Addison-Wesley. All rights reserved

Encapsulation An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client The client invokes the interface methods of the object, which manages the instance data Methods Data Client © 2004 Pearson Addison-Wesley. All rights reserved

Conditional Statements A conditional statement lets us choose which statement will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions The Java conditional statements are the: if statement if-else statement switch statement © 2004 Pearson Addison-Wesley. All rights reserved

The if Statement The if statement has the following syntax: The condition must be a boolean expression. It must evaluate to either true or false. if is a Java reserved word if ( condition ) statement; If the condition is true, the statement is executed. If it is false, the statement is skipped. © 2004 Pearson Addison-Wesley. All rights reserved

Logic of an if statement condition evaluated false statement true © 2004 Pearson Addison-Wesley. All rights reserved

Boolean Expressions 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 the equality operator (==) and the assignment operator (=) © 2004 Pearson Addison-Wesley. All rights reserved

The if Statement An example of an if statement: if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First the condition is evaluated -- the value of sum is either greater than the value of MAX, or it is not 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 © 2004 Pearson Addison-Wesley. All rights reserved

Example: Age.java (page 208) © 2004 Pearson Addison-Wesley. All rights reserved

Indentation The statement controlled by the if statement is indented to indicate that relationship The use of a consistent indentation style makes a program easier to read and understand Although it makes no difference to the compiler, proper indentation is crucial "Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding © 2004 Pearson Addison-Wesley. All rights reserved

The if Statement 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 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 The precedence of the arithmetic operators is higher than the precedence of the equality and relational operators © 2004 Pearson Addison-Wesley. All rights reserved

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 Logical NOT is a unary operator (it operates on one operand) Logical AND and logical OR are binary operators (each operates on two operands) © 2004 Pearson Addison-Wesley. All rights reserved

Logical NOT The logical NOT operation is also called logical negation or logical complement If some boolean condition a is true, then !a is false; if a is false, then !a is true Logical expressions can be shown using a truth table a !a true false © 2004 Pearson Addison-Wesley. All rights reserved

Logical AND and Logical OR The logical AND expression a && b is true if both a and b are true, and false otherwise The logical OR expression a || b is true if a or b or both are true, and false otherwise © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

Logical Operators A truth table shows all possible true-false combinations of the terms Since && and || each have two operands, there are four possible combinations of conditions a and b a b a && b a || b true false © 2004 Pearson Addison-Wesley. All rights reserved

Boolean Expressions Specific expressions can be evaluated using truth tables total < MAX found !found total < MAX && !found false true © 2004 Pearson Addison-Wesley. All rights reserved

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 if (count != 0 && total/count > MAX) System.out.println ("Testing…"); This type of processing must be used carefully © 2004 Pearson Addison-Wesley. All rights reserved

The if-else Statement An else clause can be added to an if statement to make an if-else statement if ( condition ) statement1; else statement2; 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 © 2004 Pearson Addison-Wesley. All rights reserved

Example: Wages.java (page 211) © 2004 Pearson Addison-Wesley. All rights reserved

Logic of an if-else statement condition evaluated false statement2 statement1 true © 2004 Pearson Addison-Wesley. All rights reserved

THE END © 2004 Pearson Addison-Wesley. All rights reserved