Flow of Control.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

3-1 Chapter 3 Flow of Control (part a - branching)
The if-else Statements
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
Lecture 6 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to 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.
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)
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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 prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
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,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 3 Edited by JJ Shepherd
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
Controlling Execution Dong Shao, Nanjing Unviersity.
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.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
COMP Loop Statements Yi Hong May 21, 2015.
Flow of Control Chapter 3. JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
COS 260 DAY 5 Tony Gauvin.
CNG 140 C Programming (Lecture set 3)
Selections Java.
Chapter 3 Edited by JJ Shepherd
Lecture 3- Decision Structures
Chapter 3 Branching Statements
Boolean Expressions and If
DKT121: Fundamental of Computer Programming
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,
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Flow of Control Chapter 3 Chapter 3.
2-1 Making Decisions Sample assignment statements
Flow of Control Chapter 3.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 4: Control Structures I (Selection)
Week 3 – Program Control Structure
Flow of Control Chapter 3.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Controlling Program Flow
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
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

Flow of Control

Objectives To learn about branching statements

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 chooses between two or more possible actions. A loop statement repeats an action until a stopping condition occurs.

Branching Statements: Outline The if-else Statement Introduction to Boolean Expressions Nested Statements and Compound Statements Multibranch if-else Statements The switch Statament (optional) The Conditional Operator

The if-else Statement syntax if (Boolean_Expression) Statement_1 else

The if-else Statement, cont. if (grade <= 3) total = 0; else total = total + grade;

Compound Statements To include multiple statements in a branch, enclose the statements in braces. if (count < 3) { total = 0; count = 0; }

Omitting the else Part If the else part is omitted and the expression after the if is false, no action occurs. syntax if (Boolean_Expression) Statement example if (weight > ideal) caloriesPerDay -= 500;

Introduction to Boolean Expressions The value of a boolean expression is either true or false. examples time < limit balance <= 0

Compound Boolean Expressions Boolean expressions can be combined using the “and” (&&) operator. example if ((score > 0) && (score <= 100)) ... not allowed if (0 < score <= 100)

Compound Boolean Expressions, cont. syntax (Sub_Expression_1) && (Sub_Expression_2) Parentheses often are used to enhance readability. The larger expression is true only when both of the smaller expressions are true.

Compound Boolean Expressions, cont. Boolean expressions can be combined using the “or” (||) operator. example if ((quantity > 5) || (cost < 10)) ... syntax (Sub_Expression_1) || (Sub_Expression_2)

Compound Boolean Expressions, cont. The larger expression is true when either of the smaller expressions is true when both of the smaller expressions are true.

Nested Statements An if-else statement can contain any sort of statement within it. In particular, it can contain another if-else statement. An if-else may be nested within the “if” part. An if-else may be nested within the “else” part. An if-else may be nested within both parts.

Nested Statements, cont. syntax if (Boolean_Expression_1) if (Boolean_Expression_2) Statement_1 else Statement_2 if (Boolean_Expression_3) Statement_3

Nested if Example if (temperature > 90) // int temperature if (sunny) // boolean sunny printf(“Beach”); else printf(“Movie”); if (sunny) printf(“Tennis”); printf(“Volleyball”);

Nested Statements, cont. Each else is paired with the nearest unmatched if. Indentation can communicate which if goes with which else. Braces are used to group statements.

Nested Statements, cont. Different indentation first form second form if (a > b) if (a > b) if (c > d) if (c > d) e = f; e = f; else else g = h; g = h; Same to the compiler!

Compound Statements When a list of statements is enclosed in braces ({}), they form a single compound statement. syntax { Statement_1; Statement_2; … }

Compound Statements, cont. A compound statement can be used wherever a statement can be used. example if (total > 10) { sum = sum + total; total = 0; }

Multibranch if-else Statements syntax if (Boolean_Expression_1) Statement_1 else if (Boolean_Expression_2) Statement_2 else if (Boolean_Expression_3) Statement_3 else if … else Default_Statement

Multibranch if-else Statements, cont. class Grader

Multibranch if-else Statements, cont. equivalent logically if (score >= 90) grade = ‘A’; if ((score >= 80) && (score < 90)) grade = ‘B’; if ((score >= 70) && (score < 80)) grade = ‘C’; if ((score >= 60) && (score < 70)) grade = ‘D’; if (score < 60) grade = ‘F’;

switch Statement The switch statement is a multiway branch that makes a decision based on an integral (integer or character) expression. The switch statement begins with the keyword switch followed by an integral expression in parentheses and called the controlling expression.

switch Statement, cont. A list of cases follows, enclosed in braces. Each case consists of the keyword case followed by a constant called the case label a colon a list of statements. The list is searched for a case label matching the controlling expression.

switch Statement, cont. The action associated with a matching case label is executed. If no match is found, the case labeled default is executed. The default case is optional, but recommended, even if it simply prints a message. Repeated case labels are not allowed.

switch Statement, cont. The action for each case typically ends with the word break. The optional break statement prevents the consideration of other cases. The controlling expression can be anything that evaluates to an integral type (integer or character).

Switch with char Type char grade = 'A'; switch(grade) { case 'A': case 'B': case 'C': case 'D':printf("Pass"); break; case 'W':printf("Withheld"); case 'I':printf("Incomplete"); default:printf("Fail"); }

switch Statement, cont. The action for each case typically ends with the word break. The optional break statement prevents the consideration of other cases. The controlling expression can be anything that evaluates to an integral type (integer or character).

Conditional Operator if (n1 > n2) max = n1; else max = n2; can be written as max = (n1 > n2) ? n1 : n2; The ? and : together is called the conditional operator (a ternary operator). Note (n1 > n2) ? n1 : n2 is an expression that has a value unlike the “normal” if statement

Conditional Operator, cont. The conditional operator can be useful with print statements. System.out.print(“You worked “ + hours + “ “ + ((hours > 1) ? “hours” : “hour”));

Summary of branching if statement (1 or 2 branches) Multi-branch if-else-if statement (3 or more branches) Multi-branch switch statement Conditional operator ? :

Summary You have learned about Java branching statements.