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.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

CS0007: Introduction to Computer Programming
Control Structures Corresponds with Chapters 3 and 4.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Branching Loops exit(n) method Boolean data type and expressions
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison 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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ITM352 Conditionals Lecture #5. 1/28/03ITM352 Fall 2003 Class 5 – Control Flow 2 Announcements r Assignment 1 m Create a Zip file of your JBuilder project.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
Flow of Control Part 1: Selection
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.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Controlling Execution Dong Shao, Nanjing Unviersity.
Lecture 4 – PHP Flow Control SFDV3011 – Advanced Web Development 1.
Flow of Control Chapter 3 Flow of control Branching Loops
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Introduction To Scientific Programming Chapter 3.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Introduction to Java Java Translation Program Structure
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.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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 statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Lecturer: Dr. AJ Bieszczad Chapter 3 COMP 150: Introduction to Object-Oriented Programming 3-1 l Branching l Loops l exit(n) method l Boolean data type.
COMP Loop Statements Yi Hong May 21, 2015.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Branching statements.
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Chapter 3 Branching Statements
ITM 352 Flow-Control: if and switch
JavaScript: Control Statements.
Branching Loops exit(n) method Boolean data type and expressions
Chapter 4: Control Structures I (Selection)
Web Programming– UFCFB Lecture 20
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Branching statements Kingdom of Saudi Arabia
Announcements/Reminders
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:

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 three control flow elements: 1. Sequence - just go to the next instruction 2. Branching or Selection - a choice of at least two –either go to the next instruction –or jump to some other instruction 3. Loop or Repetition - a loop (repeat a block of code) at the end of the loop –either go back and repeat the block of code –or continue with the next instruction after the block

3 Java Flow Control Statements Sequence l the default l Java automatically executes the next instruction unless you use a branching statement Branching l if l if-else l if-else if-else if- … - else l switch Loop l while l do-while l for

4 Java if Statement l Simple selection l Do the next statement if test is true or skip it if false l Syntax: if (Boolean_Expression) Action if true; //execute only if true next action; //always executed l Note the indentation for readability (not compile or execution correctness)

5 if Example l The body of the if statement is conditionally executed l Statements after the body of the if statement always execute if(eggsPerBasket < 12) //begin body of the if statement field.setText(“Less than a dozen eggs per basket”); //end body of the if statement totalEggs = numberOfEggs * eggsPerBasket; field.setText(“You have a total of ” + totalEggs + “ eggs.”);

6 Multiple Statements Action if true can be either a single Java statement or a set of statements enclosed in braces { }. l A set of statements in braces is called a compound statement and can be used anywhere a single statement can be used. if(eggsPerBasket < 12) { //begin body of the if statement field.setText(“Less than a dozen...”); costPerBasket = 1.1 * costPerBasket; } //end body of the if statement totalEggs = numberOfEggs * eggsPerBasket; field.setText(“You have a total of ” + totalEggs + “ eggs.”); All statements between braces are controlled by if

7 Two-way Selection: if-else l Select either one of two options l Either do Action1 or Action2, depending on test value l Syntax: if (Boolean_Expression) { Action1 //execute only if true } else { Action2//execute only if false } Action3//always executed

8 if-else Examples l Example with single-statement blocks: if(time < limit) field.setText(“You made it.”); else field.setText(“You missed the deadline.”); l Example with compound statements: if(time < limit) { field.setText(“You made it.”); bonus = 100; } else { field.setText(“You missed the deadline.”); bonus = 0; }

9 Definition of Boolean Values l Branching: there is more than one choice for the next instruction l Which branch is taken depends on a test condition which evaluates to either true or false l In general: if test is true then do this, otherwise it is false, do something else l Variables (or expressions) that are either true or false are called boolean variables (or expressions) So, the value of a boolean variable (or expression) is either true or false boolean is a primitive data type in Java

10 Boolean Expressions l Boolean expressions can be thought of as test conditions (questions) that are either true or false l Often two values are compared l For example: Is A greater than B? Is A equal to B? Is A less than or equal to B? etc. l In most cases, A and B can be any data type. But they should be the same data type, or it should be possible to promote one to another.

11 Java Comparison Operators

12 Java Comparison Methods for String Class l “==“ does not do what you may think for String objects »When “==“ is used to test objects (such as String objects) it tests to see if the storage addresses of the two objects are the same –are they stored in the same location? –more will be said about this later l Use “equals” method to test if the strings, themselves, are equal String s1 = “Purdue”; String s2; s2 = field.getText (); //s1.equals(s2) returns true if the user enters Purdue, false otherwise equals() is case sensitive Use equalsIgnoreCase() to ignore case

13 Nested if Statements One if statement can have another if statement inside it. These are called nested if statements. l Inner statements are indented more than outer statements. if (balance >= 0) if (RATE >= 0) balance = balance + (RATE * balance)/12; else field.setText("Cannot have negative rate"); else balance = balance – OVERDRAWN_PENALTY; inner statement outer statement The inner statement will be skipped entirely if balance >= 0 is false.

14 Multibranch selection: if-else if-else if-…-else l One way to handle situations with more than two possibilities l Syntax: if(Boolean_Expression_1) Action_1 else if(Boolean_Expression_2) Action_2. else if(Boolean_Expression_n) Action_n else Default_Action

15 if-else if-else if-…-else Example 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'; Note indentation. Even though these are nested if statements, they are all indented the same amount to indicate a multibranch selection.

16 Multibranch selection: switch l Another way to program multibranch selection Uses Controlling_Expression to decide which way to branch Controlling_Expression must be char, int, short, or byte. Controlling_Expression and Case_Label must be same type. switch( Controlling_Expression ) { case Case_Label: statements … break; case Case_Label: statements … break; default: statements … break; }

17 Multibranch selection: switch When a break statement is encountered, control goes to the first statement after the switch. break may be omitted if it is appropriate to use statements from the next case l Can have any number of cases default case is optional switch( Controlling_Expression ) { case Case_Label: statements … break; case Case_Label: statements … break; default: statements … break; }

18 switch Example switch(seatLocationCode) { case 1: field.setText(“Orchestra”); price = 40.00; break; case 2: field.setText(“Mezzanine”); price = 30.00; break; case 3: field.setText(“Balcony”); price = 15.00; break; default: field.setText(“Unknown seat code”); break; } Output if seatLocationCode is 2: Mezzanine