Lecture 6 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

3-1 Chapter 3 Flow of Control (part a - branching)
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
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.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
Chapter 4: Control Structures: Selection
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Decision Making Selection structures (if....else and switch/case)
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
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.
Flow of Control Chapter 3 Flow of control Branching Loops
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 2. Program Construction in Java. 2.4 Selection (decisions)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Chapter 3 1 l Branching l Loops l exit(n) method l Boolean data type and logic expressions Flow of Control – Part 1.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I
Java Programming Fifth Edition
Computer Programming with Java
Conditional Statements and Control Structure
Chapter 3: Decisions and Loops
Chapter 4: Control Structures I
Chapter 3 Edited by JJ Shepherd
Flow of Control.
Chapter 3 Branching Statements
Expressions and Control Flow in JavaScript
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4: Control Structures I
2-1 Making Decisions Sample assignment statements
Selection (if-then-else)
Chapter 4: Control Structures I (Selection)
Chapter 2 Programming Basics.
Control Structure.
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:

Lecture 6 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming

SE15: Branching Statements6–26–2 Today’s Learning Objectives Understand how you can alter the flow of a program Learn about the Java branching statements Learn about Boolean expressions

SE15: Branching Statements6–36–3 Lecture Outline Flow of Control Branching Statements The if-else statement Boolean expressions Multi branch if-else statements The switch statement

SE15: Branching Statements6–46–4 Flow of Control The order in which a program performs actions Java uses two kinds of statements to regulate flow of control: Branching statement Chooses one action from a list Loop statement Repeats an action until some stopping condition is met Savtich p130

SE15: Branching Statements6–56–5 if-else Chooses between two possible alternative actions Banking example If you are in credit, interest may be added to your account If you are in debt, you will be charged a penalty Example 1 if (balance >= 0) balance = balance + (INTEREST_RATE * balance); else balance = balance - OVERDRAWN_PENALTY ; Note: expression inside parentheses

SE15: Branching Statements6–66–6 Example 2 (Compound statements) if (balance >= 0) { System.out.println(“Wow, you are in credit”); balance = balance + (INTEREST_RATE * balance); } else { System.out.println(“Call us to discuss a loan”); balance = balance - OVERDRAWN_PENALTY; } Example 3 (Omitting else) if (balance >= 0) balance = balance + (INTEREST_RATE * balance);

SE15: Branching Statements6–76–7 Boolean Expressions An expression which is either true or false time < time_limit balance <= 0 You can form more complicated expressions from simpler ones by joining them with the Java version of “and”, && if ((pressure > min ) && (pressure < max)) System.out.println(“Pressure is OK.”); else System.out.println(“Warning!!”); Note: you cannot use min < pressure < max

SE15: Branching Statements6–86–8 Java Comparison Operators Math Notation NameJava Notation Examples =Equal to ==balance == 0 ≠Not equal to !=answer != ‘y’ >Greater than >cost > balance ≥Greater than or equal to >=marks >= 70 <Less than <balance < 0 ≤Less than or equal to <=cost <= balance

SE15: Branching Statements6–96–9 You can also use “or” to combine simpler expressions, || if ((salary > phoneBill) || (savings > phoneBill)) System.out.println(“You can pay the bill”); else System.out.println(“Get rid of the phone”); You can negate an expression using ! if (!(number >= min)) System.out.println(“Too small”); else System.out.println(“OK”); Note: avoid ! to improve readability

SE15: Branching Statements6–10 Nested Statements You can use one if-else statement within another if (balance >= 0) if (INTEREST_RATE >= 0) balance = balance + (INTEREST_RATE * balance); else System.out.println(“Error: negative interest”); else balance = balance - OVERDRAWN_PENALTY ;

SE15: Branching Statements6–11 Multibranch if-else Statements If you have the ability to branch two ways, then you have the ability to branch four or more ways. Just branch two ways and then have each of these outcomes branch two ways…… if (balance > 0) System.out.println(“Positive balance”); else if (balance < 0) System.out.println(“Negative balance”); else if (balance == 0) System.out.println(“Zero balance”); Savtich p145

SE15: Branching Statements6–12 The switch statement A multi-way branch that makes its decision based on the value of an integer or character expression The switch statement begins with the keyword switch followed by a controlling expression in parentheses switch (seatLocationCode) This is followed by a list of cases, consisting of the keyword case and a case label followed by the actions for each case case 1: System.println(“Orchestra”); Savtich p149

SE15: Branching Statements6–13 Example of switch switch (seatLocationCode) { case 1: System.out.println(“Orchestra”); price = 40.0; break; case 2: System.out.println(“Upper Circle”); price = 50.0; break; default: System.out.println(“Unknown Ticket Code”); break; }

SE15: Branching Statements6–14 Summary Looked at the using branching to control the flow of a program. Met if-else Boolean Expressions Multibranch if-else switch statement

SE15: Branching Statements6–15 Follow-up Work Savitch chapter 3 How do you test if two objects are equal, such as two strings? Which methods do you require to determine the alphabetical order of two strings? What does the following conditional operator do? int max, n1, n2; max = (n1 > n2) ? n1 : n2;