Chapter 5.  Conditionals  Booleans  Relational and Logical Operators  Switch statements.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statement if (amount
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Decision Making - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 10/27/20151.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Discussion 4 eecs 183 Hannah Westra.
Chapter 3 Selection Statements
Chapter 4: Control Structures I
Building Java Programs
Chapter 4: Control Structures I
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Expressions and Control Flow in JavaScript
Chapter 4: Making Decisions.
Chapter 4: Control Structures I
More Selections BIS1523 – Lecture 9.
Conditional Statements
Building Java Programs
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()
PROGRAM FLOWCHART Selection Statements.
Presentation transcript:

Chapter 5

 Conditionals  Booleans  Relational and Logical Operators  Switch statements

 If-else statements  Use logical expressions  Else statement is optional  Booleans  Primitive data type – only true or false  Relational operators  >, =,<=, ==, !=  Most mistakes made confusing = and ==

 Program that evaluates even or odd:  int x = 15;  if(x%2 == 1) {System.out.println(“odd”);}  else {System.out.println(“even”);}  --no need to evaluate with an expression since the only other possibility is even  What would be returned above?

 Program that evaluates gender  String gender = “male”;  if(gender == “male”) {System.out.println(“guy”);}  Else {System.out.println(“lady”);}  What is displayed on your screen?

 Try this:  String gender = “ma”;  Gender += “le”; //gender now is “male”;  if(gender == “male”) {System.out.println(“guy”);}  Else {System.out.println(“lady”);}  Anything change? Try it!

 When using == and != for objects (like Strings), you compare their references, not their values, so it won’t always work  Instead of == and !=, objects typically have methods for comparison:  if(gender.equals(“male”)) {System.out.println(“guy”);}  Else {System.out.println(“lady”);}

 We have already covered &&, ||, and !  Results of these operators have boolean data types  Match the following to it’s equivalent (2 pair):  1. !(p&& q)  2. !(p || q)  3. !p && !q  4. !p || !q

 Logical operators are read left-right  If(rightIsClear() && frontIsClear() && hasBeepers())  If rightIsClear() is FALSE, the other operators ARE NOT evaluated – this is short circuit evaluation

 We have used if-else statements which evaluate one of 2 possible conditions  Given multiple conditons:  If(condition1)  {…}  Else if (condition2) {…}  Else if (condition3) {…}  Else // optional given the situation

 Given a situation where one of several outcomes depend on a value:  Int APscore;  If(APscore = 1) {…}  Else if (APscore = 2) {…}  Else if (APscore = 3) {…}  Else if (APscore = 4) {…}  Else {…}

 If-else-if statements can be a little messy  Use switch statement instead:  Int APscore;  Switch(APscore)  { case 1:  …. //do action  break;  case 2:  … // do action  break; }

 Practice  Create a short program using switch statements that does the following:  Create a scanner object that asks a user to input a number from 1-5 (if it is a different number, have the user re-enter the input)  Have a different output phrase given the number

 Case study – rolling dice  Read carefully, as you will need to understand the game of craps to program  Exercise set - 1, 2, 3, 4, 5, 6, 9, 11, 13, 15, 19