CSS 161: Fundamentals of Computing

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
3-1 Chapter 3 Flow of Control (part a - branching)
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Chapter 3 Flow of Control Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
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,
Chapter 3 Flow of Control Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008 Pearson.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Flow of Control (part 2) Joe McCarthy CSS 161: Fundamentals of Computing.
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.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Week 4 Program Control Structure
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Switch statement.
Chapter 3 Flow of Control
Chapter 3 Flow of Control
CNG 140 C Programming (Lecture set 3)
SWE 510: Object Oriented Programming in Java
Midterm Review.
Boolean expressions and if-else statements
Selections Java.
5.3 Multiple Alternatives (Part 1)
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Chapter 3 Branching Statements
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 6: Conditional Statements and Loops
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Control Structures – Selection
Part II The Switch Statement
CSS 161 Fundamentals of Computing Introduction to Computers & Java
CMSC 202 Java Primer 2.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Flow of Control
Chapter8: Statement-Level Control Structures April 9, 2019
Classes, Objects and Methods
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Announcements Program 1 due noon Lab 1 due noon
Controlling Program Flow
CprE 185: Intro to Problem Solving (using C)
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

CSS 161: Fundamentals of Computing Flow of Control (part 1) Joe McCarthy CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing Outline Updates / reminders Today: Finish Lab 2 (problem 4) Start Chapter 3: Branching mechanisms (3.1) Next time (in classroom UW2-031) Continue Chapter 3: Boolean expressions (3.2) Loops (3.3) Review Assignment 1 CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing Updates / Reminder UW1-120 Mondays (“lectures”) & Fridays (labs) Wednesdays: back in classroom (UW2-031) Lab 2 Due Friday, Oct 14, 11am Assignment 2 (available today) Due Monday, Oct 17, 11am CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing Lab 2 CSS 161: Fundamentals of Computing

Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Flow of Control As in most programming languages, flow of control in Java refers to its branching and looping mechanisms Java has several branching mechanisms: if-else, if, and switch statements Java has three types of loop statements: the while, do-while, and for statements Most branching and looping statements are controlled by Boolean expressions A Boolean expression evaluates to either true or false The primitive type boolean may only take the values true or false Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Branching with an if-else Statement An if-else statement chooses between two alternative statements based on the value of a Boolean expression if (Boolean_Expression) Yes_Statement else No_Statement The Boolean_Expression must be enclosed in parentheses If the Boolean_Expression is true, then the Yes_Statement is executed If the Boolean_Expression is false, then the No_Statement is executed Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Compound Statements Each Yes_Statement and No_Statement branch of an if-else can be a made up of a single statement or many statements Compound Statement: A branch statement that is made up of a list of statements A compound statement must always be enclosed in a pair of braces ({ }) A compound statement can be used anywhere that a single statement can be used Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Compound Statements if (myScore > yourScore) { System.out.println("I win!"); wager = wager + 100; } else System.out.println ("I wish these were golf scores."); wager = 0; Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Compound Statements if (myScore > your Score) { System.out.println("I win!"); wager = wager + 100; } else System.out.println("I wish these were golf scores."); wager = 0; More compact formatting option: if (myScore > your Score) { System.out.println("I win!"); wager = wager + 100; } else { System.out.println("I wish these were golf scores."); wager = 0; } Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Omitting the else Part The else part may be omitted to obtain what is often called an if statement if (Boolean_Expression) Action_Statement If the Boolean_Expression is true, then the Action_Statement is executed The Action_Statement can be a single or compound statement Otherwise, nothing happens, and the program goes on to the next statement if (weight > ideal) calorieIntake = calorieIntake – 500; Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Nested Statements if-else statements and if statements both contain smaller statements within them For example, single or compound statements In fact, any statement at all can be used as a subpart of an if-else or if statement, including another if-else or if statement Each level of a nested if-else or if should be indented further than the previous level Exception: multiway if-else statements Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Multiway if-else Statements The multiway if-else statement is simply a normal if-else statement that nests another if-else statement at every else branch It is indented differently from other nested statements All of the Boolean_Expressions are aligned with one another, and their corresponding actions are also aligned with one another The Boolean_Expressions are evaluated in order until one that evaluates to true is found The final else is optional Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Multiway if-else Statement if (Boolean_Expression) Statement_1 else if (Boolean_Expression) Statement_2 else if (Boolean_Expression_n) Statement_n else Statement_For_All_Other_Possibilities . . . Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

The switch Statement The switch statement is the only other kind of Java statement that implements multiway branching When a switch statement is evaluated, one of a number of different branches is executed The choice of which branch to execute is determined by a controlling expression enclosed in parentheses after the keyword switch The controlling expression must evaluate to an ordinal type: char, int, short, or byte Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

The switch Statement Each branch statement in a switch statement starts with the reserved word case, followed by a constant called a case label, followed by a colon, and then a sequence of statements Each case label must be of the same type as the controlling expression Case labels need not be listed in order or span a complete interval, but each one may appear only once Each sequence of statements may be followed by a break statement ( break;) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

The switch Statement There can also be a section labeled default: The default section is optional, and is usually last Even if the case labels cover all possible outcomes in a given switch statement, it is still a good practice to include a default section It can be used to output an error message, for example When the controlling expression is evaluated, the code for the case label whose value matches the controlling expression is executed If no case label matches, then the only statements executed are those following the default label (if there is one) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

The switch Statement The switch statement ends when it executes a break statement, or when the end of the switch statement is reached When the computer executes the statements after a case label, it continues until a break statement is reached If the break statement is omitted, then after executing the code for one case, the computer will go on to execute the code for the next case If the break statement is omitted inadvertently, the compiler will not issue an error message Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

The switch Statement switch (Controlling_Expression) { case Case_Label_1: Statement_Sequence_1 break; case Case_Label_2: Statement_Sequence_2 case Case_Label_n: Statement_Sequence_n default: Default_Statement Sequence } . . . Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

The Conditional Operator The conditional operator is a notational variant on certain forms of the if-else statement Also called the ternary operator or arithmetic if The following examples are equivalent: if (n1 > n2) max = n1; else max = n2; vs. max = (n1 > n2) ? n1 : n2; The expression to the right of the assignment operator is a conditional operator expression If the Boolean expression is true, then the expression evaluates to the value of the first expression (n1), otherwise it evaluates to the value of the second expression (n2) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Next Time Back in classroom (UW2-031) Continue Chapter 3 Every Wednesday Continue Chapter 3 Boolean Expressions (3.2) Start Loops (3.3) Review Assignment 1