Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose.

Slides:



Advertisements
Similar presentations
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 4: Control Structures: Selection
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
© 2006 Pearson Addison-Wesley. All rights reserved Reference Types A variable of reference type refers to (is bound to) an object Data of reference.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Lecture Set 5 Control Structures Part A - Decisions Structures.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control Structures. 2  Control – the order in which instructions are performed in a program.  Control structures are well defined ways of determining.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
ICT Introduction to Programming Chapter 4 – Control Structures I.
© 2006 Pearson Addison-Wesley. All rights reserved Syntax if (some_condition) { then_clause } Notes some_condition must be a valid boolean expression.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Lecture 3 Selection Statements
If/Else Statements.
Chapter 3 Selection Statements
Data Types and Expressions
Chapter 4: Making Decisions.
Selections Java.
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions & the ‘if’ Statement
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Relational Operators Operator Meaning < Less than > Greater than
SELECTION STATEMENTS (2)
Summary Two basic concepts: variables and assignments Basic types:
Control Structure Chapter 3.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

Lecture 7.1 Selection - the if Instruction

© 2006 Pearson Addison-Wesley. All rights reserved An algorithm often needs to make choices… choose to print a special warning if the bank balance is negative choose whether to move a bouncing ball up or down choose how many dots to place on the dice face Selection instructions allow code to make choices. Java selection instructions:

© 2006 Pearson Addison-Wesley. All rights reserved Syntax if (some_condition) { then_clause } Notes some_condition must be a valid boolean expression. then_clause is a sequence of zero or more instructions. then_clause should be indented as shown The {... } enclosure can be eliminated if then_clause is a single instruction. Semantics some_condition is evaluated and then_clause is executed if and only if some_condition is true Example (Assume that pay, wage, hours and bonus are double instance variables.) pay = wage * hours; if (hours > 40) { pay = pay + (hours - 40) * wage * 0.5; } pay = pay + bonus;

© 2006 Pearson Addison-Wesley. All rights reserved Syntax if (some_condition) { then_clause } else { else_clause } Notes some_condition must be a valid boolean expression. then_clause and else_clause are a sequences of zero or more instructions. then_clause and else_clause should be indented as shown The {... } enclosures can be eliminated for single instruction clauses. Semantics some_condition is evaluated and then_clause is executed if and only if some_condition is true, otherwise else_clause is executed Example (Assume score, exam, homework are int instance variables and grade is a char variable.) score = exam * 25 + homework; if (score > 59) { grade = 'P'; } else { grade = 'F'; } System.out.println( “Grade is “ + grade );

© 2006 Pearson Addison-Wesley. All rights reserved Finding the maximum of two int variables Only create a rectangle if width and height are positive if (theWidth > 0 && theHeight > 0) { theRect = new Rectangle(10, 10, theWidth, theHeight); } Color an oval green 30% of the time and red 70% if ( ) { theOval.setBackground( Color.green ); } else { theOval.setBackground( Color.red ); }

© 2006 Pearson Addison-Wesley. All rights reserved Any two primitive expressions of compatible type can be compared using a relational operator, and the result is a boolean (true or false) value. Operators ==equal to (warning: don’t confuse with =) !=not equal to <less than <=less than or equal to >greater than >=greater than or equal to Example if ( count != 0 ) { average = total / count; }

© 2006 Pearson Addison-Wesley. All rights reserved boolean is a primitive data type for storing logical values. Infix Operators and (warning: don’t confuse with &) logical (inclusive) or Constants true false Prefix Operator not (logical negation) Operator Precedence (highest to lowest) ! - (unary negation) * / % + - >= == != && || =

© 2006 Pearson Addison-Wesley. All rights reserved Some declarations private int int1, int2; private double dbl1, dbl2; private char letter; Valid boolean expressions int1 < true && (letter == ‘Z’) ! (dbl1 == int2) (0 < int1) && (int1 < 100) letter==‘a’ || letter==‘e’ || letter==‘i’ || letter==‘o’ || letter == ‘u’ dbl2 != 0.0 && dbl1/dbl2 < Invalid boolean expressions int1 < = int2 ‘a’ < letter < ‘z’ int1 = int2

© 2006 Pearson Addison-Wesley. All rights reserved Way Selection Both if-then and if-then-else forms of the if instruction exhibit 2- way selection, because there are two options at execution time. Example if (examScore > 90) { letterGrade = ‘A’; } else if (examScore 80) { letterGrade = ‘B’; } else if (examScore 70) { letterGrade = ‘C’; } else if (examScore 60) { letterGrade = ‘D’; } else { letterGrade = ‘F’; } Multi-Way Selection Java includes a switch instruction that provides for limited multi- way selection, but using nested if instructions is the more general solution

© 2006 Pearson Addison-Wesley. All rights reserved An else line applies to the most recent if instruction that is not terminated. Example (Don’t write code this way...) if (someInt < 10) { System.out.println(“small”); if (secondInt < 10) { System.out.println(“small again”); } else { System.out.println(“big”); } The Moral... Indentation of code is important. In general, clauses should be indented and right parentheses aligned with the notation from which they began.