Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.

Slides:



Advertisements
Similar presentations
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Selection Statements & Exam 1 Review Recitation – 2/13/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition 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 Animated Version.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement a selection control.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Selection Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Logical Operators and Conditional statements
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Visual C++ Programming: Concepts and Projects
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Chapter Chapter 5 Selection Statements. Objectives Understand selection control statement –if statements –switch statements Write boolean expressions.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements 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.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Flow of Control Part 1: Selection
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 5: Objectives After you have read and studied this chapter, you should be able to Implement a selection control using if statements Implement a.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Decision Statements, Short- Circuit Evaluation, Errors.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
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)
Branching statements.
CNG 140 C Programming (Lecture set 3)
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Control Structures – Selection
The System.exit() Method
Control Structure Chapter 3.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Controlling Program Flow
Control Structure.
Presentation transcript:

Chapter 5 Selection Statements

Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators –boolean operators

Control Statements Statements in programs are normally executed in sequence. Method calls transfer control to another part of the code (and back again) A statement that alters the control flow is called a control statement. –Selection (branching) allows program to decide whether to do a part of the code –Repetition (Chapter 6) allows sections of code to be repeated Exceptions (Chapter 8) also alter the flow of a program

The if Statement The if statement is one type of selection statement. BufferedReader kbd = new BufferedReader( new InputStreamReader( System.in)); System.out.println(“Enter test score:”); String input = kbd.readLine(); int testScore = Integer.parseInt( input); if (testScore < 70) System.out.println(“You did not pass”); else System.out.println(“You passed”);

The if Statement The if statement specifies which block of code to execute, depending on the result of evaluating a test condition called a boolean expression. if ( ) else –The is an expression that evaluates to either true or false.

Syntax of an if statement Mapping of the sample if statement to the general format.

Boolean Expressions and Variables A boolean expression is either true or false. –true and false are reserved words There is a primitive data type called boolean which can take only these two values. We can declare a variable of data type boolean and assign a boolean value to it. boolean pass, done; pass = 70 < x;// compute the value done = true;

Boolean Expressions The six relational operators we can use in conditional expressions are: <less than <=less than or equal to ==equal to !=not equal to > greater than >=greater than or equal to These operators are used with primitive operands

Flow chart for if Statement

if Without else The if statement does not have to contain an else condition. –In the if-then structure, no special instructions are executed if the boolean expression evaluates to false. if (testScore >=95){ System.out.println( “You are an honor student”); } behaves the same as if (testScore >=95){ System.out.println( “You are an honor student”); } else { }

Compound Statements What if you need to do multiple statements in the if and else parts of an if statement? –Make a compound statement by putting curly braces around all the statements to be done

Flow Chart: if without else

Boolean Operators A boolean operator takes boolean values as its operands and returns a boolean value. The three boolean operators are AND&&binary OR||binary NOT!unary

Truth Tables Boolean operators and their meanings: Short-circuit evaluation DeMorgan's Laws pq!pp&&qp||q true falsetrue false true falsetrue falsetrue false truefalse

Precedence rules OrderOperatorRule first()innermost first left to right -, !single operand *, /, %left to right +, -left to right =, >left to right == !=left to right &&left to right ||left to right last=right to left

Nested-if Statements An if statement that contains another if statements in either its then or else block is called a nested-if statement. if (testScore >= 70){ if (studentAge < 10){ System.out.println(“You did a great job”); } else { System.out.println(“You did pass”); //test score >=70 and age >=10 } } else { //test score < 70 System.out.println(“You did not pass”); }

Nested if flow chart

Dangling else CAUTION: Be careful with nested if statements that don't have the else part. if (x < y) if (x < z) System.out.println(“Hello”); else System.out.println(“Goodbye”); An else belongs to the nearest if unless curly braces are used to indicate otherwise. Compiler doesn't look at indentation.

Comparing Objects When two variables are compared, we are comparing their contents. In the case of objects, the content is the address where the object is stored. –using == and != on objects compares their addresses To compare the content of objects you need to define comparison methods for the class. –equals (inherited from Object) –compareTo (Chapter 9)

Equality of Objects How the equality == testing works with the objects. To test the equality of the data, the equals method needs to be defined for the weight class

Comparing Objects : Example Class Ch5Weight{... public boolean equals(Ch5Weight wgt) { boolean result; double thisGram = this.getGram(); double otherGram = wgt.getGram(); if (thisGram == otherGram){ result = true; } else { result = false; } return result; }... }

The switch Statement The switch statement provides an efficient way to evaluate and process multiple options. switch (gradeLevel){ case 1: System.out.println(“Go to the gymnasium”); break; case 2: System.out.println(“Go to the Science Auditorium”); break; case 3: System.out.println(“Go to Halligan Hall Room 104”); break; case 4: System.out.println(“Go to Root Hall Room 101”); break; }

The switch Statement The syntax for the switch statement is switch ( ){ :... : default: }

The switch Statement The data type of must be char, byte, short, or int. The value of is compared against the constant i of. If there is a matching case, its case body is executed. Otherwise, the execution continues to the statement following the switch statement.

The switch Statement The break statement causes execution to skip the remaining portion of the switch statement and resume execution following the switch statement. The break statement is necessary to execute statements in one and only one case.

Effect of break on switch Without breakWith break

5.5 The switch Statement It is good practice to implement a default case in a switch statement. The default case will be executed if there is no matching case.