COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University

Slides:



Advertisements
Similar presentations
15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Advertisements

Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Subject: Information Technology Grade: 10
DECISIONS Chapter 5. The if Statement  Action based on a conditions  If the condition is true, the body of the statement is executed if (amount
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Chapter 5 Decisions Goals To be able to implement decisions using if statements To be able to implement decisions using if statements To understand how.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions.
Fall 2006Slides adapted from Java Concepts companion slides1 Decisions Advanced Programming ICOM 4015 Lecture 5 Reading: Java Concepts Chapter 6.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2013 by John Wiley & Sons. All rights reserved. DECISIONS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 15,
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Decisions Bush decision making.
CPS120: Introduction to Computer Science Decision Making in Programs.
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
Decision Statements, Short- Circuit Evaluation, Errors.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Discussion 4 eecs 183 Hannah Westra.
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Control Statements
Chapter Goals To implement decisions using if statements
The switch Statement, and Introduction to Looping
Chapter 4: Making Decisions.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Chapter 5/6 Decisions.
Slides by Donald W. Smith
Conditional Branching
DECISIONS.
Introduction to Computer Science I.
EECE.2160 ECE Application Programming
CSCE 206 Lab Structured Programming in C
Presentation transcript:

COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University

Used to Implement decisions Two key elements condition body amount <= balance balance = balance - amount true false condition body if (amount <= balance) { balance = balance – amount; } if-then

Used to Implement decisions Two key elements condition body amount <= balance balance = balance - amount true false condition body balance = balance – OVERDRAFT_PENALTY body if (amount <= balance) { balance = balance – amount; } else { balance = balance – OVERDRAFT_PENALTY; } if-then-else

Example if (originalPrice > 100) { discountPrice = originalPrice – 20; } else { discountPrice = originalPrice – 10; } originalPrice discountPrice good to use braces

How about swapping the two statements if (originalPrice < 100) { discountPrice = originalPrice – 10; } else { discountPrice = originalPrice – 20; } originalPrice discountPrice different when originalPrice is 100

Conditional Operator condition ? value1 : value2 actualFloor = floor > 13 ? floor-1 : floor; if (floor > 13) { actualFloor = floor – 1; } else { actualFloor = floor; } you will find this in many java programs, but not recommended for now

Relational Operators: Comparing Numbers if (floor operator 13) relational operators >greater than <less than >=greater than or equal to <=less than or equal to ==equal to !=not equal to ExpressionValue 3 <= 4true 3 =< 4ERROR 3 > 4false 4 < 4false 4 <= 4true 3 == 5-2true 3 = 6/2ERROR “10 > 5ERROR use == for equality cannot compare a string to a number

Comparing Floating-point Numbers double r = Math.sqrt(2.0); if (r * r == 2.0) { System.out.println(“Math.sqrt(2.0) squared is 2.0”); } else { System.out.println(“Math.sqrt(2.0) squared is” + r * r); } Floating-point numbers have only a limited precision, and calculations can introduce round-off errors In most cases, it does not make sense to compare to two reals exactly. Instead, we should test whether they are close enough: |x-y| < epsilon (where epsilon can be set to very small, say 10e-14)

Comparing Strings String s1 = “Robert”; String s2 = s1.substring(0, 3); // s2 = “Rob” if (s2 == “Rob”) // test is false if (s2.compareTo(“Rob”) == 0) // test is true Use method “compareTo” instead of “==“

Nested Branches if (condition1) { … } else if (condition 2) { … } else if (condition 3) { … } else { … } Richter Scale ValueEffect 8 most structures fall 7 many buildings destroyed 6 many buildings damaged 4.5 damaged to poorly constructed buildings

Nested Branches if (richter >= 8.0) { System.out.println(“Most structures fall”); } else if (richter >= 7.0) { System.out.println(“Many buildings destroyed”); } else if (richter >= 6.0) { System.out.println(“many buildings damaged, some collapse”); } else if (richter >= 4.5) { System.out.println(“Damage to poorly constructed buildings”); } Richter Scale ValueEffect 8 most structures fall 7 many buildings destroyed 6 many buildings damaged 4.5 damaged to poorly constructed buildings

Example: Federal Tax Rate Schedule Singlemarried Taxable incomerateTaxable incomerate <=$21,45015%<=$35,80015% Over $21,450 but <= $51,90028%Over $35,800 but <= $86,50028% Over $51,90031%Over $86,50031% (1)Flow chart (2)Test cases (3)Code your code input 1. amount of tax due 1.marriage_status 2.income output

Read inputs: (married, income) Start single? income <= income <= rate = 15%rate = 28%rate = 31% income <= income <= rate = 15%rate = 28% rate = 31% Yes No Yes No Compute due tax: rate * income End

Another Example Write (1) test cases, (2) flowchart, and (3) code to prompt the user for three sides (of type INT) of a triangle. Then check if it is an equilateral triangle (i.e., all three sides are equal), else check if it is an isosceles triangle (i.e., two sides are equal). Print out if it is an equilateral, isosceles, or scalene triangle (i.e., no sides are equal).

One More Example HorseGoatMonkeyRoosterDogPigMouseOXTigerRabbitDragonSnake The celebration of the Chinese Lunar year goes back centuries. There are 12 different creatures represented in the Chinese calendar. A different animal is commemorated each year. After 12 years the animals are repeated. If you were born in between 1966 and 2013, you can use the following table to find out your “ year animal ”. For examples, if you were born in 1990, your year animal is “ Horse ” ; if you were born in 1991, your year animal is “ Rabbit ”.

Switch Statement switch (variable) { case value1: body1 break; case value2: body2; break; case value3: body3; break; ::: default: body; break; }