lecture7 Control Structures

lecture7 Control Structures - CMPT 128: Introduction to...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 © Janice Regan, CMPT 128, Jan 2007 CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Selection
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, Jan 2007 2 Control Structures Three methods of processing a program In sequence Branching Looping Branch: Altering the flow of program execution by making a selection or choice Loop: Altering the flow of program execution by repetition of a particular block of statement(s)
Background image of page 2
© Janice Regan, CMPT 128, Jan 2007 3 Selection Based on Branching One-Way Selection One alternative: “Things to do” if a condition is true C implementation: if statement Two-Way Selection Two alternatives: “Things to do” if the condition is true, a different “things to do” if the condition is false C implementation: if-else statement Compound Statements Allows a group of statements to be considered as a block That block can replace a single statement in any selection structure
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, Jan 2007 4 Selection Based on Branching Multiple Selections A series of different “things to do” if a series of conditions hold C implementation: if-elseif-else structure Alternate C implementation: Nested if statements Switch Structures A series of different “things to do” depending on the value of a particular variable
Background image of page 4
© Janice Regan, CMPT 128, Jan 2007 5 Decisions Use a decision statement when an action is to be taken only if a particular condition holds The condition which must hold may be logical or relational expression or a Boolean variable. The value of the condition must be true or false Each possible path through a condition statement will contain a sequence of steps to be executed The condition and the sequences of steps that are executed for each outcome of the condition statement form a selection structure . A selection structure is a type of control structure
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, Jan 2007 6 Flowcharts Flowcharts use some basic symbols To start or end a function To contain calculations To make decisions To connect different parts of an algorithm
Background image of page 6
© Janice Regan, CMPT 128, Jan 2007 7 One-way selection A series of actions is taken only if a particular condition is true. The condition is a logical statement with a value of true or false If the condition is true the actions are executed If the condition is false the actions are not executed Implemented in C++ as an if statement
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, Jan 2007 8 Flowchart: one way selection A simple decision uses a decision box to hold the condition (Boolean value: relational or logical expression) The sequence of statements is held in a sequence box or boxes condition Statement 1; Statement n; T F
Background image of page 8
© Janice Regan, CMPT 128, Jan 2007 9 if Statement Syntax Selection structure for C++ Formal syntax: if (<boolean_expression>) <yes_statement> Note yes_statement is only ONE C++ statement!
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, Jan 2007 10 Example of one-way selection
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/18/2010 for the course CMPT 128 taught by Professor Regan during the Spring '08 term at Simon Fraser.

Page1 / 39

lecture7 Control Structures - CMPT 128: Introduction to...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online