Programming Techniques

Slides:



Advertisements
Similar presentations
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Advertisements

CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
ISBN Chapter 8 Statement-Level Control Structures.
UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
CS0004: Introduction to Programming Repetition – Do Loops.
Chapter 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
Control Flow C and Data Structures Baojian Hua
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
1) Fixed count loops 2) Variable count loops 3) Post-test Loops 4) Terminating a loop COMP205 IMPERATIVE LANGUAGES 11. PROGRAM CONSTRUCTS 2 (REPETITION)
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
TYPE EQUIVALENCE 1) Coercion 2) Casting 3) Conversion.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
sequence of execution of high-level statements
Controlling Execution Dong Shao, Nanjing Unviersity.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Control Structures sequence of execution of high-level statements.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
W E E K F I V E Statement-Level Control Structures.
LECTURE 18 Control Flow. CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Imperative Programming Statements and invariants.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Lecture 18 Control Flow.
Def: A control structure is a control statement and
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.
Think What will be the output?
Quick Test What do you mean by pre-test and post-test loops in C?
Programming Fundamentals
Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We.
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
A First Book of ANSI C Fourth Edition
Understanding the Three Basic Structures
Chapter 2.1 Repetition.
Three Special Structures – Case, Do While, and Do Until
Chapter 6: Repetition Statements
Chapter8: Statement-Level Control Structures April 9, 2019
Flow of Control.
The structure of programming
Thinking procedurally
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Programming Techniques Programming Constructs Sequence, Iteration and Branching (Selection)

Sequence A sequence construct tells the processor which statement is to be executed next. By default this is the statement following the current statement (or the first statement in the program). If we wish to "jump" to some other statement we can use a goto statement.

Disadvantages Goto statements make it difficult to "trace" a program's execution and to determine the state of variables at a given point during processing. As a result errors are often obscure and difficult to locate. For this reason some more modern (post mid 1960's) imperative languages, for example Modula-2 and Pascal, have abandoned the goto statement all together. However, there are some legitimate reasons why a goto may be desirable, for example to facilitate error handling or to terminate a deeply nested sequence of loops.

Repetition or iteration A repetition construct causes a group of one or more program statements to be invoked repeatedly until some end condition is met. Typically such constructs are used to step through arrays or linked lists. We can identify two main forms of repetition: Fixed count loops - repeat a predefine number of times. Variable count loops - repeat an unspecified number of times. To which we could add recursion (routine calls itself). Recursion is not used so much in imperative languages, although it is the principal program construct used to achieve repetition in logic and functional languages, thus we will confine ourselves in the following discussion to fixed and variable count loops.

Pretest and posttest loops Both fixed and variable count loops can be further classified according to whether they are pretest or posttest loops. In the case of a pretest loop (also referred to as an entrance-controlled loop) the end condition is tested for prior to each repetition, while in the case of a posttest loop (also referred to as an exit-controlled loop) the end condition is tested for after each repetition.

Statements A selection statement provides for selection between alternatives. We can identify three types of selection construct: If statements Case statements Pattern matching

IF STATEMENT An if statement, sometimes referred to as a conditional, can be used in two forms: If condition then action1 If condition then action1 else action2

Terminating a Loop Sometimes there is a need to terminate a loop somewhere in the middle. Many languages therefore support a break (C) or exit (Ada) statement. Ada actually supplies two types of exit statement, exit and exit when.

NESTING Nesting may refer to inserting a graphic image into a word processor. 2. With computer programming, A nested function is a function which is contained inside of another function within the source code of a program.