Copyright © 2004-2013 Curt Hill Flow of Control A Quick Overview.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Control Structures II (Repetition)
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
CPS120 Introduction to Computer Science Iteration (Looping)
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright Curt Hill A Quick Introduction to Looping Breadth not Depth.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
Controlling Execution Dong Shao, Nanjing Unviersity.
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,
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
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.
Control Structures sequence of execution of high-level statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
CPS120 Introduction to Computer Science Iteration (Looping)
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Statement Level Flow of Control GOTOs and Other Weirdness Copyright © by Curt Hill.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
The for Statement A most versatile loop
Flow of Control An Overview
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Def: A control structure is a control statement and
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 8: Control Structures
Chapter 4 Control Statements: Part I
Concepts From Alice Switching to Java Copyright © Curt Hill.
Control Structures In Text: Chapter 8.
Structured Program
CMSC 202 Java Primer 2.
Compound Statements A Quick Overview
The Java switch Statement
Classes, Objects and Methods
Introduction to Programming
The IF Revisited A few more things Copyright © Curt Hill.
The IF Revisited A few more things Copyright © Curt Hill.
Methods Scope How are names handled?
Methods Coding in Java Copyright © Curt Hill.
While and Do While Syntax, semantics and examples
Presentation transcript:

Copyright © Curt Hill Flow of Control A Quick Overview

Copyright © Curt Hill Up to Now… We can only write programs that execute each and every statement one time only No conditionality and no repetition –We might get repetition by calling a method multiple times Not much call for these types of programs Mostly –Formula evaluations –Simple pictures

Copyright © Curt Hill Flow of control The idea of this is how execution moves through a program Most programming languages support three types of flow of control –Sequential –Decision –Iteration Java is no exception to this

Copyright © Curt Hill Flowchart Representation Sequential DecisionIteration

Copyright © Curt Hill The Three Types Sequential flow –Each statement is executed once and only once Decision –A test determines which path to take –Only one of the paths may be taken –Usually a boolean choosing one of two paths Iteration –A test determines if the path is to be executed zero or more times

Copyright © Curt Hill A few more things Other possibilities do exist, but are not needed Bohm and Jacoppini proved in the 60s that these three are all that is needed to write any program Most languages developed since then have these three in variations All the statements covered so far are sequential

Copyright © Curt Hill Java Flow of Control Has two decision statements –If –Switch Case Has four loops –for (two types) –while –do Also the break and continue statements

Copyright © Curt Hill Flowcharts Again The rectangle represents any series of statements that a single entry point and a single exit point The decision and iteration are also single entry and single exit So in any flowchart a rectangle can be replaced by a decision or iteration This can be done to any depth

Copyright © Curt Hill The Compound Statement The rectangle does have a corresponding construction in the C family of languages – the compound statement The compound statement is just a pair of braces { } and any number of statements in between them It is also the executable portion of a method It allows us to wrap many statements and treat them as just one

Copyright © Curt Hill Compound statements Start with a { and end with a } No semicolon follows the closing brace Any place that one statement can be a compound statement can also be Primitive variables declared in a compound statement last until end of statement Objects may survive, but not their handles

Copyright © Curt Hill Review Scope That area where a variable is known The scope of a primitive starts with its declaration It ends with the enclosing brace Object handles work the same way, but the object itself may survive the compound statement Java has more complicated scope than some other languages

Copyright © Curt Hill Duplicate Names Some languages like FORTRAN and many versions of BASIC only allow one instance of a name Thus code like this: int a=2; … double a = 3.4; would be illegal regardless of where in the program these lines occur Java has a rather more complicated rule

Copyright © Curt Hill A Scope Block A Scope Block is the area of a compound statement –Excluding any nested compound statements Blocks may nested Within one scope block all names declared must be unique However, the same name may be used in a different block, provided it does not conflict with another available

Copyright © Curt Hill Scope Example Consider the following code: { int a=5; // first a int b = a * 2; { // new brace : new scope int c = a; System.out.print(c); System.out.println(“ “); } System.out.print(c); What will happen?

Copyright © Curt Hill Results Syntax error The c was declared inside a compound statement There was an attempt to use it after the statement The c variable only exists inside the compound statement However, it may access things declared in surrounding compound statements

Copyright © Curt Hill Finding names How does compiler connect a name with declaration? Here is what the compiler does: –Look in the current block (compound statement) for the name –If found stop looking –If not then go the next enclosing block –Keep doing this until there are no further blocks or the name is found

Copyright © Curt Hill Another Scope Example How will names be found? int x,z; … { int w,y; … { int v; … v = x * y; } … } Three scope blocks

Copyright © Curt Hill Duplicate Names Unlike its predecessor C++, Java only allows duplicate names where they do not overlap Duplicates may exist, however no code has access to both Consider next screen:

Copyright © Curt Hill Scope Example Consider the following code: { int a=5; // first a int b = a * 2; { // new brace : new scope int c = a; // one c … } { // new brace : new scope int c = b; // second c … }

Copyright © Curt Hill Discussion If you are in a scope block, you may look outside to enclosing blocks You may not look inside a scope block –The variables do not exist unless execution is inside the block These two c variables did not overlap in range

Copyright © Curt Hill Another Thought The above examples use compound statements in an unusual way The import statement adds new items to the global scope Perhaps a great many new items A name not found by the time of finishing the global scope is undefined

Copyright © Curt Hill A Little History The idea of scope was proposed in 1960 as part of the Algol language It was perceived to be so good that most languages since then have used it However, FORTRAN, COBOL, BASIC all were established before the idea caught on Most newer languages did adopt some variation: –C, C++, Java, Pascal, Ada

Copyright © Curt Hill Why use it? Scope makes large scale programming possible When using libraries a program may have hundreds to millions of variables created by programmers who did not talk to each other Since the scope rules isolate these variables we do not have to worry if this variable name has been used before Java does not need the unlimited scope because most things are declared inside a class

Copyright © Curt Hill Classes and Scope We have already seen Math.PI This is a constant defined inside the Math object Classes act like scope blocks as well with an important difference We can look inside at public items This is done by giving first the class name, a dot, then the interior name

Copyright © Curt Hill Final Thoughts It is always safe to keep your names unique within a program Upon entering a new compound statement Any name not available to you currently may be used It may be attached to any type as well Always use meaningful names One letter names are usually reserved for formulas and control variables in loops