COMP 110: Spring 20091 Announcements Lab 1 was due at noon Lab 2 on Friday (Bring Laptops) Assignment 1 is online TA Office hours online 30-min quiz at.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Chapter 4 - Control Statements
Logic & program control part 3: Compound selection structures.
CS0007: Introduction to Computer Programming
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Lecture 6 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
Computer Science Selection Structures.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
CONTROLLING PROGRAM FLOW
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
ITM 352 Flow-Control: if and switch. ITM © Port, KazmanFlow-Control - 2 What is "Flow of Control"? Flow of Control is the execution order of instructions.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
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.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
If Statements If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter#3 Part1 Structured Program Development in C++
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
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
COMP Loop Statements Yi Hong May 21, 2015.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Branching statements.
CS0007: Introduction to Computer Programming
Selections Java.
Lecture 3- Decision Structures
Flow of Control.
ITM 352 Flow-Control: if and switch
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
If statement.
Chapter 2 Programming Basics.
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Announcements Program 1 due noon Lab 1 due noon
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

COMP 110: Spring Announcements Lab 1 was due at noon Lab 2 on Friday (Bring Laptops) Assignment 1 is online TA Office hours online 30-min quiz at end of class select all that apply multiple choice

COMP 110: Spring Questions? Last time we covered Typecasting Strings Binary/Unary operators I/O Style

COMP 110: Spring Today in COMP 110 Branching if – else control structure booleans relational operators

COMP 110: Spring Branching Sometimes, it is necessary to make decisions in programs Example The remainder operator can be used to determine if a number n is even or odd If n%2 equals 0, n is even If n%2 equals 1, n is odd

COMP 110: Spring Branching We know how to calculate n % 2 int result = n % 2; But what next to do next?

COMP 110: Spring Branching int result = n % 2; Evaluate result is 0? Execute Print “n is even” Execute Print “n is odd” true false

COMP 110: Spring If-Else Statement An if-else statement allows us to make decisions in a program int result = n % 2; if(result == 0) System.out.println(“That number is even!”); else System.out.println(“That number is odd!”);

COMP 110: Spring If-Else Example int n = 2 int result = n % 2; if(result == 0) System.out.println(“That number is even!”); else System.out.println(“That number is odd!”); System.out.println(“Finished!”); result = 0

COMP 110: Spring If-Else Example int n = 3 int result = n % 2; if(result == 0) System.out.println(“That number is even!”); else System.out.println(“That number is odd!”); System.out.println(“Finished!”); result = 1

COMP 110: Spring Boolean Expressions (result == 0) is a boolean expression Boolean expressions evaluate to either true or false Examples 10 > 5, (true) 4 > 6, (false) Integers are whole numbers, (true)

COMP 110: Spring Java Comparison Operators ==Equal to !=Not equal to >Greater than >=Greater than or equal to <Less than <=Less than or equal to

COMP 110: Spring If-Else Statement Syntax Syntax if(Boolean_Expression) Statement_1 else Statement_2 If Boolean_Expression is true, Statement_1 is executed; otherwise Statement_2 is executed

COMP 110: Spring Compound Statements Multiple statements can be included in each branch Called a compound statement Enclose between {…} if(Boolean_Expression) { Statements_1 } else { Statements_2 }

COMP 110: Spring If without Else Syntax if(Boolean_Expression) Statement_1 Example if(accntBalance-withdrawal < 0) //overdraft fee accntBalance = accntBalance – FEE;

COMP 110: Spring Common Comparison Mistakes Don’t confuse the assignment operator (=) with the comparison operator (==)! if(x == y) //valid if(x = y) //syntax error

COMP 110: Spring Common Comparison Mistakes Don’t use == to compare Strings Use string.equals(A_String) or string.equalsIgnoreCase(A_String) Example String s1 = keyboard.next(); //read in a string if(s1.equals(“Hello”)) System.out.println(“The String is Hello.”); else System.out.println(“The String is not Hello.”);

COMP 110: Spring The && Operator (AND) We can check for multiple conditions using the && (AND) operator Meaning is similar to that of English “and” if ((temperature > 50) && (temperature < 75)) { // walk to school if 50 < temperature < 75 } else { //otherwise drive to school }

COMP 110: Spring The || Operator (OR) We can also join boolean expression with || (OR) Meaning is similar to that of English “or” if (raining || runningLate) { //drive to school }

COMP 110: Spring The ! Operator (NOT) Boolean negation !false is True !true is false Example if (!cloudy) { // walk to school if it’s not cloudy }

COMP 110: Spring Effect of Boolean Operators ABA && BA || B!A true false truefalse truefalse truefalsetrue false true

COMP 110: Spring Boolean Expression Examples Using x = 5, y = 10, z = 15 (x x) (false && true) -> false (x x) (true || true) -> true (x > 3 || z != 15) (true || false) -> true (!(x > 3) && x + y == z) (false && true) -> false

COMP 110: Spring Avoiding the Negation Op !(A !(A > B) --> !(A >= B) --> !(A == B) --> !(A != B) --> (A >= B) (A > B) (A <= B) (A < B) (A != B) (A == B) It’s best to avoid use of the negation operator (!) when possible

COMP 110: Spring Nested If-Statements It’s possible to have if statements inside other if statements Example We want to perform some checks on the user’s account balance If the balance is >= 0, we’ll add interest if the interest rate is >= 0, and print an error message if interest rate is < 0. If the balance is < 0, we’ll subtract a fee.

COMP 110: Spring Nested If Statements Evaluate balance >= 0 Evaluate INTEREST_RATE >= 0 Execute balance -= FEE truefalse Execute balance = balance + balance*INTEREST_RATE Execute Print Error Message a truefalse

COMP 110: Spring Nested If Statements if(balance >= 0) { if(INTEREST_RATE >= 0) balance = balance + INTEREST_RATE*balance; else System.out.println(“Negative Interest!”); } else balance = balance – FEE;

COMP 110: Spring Nested If Statements What if we didn’t need to print the error message? if(balance >= 0 && INTEREST_RATE >= 0) balance = balance + INTEREST_RATE*balance; else balance = balance – FEE; //whats wrong here?

COMP 110: Spring Multi-Branch If Statements What if we need to decide between many possibilities? Example Given a numeric score (0..100) determine whether the grade is an A,B,C,D,or F

COMP 110: Spring Multi-Branch If Statements A if: score >= 90 B if: 90 > score >= 80 C if: 80 > score >= 70 D if: 70 > score >= 60 F in all other cases

COMP 110: Spring Multi-Branch If Statements We could write this as follows if(score >=90) grade = ‘A’; else if(score >=80) grade = ‘B’; else if(score >=70) grade = ‘C’; else if(score >=60) grade = ‘D’; else grade = ‘F’;

COMP 110: Spring Multi-Branch If Statements The preferred way to write this is if(score >=90) grade = ‘A’; else if(score >=80) grade = ‘B’; else if(score >=70) grade = ‘C’; else if(score >=60) grade = ‘D’; else grade = ‘F’;

COMP 110: Spring Multi-Branch If Statement Syntax if(Boolean_Expression_1) Action_1 else if(Boolean_Expression_2) Action_2 … else if(Boolean_Expression_n) Action_n else Default_Action

COMP 110: Spring Programming Demo Write a program to read in three distinct nonnegative integers from the keyboard and display them in increasing order

COMP 110: Spring Programming Demo Designing the algorithm Determine which of a,b,c is the smallest If it’s not a, is it b? If it’s not a or b, must be c Determine which of the two remaining integers is smaller than the other

COMP 110: Spring Programming Demo Evaluate a is smallest? true false a < b < ca < c < b Evaluate b < c true false b < a < cb < c < a Evaluate a < c true false c < a < bc < b < a Evaluate a < b true false Evaluate b is smallest? truefalse

COMP 110: Spring Programming Demo Pseudocode Ask user for three integers, a,b,c Determine which of a,b,c is the smallest Determine which of the remaining two is smaller Print a,b,c in ascending order

COMP 110: Spring Friday Recitation Bring Laptop (fully charged) Any questions about Program 1