Controlling Execution 2016-6-31Dong Shao, Nanjing Unviersity.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Statement-Level Control Structures
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
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.
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
The Flow of Control Among Statements.  Selection Statements  Iterative Statements  Unconditional Branching  Guarded Commands.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Control Flow C and Data Structures Baojian Hua
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120 Introduction to Computer Science Iteration (Looping)
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
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.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Flow of Control.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Flow of Control.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

Controlling Execution Dong Shao, Nanjing Unviersity

true and false All conditional statements use the truth or falsehood of a conditional expression to determine the execution path. An example of a conditional expression is a == b. This uses the conditional operator == to see if the value of a is equivalent to the value of b. The expression returns true or false Dong Shao, Nanjing Unviersity

if-else The if-else statement is the most basic way to control program flow. The else is optional, so you can use if in two forms: Dong Shao, Nanjing Unviersity

The Boolean-expression must produce a boolean result. The statement is either a simple statement terminated by a semicolon, or a compound statement, which is a group of simple statements enclosed in braces. Although Java, like C and C++ before it, is a “free-form” language, it is conventional to indent the body of a control flow statement so the reader can easily determine where it begins and ends Dong Shao, Nanjing Unviersity4

Iteration Looping is controlled by while, do- while and for, which are sometimes classified as iteration statements. A statement repeats until the controlling Boolean-expression evaluates to false Dong Shao, Nanjing Unviersity5

while The form for a while loop is: The Boolean-expression is evaluated once at the beginning of the loop and again before each further iteration of the statement Dong Shao, Nanjing Unviersity6

do-while The form for do-while is : The sole difference between while and do-while is that the statement of the do-while always executes at least once, even if the expression evaluates to false the first time Dong Shao, Nanjing Unviersity7

for A for loop is perhaps the most commonly used form of iteration. This loop performs initialization before the first iteration. Then it performs conditional testing and, at the end of each iteration, some form of “stepping.” Dong Shao, Nanjing Unviersity8

The form of the for loop is: Any of the expressions initialization, Boolean-expression or step can be empty. The expression is tested before each iteration, and as soon as it evaluates to false, execution will continue at the line following the for statement. At the end of each loop, the step executes Dong Shao, Nanjing Unviersity9

The comma operator The comma operator (not the comma separator, which is used to separate definitions and method arguments) has only one use in Java: in the control expression of a for loop. In both the initialization and step portions of the control expression, you can have a number of statements separated by commas, and those statements will be evaluated sequentially Dong Shao, Nanjing Unviersity10

Using the comma operator, you can define multiple variables within a for statement, but they must be of the same type Dong Shao, Nanjing Unviersity11

Foreach syntax Java SE5 introduces a new and more succinct for syntax, for use with arrays and containers. The foreach syntax not only saves time when typing in code. More importantly, it is far easier to read and says what you are trying to rather than giving the details of how you are doing it Dong Shao, Nanjing Unviersity12

return Several keywords represent unconditional branching, which simply means that the branch happens without any test. The return keyword has two purposes: It specifies what value a method will return (if it doesn’t have a void return value) and it causes the current method to exit, returning that value Dong Shao, Nanjing Unviersity13

break and continue You can also control the flow of the loop inside the body of any of the iteration statements by using break and continue. break quits the loop without executing the rest of the statements in the loop. continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration Dong Shao, Nanjing Unviersity14

The infamous “goto” The goto keyword has been present in programming languages from the beginning. Indeed, goto was the genesis of program control in assembly language. A goto is a jump at the source-code level, and that’s what brought it into disrepute Dong Shao, Nanjing Unviersity15

The problem is not the use of goto, but the overuse of goto; in rare situations goto is actually the best way to structure control flow. Although goto is a reserved word in Java, it is not used in the language; Java has no goto. However, it does have something that looks a bit like a jump tied in with the break and continue keywords. It’s not a jump but rather a way to break out of an iteration statement. The reason it’s often thrown in with discussions of goto is because it uses the same mechanism: a label Dong Shao, Nanjing Unviersity16

A label is an identifier followed by a colon, like this: The only place a label is useful in Java is right before an iteration statement. And that means right before Dong Shao, Nanjing Unviersity17

That’s because the break and continue keywords will normally interrupt only the current loop, but when used with a label, they’ll interrupt the loops up to where the label exists. If not for the break outer statement, there would be no way to get out of the outer loop from within an inner loop, since break by itself can break out of only the innermost loop. (The same is true for continue.) Dong Shao, Nanjing Unviersity18

Rule for break and continue 1. A plain continue goes to the top of the innermost loop and continues. 2. A labeled continue goes to the label and reenters the loop right after that label. 3. A break “drops out of the bottom” of the loop. 4. A labeled break drops out of the bottom of the end of the loop denoted by the label Dong Shao, Nanjing Unviersity19

switch The switch is sometimes called a selection statement. The switch statement selects from among pieces of code based on the value of an integral expression. Its general form is Dong Shao, Nanjing Unviersity20

Integral-selector is an expression that produces an integral value. The switch compares the result of integral- selector to each integral-value. If it finds a match, the corresponding statement (a single statement or multiple statements; braces are not required) executes. If no match occurs, the default statement executes Dong Shao, Nanjing Unviersity21

You will notice in the preceding definition that each case ends with a break, which causes execution to jump to the end of the switch body. This is the conventional way to build a switch statement, but the break is optional. If it is missing, the code for the following case statements executes until a break is encountered Dong Shao, Nanjing Unviersity22

Summary Fundamental features that appear in most programming languages: calculation, operator precedence, type casting, and selection and iteration Dong Shao, Nanjing Unviersity23