Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

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.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
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.
Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
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)
true (any other value but zero) false (zero) expression Statement 2
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
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.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision Making Selection structures (if....else and switch/case)
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
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.
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.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
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.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
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.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
CPS120: Introduction to Computer Science Decision Making in Programs.
Statements and Control Flow The programs we have seen so far do exactly the same list of instructions every time What if we want to do different things.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Java Programming Fifth Edition
Computer Programming with Java
The switch Statement, and Introduction to Looping
Chapter 3 Edited by JJ Shepherd
Flow of Control.
Chapter 3 Branching Statements
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Selection (if-then-else)
Control Structures: Selection Statement
Chapter 4: Control Structures I (Selection)
Chapter 2 Programming Basics.
PROGRAM FLOWCHART Selection Statements.
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.
CSS 161: Fundamentals of Computing
Presentation transcript:

Branch Statements (Decision)

Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible actions.  A loop statement (to be discussed later) repeats an action again and again until some stopping condition is met.

The if-else Statement  Example: if (balance >= 0) balance = balance +(INTEREST_RATE * balance)/12; else balance = balance – OVERDRAWN_PENALTY;  If the condition (balance >= 0) is true, the if part of the statement is executed. If it is false the else part is executed.

The if-else Statement (cont’d)  If the else part is omitted nothing happens if the condition is false.  Example: if (weight > ideal) calorieAllotment = calorieAllotment -500;

Boolean Expressions  The condition in an if-else statement is a boolean expression.  A boolean expression is simply an expression that is either true or false.  The simplest boolean expressions are comparisons of two expressions, like time < limit, or balance <= 0

Boolean Expressions (cont’d)  Far more complicated boolean expressions are possible.  Examples: if ((pressure > min) && (pressure < max))... if ((a + b < c) || (a + c < b) || (b + c < a))...

Java Comparison Operators Math NotationNameJava NotationJava Examples = Equal to == balance == 0 ≠ Not equal to != income != tax > Greater than > cost > value ≥ Greater than or equal to >= points >= 60 < Less than < pressure < max ≤ Less than or equal to <= cost <= value

Java Boolean Operators Logical Notation NameJava Notation Java Examples ^and&& rich && famous vor|| win || lose ¬not! !married

Nested if-else Statements  if-else statements can be nested within other if-else statements  Example: if (balance >= 0) if (INT_RATE >= 0) balance = balance +(INT_RATE * balance)/12; else System.out.println(“Negative int error”); else balance = balance – OVERDRAWN_PENALTY;

Compound Statements  If the if or the else part of the branching statement can not be expressed in a single statement, the multiple statements needed can be grouped together by putting braces { } around them. They are then treated as single compound statement. { system.out.println(“Good for you.”); balance = balance +(INT_RATE * balance)/12; }

Multibranch if-else Statements Example: 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”);

The Switch Statement  The switch statement is a multiway branch that makes its decision as to which way to branch based on the value of an integer or character expression.  The switch statement begins with the keyword switch followed by an expression in parentheses.  Below this is a list of cases enclosed in braces, each case consisting of the keyword case followed by a constant, then a colon, and then a list of statements which are the actions for that case.

Switch Example switch (numberOfBabies) { case 1: case 2: System.out.println(“Congratulations.”); break; case 2: System.out.println(“Wow. Twins.”); break; case 3: System.out.println(“Wow. Triplets.”); break; default: System.out.println(“Unbelievable.”); break; }