Conditional Statements

Similar documents
Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

Java I/O and Control Structures

Java I/O and Control Structures Algorithms in everyday life

Algorithms and Conditionals

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I

Conditionals and Loops

CSC 1051 Data Structures and Algorithms I

Selection and Repetition Revisited

Selection and Repetition

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Basics of Java Programming variables, assignment, and input

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection and Repetition Revisited

CSC 1051 Data Structures and Algorithms I

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Chapter 3. Selections

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection and Repetition

Lecture 3: Variables and assignment

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

Selection Statements and operators

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

Selection Statements and operators

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

More Programming Constructs -- Introduction

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

BRANCHING if-else statements

2: Basics of Java Programming

Introduction to Arrays

Conditional Programming

Selections. CSE 114, Computer Science 1 Stony Brook University

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Iterators and File Input

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Arrays, Part 3: Multidimensional arrays

Introduction to Bioinformatics

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Information Science 1

CMPT 125: Lecture 4 Conditionals and Loops

Chapter 4: Conditionals and Loops

COSC The if Statement. Yves Lespérance. Lecture Notes Week 5 Control Structures

Flow of Control. Conditional Statements. Conditional Statements. CS256 Computer Science I Kevin Sahr, PhD. Lecture 15: The If-Statement

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Computational Expression

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Information Science 1

Flow Control. So Far: Writing simple statements that get executed one after another.

Operators in java Operator operands.

Conditions, logical expressions, and selection. Introduction to control structures

CT 229 Java Syntax Continued

COMP Primitive and Class Types. Yi Hong May 14, 2015

CS 106 Introduction to Computer Science I

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Java Flow of Control

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

CS112 Lecture: Making Choices

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Chapter 4: Control Structures I (Selection)

Chapter 4: Conditionals and Loops

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Control Structures: if and while A C S L E C T U R E 4

CS 117 Fall Compound boolean expressions. Control Statements, Part 2. Using boolean operators. Boolean operators

Topics. Chapter 5. Equality Operators

CSC 220: Computer Organization Unit 10 Arithmetic-logic units

Conditional Statement

Operators. Java operators are classified into three categories:

The Vic Class. a1 b e g end. a d2 end. a3 b e g3 h3 end. a d4 e4 end. GarthB LyleL stack

Name Section: M/W T/TH Number Definition Matching (8 Points)

Handout 4 Conditionals. Boolean Expressions.

Name Section: M/W T/TH Number Definition Matching (6 Points)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Data Representation Classes, and the Java API

Chapter 4: Conditionals and Loops

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

Data Representation and Applets

Control Structures: The IF statement!

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

In this chapter, you will:

Chapter 4 The If Then Statement

1007 Imperative Programming Part II

CS302: Self Check Quiz 2

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Data Representation and Applets

Transcription:

Conditional Statements CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus

Control flow Sequence of statements that are actually executed in a program statement 1 statement 2 statement 3 statement 4

Control flow Sequence of statements that are actually executed in a program Conditional and Repetition statements: enable us to alter control flow statement 1 statement 1 statement 2 true statement 2 condition 1 false statement 3 condition 2 true statement 4 statement 4 false statement 3 This slide dapted from Wayne&Sedgewick Princeton course: http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php

Control flow Sequence of statements that are actually executed in a program Example:. input qp. statement input. credits 2. statement gpa. = qp / credits 3. statement. print gpa 4. statement print. goodbye 4. message

Control flow Sequence of statements that are actually executed in a program Example: Example. input qp. statement input. credits 2.. input qp. statement input. credits 2. statement gpa. = qp / credits 3. statement. print gpa 4. true print. no GPA. yet credits is zero? false statement gpa. = qp / credits 3. statement. print gpa 4. statement print. goodbye 4. message statement print. goodbye 4. message

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

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 (=)

Conditional statements if ( condition ) statement; // no else clause if ( condition ) statement1; else statement2; condition evaluated false condition evaluated false true true statement statement 1 statement2

Example: How do we fix output to use singular/plural as appropriate? For example: Enter the total amount to be given as change: 18 That amount can be given as: 0 quarters get rid of this! 1 dimes 1 nickels 3 pennies

Example: 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 two destinations should be suggested depending on whether person is under 30. For example, a run of the program might look like this: How old is the traveler > 15 You should consider Hershey Park.

Nested loops statement 1 statement 1; if (condition 1) true condition 1 false statement 2; statement 2 else if (condition 2) condition 2 true statement 4 statement 4; false else statement 3; statement 3 statement 5; statement 5 This slide dapted from Wayne&Sedgewick Princeton course: http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php

Another example: 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.

Java Logical Operators logical operators can be used with boolean operands to express more complex boolean conditions:! Logical NOT && Logical AND Logical OR

Vacation example revisited: 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 variables: boolean aboveagelimit = false; boolean useplural = hours > 1; boolean expression using a relational operator

Example total 103 if (total < MAX+5 &&!found) System.out.println ("Processing "); MAX 100 found false All logical operators have lower precedence than the relational operators The! operator has higher precedence than && and

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 false 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

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 a b a && b a b true true true true true false false true false true false true false false false false

Quick Check 1 What do the following statements do? if (total!= stock + warehouse) inventoryerror = true; total 20 stock 8 warehouse 12 inventoryerror false if (found!done) System.out.println("Ok"); found false done true

Quick Check 2 Try again with different values if (total!= stock + warehouse) inventoryerror = true; total 20 stock 7 warehouse 12 inventoryerror false if (found!done) System.out.println("Ok"); found false done false

Quick Check 3 Try again with different values if (total!= stock + warehouse) inventoryerror = true; total 20 stock 8 warehouse 12 inventoryerror true if (found!done) System.out.println("Ok"); found true done false

Boolean Expressions using truth tables let s try this one: found done!done found!done false false false true true false true true

Boolean Expressions using truth tables another example: total < MAX found!found total < MAX &&!found false false false true true false true true

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.");

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

More examples in textbook, Section 5.2 http://www.csc.villanova.edu/~map/1051/chap05/age.java http://www.csc.villanova.edu/~map/1051/chap05/wages.java http://www.csc.villanova.edu/~map/1051/chap05/guessing.java http://www.csc.villanova.edu/~map/1051/chap05/minofthree.java