1 Fall 2007ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Chapter 4 - Control Statements
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
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.
CS0007: Introduction to Computer Programming
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Fall 2008ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
The if Statement The code in methods executes sequentially.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
1 Fall 2007ACS-1903 Chapter 3 Decision Structures Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
C++ for Engineers and Scientists Third Edition
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
© 2012 Pearson Education, Inc. All rights reserved. Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Data Structures.
Chapter 3 Decision Structures 1. Contents 1.The if Statement 2.The if-else Statement 3.The if-else-if Statement 4.Nested if Statement 5.Logical Operators.
Lecture 3 Decision Structures. 4-2 Topics – The if Statement – The if - else Statement – The PayRoll class – Nested if Statements – The if - else - if.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 3 Decision Structures Starting Out with Java: From Control Structures through Objects.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Conditionals & boolean operators
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Simple Control Structures IF, IF-ELSE statements in C.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2013.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Control Stuctures Module 4 Copyright © Copyright © Slide #2 Decision Structures Chapter Objectives To understand: how to write boolean expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3: Decision Structures
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
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.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Decision Statements, Short- Circuit Evaluation, Errors.
Lesson thirteen Conditional Statement "if- else" ©
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)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Ltd.
Lecture 3 Selection Statements
Chapter 3: Decision Structures by Tony Gaddis and Godfrey Muganda
Chapter 3: Decision Structures
Lecture 3- Decision Structures
Operator Precedence Operators Precedence Parentheses () unary
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
SELECTION STATEMENTS (1)
Chapter 4 Control Statements: Part I
Chapter 3: Decision Structures
Starting Out With Java 5 (Control Structures to Objects) Chapter 3
Pages:51-59 Section: Control1 : decisions
Chapter 5: Selection Statement
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Pages:51-59 Section: Control1 : decisions
Presentation transcript:

1 Fall 2007ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators Comparing String Objects The Conditional Operator The switch Statement Variable Declaration and Scope DecimalFormat Class printf

2 Fall 2007ACS-1903 if statements Suppose we need to calculate reward points for a retailer. Assume a customer receives 1 reward point for each 25 dollars spent, but if the customer spends more than 300 dollars the customer receives an extra 50 points. Examples: Dollars spent Reward Points

3 Fall 2007ACS-1903 pseudocode If sale is less than or equal to 300 Then reward points are dollar value / 25 Otherwise reward points are 50 plus (dollar value / 25)

4 Fall 2007ACS-1903 Flowchart Sale > 300 Reward points = 50 + (Dollars / 25) Reward points = Dollars / 25 true false

5 Fall 2007ACS-1903 if statement As a UML activity diagram [sale > 300] [sale<=300] rewardPoints = 50 + dollars / 25rewardPoints = dollars / 25

6 Fall 2007ACS-1903 if statement if ( sale <= 300 ) rewardPoints = dollars / 25; else rewardPoints = 50 + dollars / 25; Now … using BlueJ

7 Fall 2007ACS-1903 if statements The if statement contains a section of code that executes if the value of a boolean expression is true. Optionally, there is a section of code that executes if the value of the boolean expression is false. General syntax if (boolean expression) java statement; [ else java statement; ] Note that “[“ and “]” are ways we indicate an optional component in syntax If there is more than one statement you want for a “then” clause or an “else” clause, you can create a compound statement using curly braces {}

8 Fall 2007ACS-1903 if statements Note that an If-else is a java statement and so we allow for If’s to be nested Suppose we give reward points out as before but with an additional bonus of 100 points if the sale is for more than $500. Pseudocode: If sale <= 300 then reward = dollars /25 otherwise if sale <= 500 then reward = dollars/ otherwise reward = dollars/

9 Fall 2007ACS-1903 Flowchart Sale > 300 Reward points = 50 + (Dollars / 25) Reward points = Dollars / 25 true false Reward points = (Dollars / 25) Sale > 500 true false

10 Fall 2007ACS-1903 Java Code if ( saleAmount <= 300 ) rewardPoints = dollarsSpent / 25; else if (saleAmount <=500 ) rewardPoints = 50 + dollarsSpent / 25; else rewardPoints = dollarsSpent/25;

11 Fall 2007ACS-1903 Relational Operators In most cases, the boolean expression, used by the if statement, uses relational operators. Relational OperatorMeaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to

12 Fall 2007ACS-1903 Boolean Expressions A boolean expression is any variable or calculation that results in a true or false condition. ExpressionMeaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y. x == y Is x equal to y? x != y Is x not equal to y?

13 Fall 2007ACS-1903 Boolean Expressions if (x > y) System.out.println(“X is greater than Y”); if(x == y) System.out.println(“X is equal to Y”); if(x != y){ System.out.println(“X is not equal to Y”); x = y; System.out.println(“However, now it is.”); } Example: AverageScore.javaAverageScore.java Note the use of “{“ and “}” There are 3 statements executed when x!=y is true

14 Fall 2007ACS-1903 Comparing Strings if (gender.equals("male")) { System.out.println("Hello Mr." + name); } else { System.out.println("Hello Ms." + name); } System.out.println("Your gross pay is $" + grossPay); To compare two strings in Java you typically use equals() or equalsIgnoreCase() See documentation for class String Now … using BlueJ

15 Fall 2007ACS-1903 Programming Style Rules of thumb: The conditionally executed statement should be on the line after the if condition. The conditionally executed statement should be indented one level from the if condition. It is suggested by some to always use curly braces, but when an if statement does not have the curly braces, it is ended by the first semicolon encountered after the if condition. if(expression) statement; No semicolon here. Semicolon ends statement here.