Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=, <, <=  Pay attention to the equality operator, TWO equal signs.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
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.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
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.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Control statements Mostafa Abdallah
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Random Functions Selection Structure Comparison Operators Logical Operator
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Chapter 3: Decisions and Loops
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
CSC115 Introduction to Computer Programming
SELECTION STATEMENTS (1)
Control Structures.
Topics The if Statement The if-else Statement Comparing Strings
IF if (condition) { Process… }
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSC530 Data Structure - Decision
Control Structures: Selection Statement
Logical Operations In Matlab.
Conditional Logic Presentation Name Course Name
SELECTIONS STATEMENTS
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Presentation transcript:

Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=, <, <=  Pay attention to the equality operator, TWO equal signs.  All relational operators result in boolean value: true or false

Decisions  Compare two Strings  Character by character from left to right  Compare their ASCII values  Once they are not equal, comparison stops

Decisions  Logical operators  Operate on logical expressions  Logical expression: any expression whose value is a boolean value  Three operators:  && --- logical and  || --- logical or  ! --- logical not

Decisions  Logical “and”: p && q, where p and q are logical expressions  Any one of p and q is false  the result is false  Both p and q must be true to get the result true.

Decisions  Logical “or”: p || q, where p and q are logical expressions  Any one of p and q is true  the result is true  Both p and q must be false to get the result false.

Decisions  Logical “not”: !p, where p is a logical expression  If p is true, !p is false  if p is false, !p is true

Decisions  Truth table  T --- True, F --- False PqP && qp || q!p TTTTF TFFTF FTFTT FFFFT

Decisions  if statement  switch statement

if statement  Simple if statement if (condition) { statements; } if condition is true, execute the statements, otherwise skip it.

if statement  if-else statement if (condition) { Tstatements; } else { Fstatements; }  if condition is true, execute the Tstatements  if condition is false, execute the Fstatements

Multi-choice if statement if (condition 1) { statements 1; } else if (condition 2){ statements 2; } else if …… …… [ else { last statements; } ]

Multi-choice if statement If condition 1 is true, execute statements 1  Go out of the if statement; If condition 1 is false, check condition2. If condition 2 is true, execute statements 2  Go out of the if statement; …… If all conditions are false a) if there is an else statement, execute the laststatements b) if there is no else statement, simply go out the if statement.

Multi-choice if statement  At most one set of the statements is executed!!!  The conditions should be exclusive and not overlapped.

switch and case statement switch (expression) { case constant1: statements1; break; case constant2: statement2; break; …… [ default: default statements; ] }

switch and case statement expression --- an integer expression Start with version 7, expression could be an String expression constant1, constant2… --- integer constants In version 7, could use String constants

switch and case statement If expression equals constant1, statements1 is executed;  Go out of the switch statement; If expression does not equals constant1, check constatant2, if they are equal, statements2 is executed;  Go out of the switch statement; …… If the expression does not equal to any constant, a) if there is a default clause, execute the defaultstatements b) if there is no default clause, simply go out the switch statement.

switch and case statement  At most one set of the statements is executed!!!  The constants should be different.

Programming Problems 1. Write a program that reads the number of hours (less than 60) worked in a week and hourly rate from the console, computes the gross income and displays the result on the console. Note that if the worked more than 40 hours, the extra hours should pay 1.5 times of the base rate.

Programming Problems 2.Write a program that reads the grades (20-100) from the console, computes the letter grade and displays it on the console : F; 60-69: D; : C; 80-89: B; : A.

Programming Problems 1. Write a program that reads the number of hours (less than 60) worked in a week and hourly rate from the console, computes the gross income and displays the result on the console. Note that if the worked more than 40 hours, the extra hours should pay 1.5 times of the base rate.

Programming Problems 3.Write a program that gives comments according to the letter grades.