Decision Making Selection structures (if....else and switch/case)

Slides:



Advertisements
Similar presentations
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.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
True or false A variable of type char can hold the value 301. ( F )
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Computer Science Department Relational Operators And Decisions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
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.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Decision Statements, Short- Circuit Evaluation, Errors.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Java Programming Fifth Edition
The if…else Selection Statement
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 6 Conditionals.
Selections Java.
Flow of Control.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Conditionals & Boolean Expressions
Selection (if-then-else)
The Java switch Statement
PROGRAM FLOWCHART Selection Statements.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Controlling Program Flow
Chapter 6 Conditionals.
Presentation transcript:

Decision Making Selection structures (if....else and switch/case)

if....else We use selection many times every day as we make decisions For Example: if the car is available drive to work Q: what will happen if the car is not available? A: In this case, an alternate action should be stated. Condition Action

if....else if the car is available drive to work else take public transit to work The word else indicates the "take public transit" is the alternate action that will take place if the condition is NOT true. Only one condition can be true.

else if The words else and if are used together to introduce another possible condition Many conditions can be included but only one condition will be true. if the car is available drive to work else if public transit is running take public transit to work else if the bike is available bike to work else walk to work

A Condition (boolean expression) can be formed by using logical operator Logical Operators consist of: == Equals > Greater than != Not Equals < Less than <= Less than or equals >= Greater than or equals && And || Or (the ‘pipe’ symbols)

And (&&) Result is true if All Conditions are true For example: If you are hungry, AND you have money, THEN you can buy lunch

&& (And) Expression1Expression2Result True False True False True False

|| (OR) Result is true if ANY condition is true. For example: If you have money OR you have a credit card, you can pay the bill.

|| (Or) Expression1Expression2Result True False True False True False

!= (Not) Changes True Result to false & Vice Versa. For Example: If not sunny then it is cloudy.

!= (Not ) ExpressionResult True False True

Selection structure in Java Set brackets required if more than 1 statement only And... both must be true Default... ‘else’ occurs if no other condition prior is true Default Selection: if ( mark >=50 ) { c.println ( “You passed” ); } else { c.println ( “You failed”); }

Multiple Selection

if without else In some cases, there is an action to carry if a condition is true and nothing to do if the condition is not met. If a discount of 25% is applied to all purchases over $200.00, the calculation would be: double discount=0; if (purchase>200.00) { discount=purchase*.25; purchase-=discount; }

"switch/case" Multiple - Selection Statement Java provides the switch/case multi- selection statements to perform different actions based on the possible values of an integer variable or expression. Each action is associated with a constant integral value (i.e. int, char, short or byte) that the variable or expression can have. It cannot be used with long or double.

This structure is used in conjunction with the break instruction. This type of structure is often referred to in other languages as the select …case structure.

Switch: Java Syntax switch ( identifier) { case value1: // …statements to be executed break ; case value2: // …statements to be executed break ; case value3: case value4: case value5: // …statements to be executed… break ; default : // …statement break ; } // switch

Switch: Integer Example // Here is an example of a switch construct that outputs a comment based on a // student's mark out of 10 int mark = In.getInt ( ); switch (mark) { case 10: System.out.println ("Perfect."); break; case 9: case 8: System.out.println ("Pretty Good."); break; Case 7: case 6: System.out.println ("Okay."); break; case 5: System.out.println ("Squeaked By!"); break; default: System.out.println ("Did not pass. Ouch!"); break; } // switch

Notes The keywords if and else must be lower case A line that contains a key word does not end with a semi- colon. The condition is in parentheses (age < 13) By convention the action statements should be indented Each set of actions is contained in braces, the braces may be omitted if there is only one action but it is good practice to include them. The equality operator in Java is two equals signs. If you are testing to see if a person's age is 21, the statement would be: if (age==21) The switch/case construct must be used in conjunction with the break statement. The default statement is to be used as a catch all case in case none of the above cases is executed.

Homework Page 109 # 1 Page 115 # 4 Page 118 # 3 Read page 98, 99 (compareTo)