CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.

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.
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
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
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:
Chapter 3: Program Statements
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
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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:
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Control statements Mostafa Abdallah
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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)
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Boolean Expressions & the ‘if’ Statement
Logical Operators & Truth Tables.
Chapter 3: Program Statements
Outline Boolean Expressions The if Statement Comparing Data
Logic of an if statement
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)
Conditionals and Loops
Boolean Expressions & the ‘if’ Statement
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
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:

CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 – Algorithms and Data Structures I

Conditional statements alter the linear flow of control. They use boolean expressions to determine what to do next. Example: if (credits == 0) System.out.println ("GPA: None"); else { gpa = qp / credits; System.out.println ("\n\tGPA: " + gpa); } CSC 1051 M.A. Papalaskari, Villanova University A boolean expression Review if true, do this if more than one statement, enclose in braces if false, do these

Conditional statements condition evaluated statement 1 true false statement2 CSC 1051 M.A. Papalaskari, Villanova University condition evaluated statement true false if ( condition ) statement1 ; else statement2 ; if ( condition ) statement ; // no else clause

Another example: CSC 1051 M.A. Papalaskari, Villanova University Create an application called Vacation that prompts for and inputs an integer representing someone’s age and then suggests an appropriate vacation destination. One of three destinations should be suggested depending on whether the answer is less than 20, between 20 and 50, or over 50. For example, a run of the program might look like this: How old is the traveler > 15 You should consider Hershey Park.

Boolean Expressions The reserved words true and false are the only valid values for a boolean type Example boolean declarations: A boolean expression using a relational operator CSC 1051 M.A. Papalaskari, Villanova University boolean aboveAgeLimit = false; boolean usePlural = hours > 1; Review

Java relational operators relational operators can be used with numeric types and produce 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 ( = ) CSC 1051 M.A. Papalaskari, Villanova University Review

Java Logical Operators logical operators can be used with boolean operands to express more complex boolean conditions: ! Logical NOT && Logical AND || Logical OR CSC 1051 M.A. Papalaskari, Villanova University

Example if (total < MAX+5 && !found) System.out.println ("Processing…"); CSC 1051 M.A. Papalaskari, Villanova University All logical operators have lower precedence than the relational operators The ! operator has higher precedence than && and || total 103 MAX 100 found false

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: CSC 1051 M.A. Papalaskari, Villanova University a!a truefalse true

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 CSC 1051 M.A. Papalaskari, Villanova University

Logical AND and Logical OR 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 CSC 1051 M.A. Papalaskari, Villanova University aba && ba || b true false true falsetruefalsetrue false

Quick Check 1 CSC 1051 M.A. Papalaskari, Villanova University What do the following statements do? if (total != stock + warehouse) inventoryError = true; if (found || !done) System.out.println("Ok"); total 20 stock 8 8 found false done true warehouse 12 inventoryerror false

Quick Check 2 CSC 1051 M.A. Papalaskari, Villanova University Try again with different values if (total != stock + warehouse) inventoryError = true; if (found || !done) System.out.println("Ok"); total 20 stock 7 7 found false done false warehouse 12 inventoryerror true

Quick Check 3 CSC 1051 M.A. Papalaskari, Villanova University Try again with different values if (total != stock + warehouse) inventoryError = true; if (found || !done) System.out.println("Ok"); total 20 stock 8 8 found true done false warehouse 12 inventoryerror true

Boolean Expressions using truth tables – let’s try this one: CSC 1051 M.A. Papalaskari, Villanova University founddone!donefound || !done false true false true

Boolean Expressions CSC 1051 M.A. Papalaskari, Villanova University total < MAXfound!foundtotal < MAX && !found false true false true using truth tables – another example:

How much of a boolean expression do we need to evaluate before determining its value? *** Short-Circuited Operators The processing of && and || is “short-circuited” in cases where the left operand is sufficient to determine the result (the right operand is not evaluated at all) This can be both useful and dangerous! if (count != 0 && total/count > MAX) System.out.println ("Testing."); CSC 1051 M.A. Papalaskari, Villanova University

Indentation Revisited Remember that indentation is for the human reader, and is ignored by the computer if (total > MAX) System.out.println ("Error!!"); errorCount = errorcount + 1;; Despite what is implied by the indentation, the increment will occur whether the condition is true or not CSC 1051 M.A. Papalaskari, Villanova University

More examples in textbook, Section 5.2 CSC 1051 M.A. Papalaskari, Villanova University Age.java Age.java Wages.java Guessing.java MinOfThree.java