Control Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Control Structures

Description:

A computer can be instructed to repeat instructions under certain conditions. Iteration Control Structures Iteration control structures are looping mechanisms. – PowerPoint PPT presentation

Number of Views:166
Avg rating:3.0/5.0
Slides: 18
Provided by: Paul7268
Learn more at: http://space.wccnet.edu
Category:

less

Transcript and Presenter's Notes

Title: Control Structures


1
Control Structures
  • CPS120 Introduction to Computer Science
  • Lecture 5

2
What Can a Program Do?
  • A program can only instruct a computer to
  • Read Input
  • Sequence
  • Calculate
  • Store data
  • Compare and branch
  • Iterate or Loop
  • Write Output

3
Fundamental Programming Concepts
  • Assignment of values to a variable
  • Iteration (Looping)
  • Over a set of set of statements
  • With respect to a logical expressions
    (conditions)
  • Delegation of sub-tasks to functions / procedures

4
The Structure Theorem
  • The Structure Theorem states that any algorithm
    can be built from three basic control structures.
  • One-after-another (Sequence)
  • Decision-making (Selection)
  • Making choices between 2 or more alternatives
  • Repetition (Iteration)
  • Concerned with repetitive tasks (and the
    termination conditions of loops)

5
C Control Structures
  • "Sequence statements" are imperatives
  • "Selection" is the "if then else" statement
  • AND, OR, NOT and parentheses ( ) can be used for
    compound conditions
  • "Iteration" is satisfied by a number of
    statements
  • "while"
  • " do "
  • "for"
  • The case-type statement is satisfied by the
    "switch" statement.
  • CASE statements are used for most non-trivial
    selection decisions

6
Sequence Control Structures
  • Sequence control structures direct the order of
    program instructions.
  • The fact that one instruction follows anotherin
    sequenceestablishes the control and order of
    operations.

7
Calculate
  • A program can instruct a computer to perform
    mathematical operations.

Add 1 to Counter
8
Store
  • A program will often instruct a computer to store
    intermediate results.

Place 1 in Counter
9
Compare and Branch
  • A program can instruct a computer to compare two
    items and do something based on a match or
    mismatch which, in turn, redirect the sequence of
    programming instructions.
  • There are two forms
  • IF-THEN
  • IF-THEN-ELSE

10
IF-THEN
Test condition p
11
IF-THEN-ELSE
12
Iterate
  • A program loop is a form of iteration. A computer
    can be instructed to repeat instructions under
    certain conditions.

13
Iteration Control Structures
  • Iteration control structures are looping
    mechanisms.
  • Loops repeat an activity until stopped. The
    location of the stopping mechanism determines how
    the loop will work
  • Leading decisions
  • Trailing decisions

14
Leading Decisions
  • If the stop is at the beginning of the iteration,
    then the control is called a leading decision.
  • The command DO WHILE performs the iteration and
    places the stop at the beginning.

15
DO WHILE Loop
16
Trailing Decisions
  • If the stop is at the end of the iteration, the
    control mechanism is called a trailing decision.
  • The command DO UNTIL performs the iteration and
    puts the stop at the end of the loop.

17
DO UNTIL Loop
Loop statement a
Write a Comment
User Comments (0)
About PowerShow.com