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

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Control Structures.
3-1 Chapter 3 Flow of Control (part a - branching)
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
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.
Lecture 6 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.
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)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4: Control Structures: Selection
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
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 (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):
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
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.
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.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
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.
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 Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
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 statements Mostafa Abdallah
 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 Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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
Chapter 3: Decisions and Loops
Chapter 3 Edited by JJ Shepherd
Flow of Control.
Chapter 3 Branching Statements
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
Topics The if Statement The if-else Statement Comparing Strings
2-1 Making Decisions Sample assignment statements
Selection (if-then-else)
Control Structures: Selection Statement
Chapter 4: Control Structures I (Selection)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
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 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming

SE15: Branching Statements7–27–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 Statements7–37–3 Lecture Outline Flow of Control Branching Statements The if statement The if-else statement Boolean expressions Multi branch if-else statements The switch statement

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

SE15: Branching Statements7–57–5 The if statement An example if statement if (total > 30) System.out.println(“Count exceeded”); The condition in the statement is total > 30. This expression must evaluate to a boolean result (true or false). If it is true the println statement is executed, if not the println statement is skipped. The condition is enclosed in parentheses.

SE15: Branching Statements7–67–6 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 Statements7–77–7 Block or Compound Statements You may want to do more than one thing as the result of evaluating a boolean condition. In java we can replace the single statement with a block statement. A block statement is a collection of statements enclosed in parentheses.

SE15: Branching Statements7–87–8 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; }

SE15: Branching Statements7–97–9 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”, && (logical operator) 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 Statements7–10 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 Statements7–11 Logical Operators 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 Statements7–12 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 Statements7–13 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…… A example of a three way branch 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 Statements7–14 The switch statement A multi-way branch that makes its decision based on the value of an integer or character expression by matching that value with one of several possible cases. 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 Statements7–15 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 Statements7–16 Summary Looked at the using branching to control the flow of a program. Met if if-else Boolean Expressions Multibranch if-else switch statement

SE15: Branching Statements7–17 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;