CS1150 Principles of Computer Science Boolean, Selection Statements

Similar documents
Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 3. Selections

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Selection Statements. Pseudocode

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

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Selections. CSE 114, Computer Science 1 Stony Brook University

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Python - Week 3. Mohammad Shokoohi-Yekta

Chapter 3: Operators, Expressions and Type Conversion

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

Chapter 3 Selection Statements

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Introduction to Object-Oriented Programming

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Operators. Java operators are classified into three categories:

CS1150 Principles of Computer Science Boolean, Selection Statements (Part II)

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

IEEE Floating-Point Representation 1

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

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

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! &&

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

Pace University. Fundamental Concepts of CS121 1

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

Lecture 4: Conditionals

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

Chapter 5. Conditions, Logical Expressions, and Selection Control Structures

Chapter 5 Topics. Chapter 5 Topics. Flow of Control. bool Data Type. Flow of Control. Chapter 5

Lecture 3 Operators MIT AITI

Eng. Mohammed S. Abdualal

Programming with Java

Programming Logic and Design Sixth Edition

JAVA OPERATORS GENERAL

Java enum, casts, and others (Select portions of Chapters 4 & 5)

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Prof. Navrati Saxena TA: Rochak Sachan

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Chapter 4: Basic C Operators

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

CONDITIONAL EXECUTION

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Chapter 4: Control Structures I

QUIZ: What value is stored in a after this

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Programming for Engineers Iteration

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

H212 Introduction to Software Systems Honors

CS Programming I: Branches

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Checking Multiple Conditions

Term 1 Unit 1 Week 1 Worksheet: Output Solution

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Lecture 1 Java SE Programming

Control Structures in Java if-else and switch

CIS 110: Introduction to Computer Programming

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

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

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

CS302: Self Check Quiz 2

In this chapter, you will:

Expressions and Precedence. Last updated 12/10/18

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Chapter 6 Primitive types

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Information Science 1

CS 106 Introduction to Computer Science I

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

CSS 161 Fundamentals of Compu3ng. Flow control (2) October 10, Instructor: Uma Murthy

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Control Structures in Java if-else and switch

Basic Operations jgrasp debugger Writing Programs & Checkstyle

SECTION II: LANGUAGE BASICS

CS111: PROGRAMMING LANGUAGE II

Flow of programming execution control is sequential unless a control structure is used to change that there are 2 general types of

Relational & Logical Operators, if and switch Statements

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

61A Lecture 3. Friday, September 5

++x vs. x++ We will use these notations very often.

Arithmetic and IO. 25 August 2017

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators & Expressions

Transcription:

CS1150 Principles of Computer Science Boolean, Selection Statements Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150

Math Center https://www.uccs.edu/mathcenter/schedules ENGR 233 Computer Science Tutoring Sunday Monday Tuesday Wednesday Thursday Friday 11 am - 3 pm 8 am - 7 pm 8 am - 1:30 pm 4 pm - 7 pm 8 am - 7 pm 8 am - 5 pm 8:30 am - 4:30 pm 2

Review What happens when we create a Scanner? o o Scanner input = new Scanner(System.in); int value = input.nextint(); CS1150

Overview Boolean data type If statement Switch statement CS1150

The boolean Type and Operators Often a program needs to compare two values, such as whether i is greater than j. Java provides six comparison operators (aka relational operators) to compare two values. See next slide. Result of the comparison is a boolean value: true or false. boolean b = (1 > 2); Similar to int, float, double, etc., boolean is just another data type. CS1150

Relational Operators Java Mathematics Name Example Result Operator Symbol (radius is 5) < < less than radius < 0 false <= less than or equal to radius <= 0 false > > greater than radius > 0 true >= greater than or equal to radius >= 0 true == = equal to radius == 0 false!= not equal to radius!= 0 true boolean b = (1 > 2); true and false are boolean literals CS4500/5500

Logical Operators Operator Name Description! not logical negation && and logical conjunction or logical disjunction ^ exclusive or logical exclusion 7

Truth Table for Operator! (not) p!p Example (assume age = 24, weight = 140) true false!(age > 18) is false, because (age > 18) is true. false true!(weight == 150) is true, because (weight == 150) is false. 8

Truth Table for Operator && (and) p 1 p 2 p 1 && p 2 Example (assume age = 24, weight = 140) false false false (age <= 18) && (weight < 140) is false, because both conditions are both false. false true false true false false (age > 18) && (weight > 140) is false, because (weight > 140) is false. true true true (age > 18) && (weight >= 140) is true, because both (age > 18) and (weight >= 140) are true. 9

Truth Table for Operator (or) p 1 p 2 p 1 p 2 Example (assume age = 24, weihgt = 140) false false false false true true (age > 34) (weight <= 140) is true, because (age > 34) is false, but (weight <= 140) is true. true false true (age > 14) (weight >= 150) is true, because (age > 14) is true. true true true 10

Truth Table for Operator ^ (exclusive or) p 1 p 2 p 1 ^ p 2 Example (assume age = 24, weight = 140) false false false (age > 34) ^ (weight > 140) is false, because (age > 34) is false and (weight > 140) is false. false true true (age > 34) ^ (weight >= 140) is true, because (age > 34) is false but (weight >= 140) is true. true false true (age > 14) ^ (weight > 140) is true, because (age > 14) is true and (weight > 140) is false. true true false 11

Short Circuit Evaluation We stop evaluating a boolean expression as soon as its value can be determined This happens when have a compound expressions involving && and && - stop once an expression that evaluated to false is found - stops once an expression that evaluates to true is found Example: Boolean1.java 12

Order of Operators (Section 3.15) Anything in parentheses expr++ expr-- (postfix) + - ++expr --expr (unary plus/minus, prefix) (type) (Casting)! (not) * / % (multiplication, division, remainder) + - (binary addition, subtraction) < <= > >= (relational operators) ==!= (equality) ^ (exclusive or) && (and) (or) = += -= *= /= %= (assignment, augmented assignment) Let s see Boolean1.java as an example CS1150

Selection Statements Homework 1, question 4 we assumed r>0 o o Calculate surface area of a sphere with radius r (user input) Everything is fine as long as r is a positive number What if the user input is a negative number? o Write code that can handle user input that is not correct Types of selection statements o o if statement switch statement CS1150

If Statement Select a path of execution based on a condition o o o A condition is evaluated (i.e. boolean expression) If the condition is true } The statements in the true branch are executed If the condition is false } The statements in false branch are executed (if there is an "else") CS1150

One-way if Statements if (boolean-expression) { statement(s); } CS4500/5500

One-way if Statements if i > 0 { System.out.println("i is positive"); } (a) Wrong if (i > 0) { System.out.println("i is positive"); } (b) Correct if (i > 0) { System.out.println("i is positive" ); } (a) Equivalent if (i > 0) System.out.println("i is positive"); (b) But only one statement can omit {} Let s see code Boolean2.java, Even1.java CS4500/5500

Two-way if Statement if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Example: Even2.java CS4500/5500

Multiple Alternative if Statements if (score >= 90.0) System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if (score >= 70.0) System.out.print("C"); else if (score >= 60.0) System.out.print("D"); else System.out.print("F"); Equivalent This is better if (score >= 90.0) System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if (score >= 70.0) System.out.print("C"); else if (score >= 60.0) System.out.print("D"); else System.out.print("F"); (a) (b) The else clause matches the most recent if clause Example: ComputeAndInterpretBMI.java CS4500/5500

Problem: Body Mass Index Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. The interpretation of BMI for people 16 years or older is as follows: BMI Interpretation BMI < 18.5 Underweight 18.5 <= BMI < 25.0 Normal 25.0 <= BMI < 30.0 Overweight 30.0 <= BMI Obese 20

If statement notes The else clause matches the most recent if clause What s the output? CS1150

If statement notes To force the else clause to match the first if clause, must add a pair of braces: int i = 1, j = 2, k = 3; if (i > j) { } if (i > k) else System.out.println("A"); System.out.println("B"); CS1150

If statement summary Block statements {} can be omitted if there is only one statement Recommendation Use curly braces: it helps make the code easier to modify and less error prone Avoid deeply nested if statements An else" clause always belongs with the most recent if clause CS1150

Summary Numeric literals Data casting Boolean data type If statement CS1150