© 2006 Pearson Addison-Wesley. All rights reserved 7.1.1 Syntax if (some_condition) { then_clause } Notes some_condition must be a valid boolean expression.

Slides:



Advertisements
Similar presentations
Logic & program control part 2: Simple selection structures.
Advertisements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
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.
© 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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 3 Edited by JJ Shepherd
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,
Flow of Control Part 1: Selection
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
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.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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.
Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved An algorithm often needs to make choices… choose.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Selections Java.
Chapter 4: Making Decisions.
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions & the ‘if’ Statement
Relational Operators Operator Meaning < Less than > Greater than
SELECTION STATEMENTS (2)
Chapter 7 Conditional Statements
Summary Two basic concepts: variables and assignments Basic types:
Control Structure Chapter 3.
Logic of an if statement
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
Boolean Expressions & the ‘if’ Statement
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

© 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.