{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture7 Control Structures

lecture7 Control Structures - CMPT 128 Introduction to...

Info icon This 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
Image of page 1

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

View Full Document Right 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)
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
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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
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!
Image of page 9

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

View Full Document Right Arrow Icon
© Janice Regan, CMPT 128, Jan 2007 10 Example of one-way selection Example if statement in C++: if (date == today && time > now) cout << “Printing appointment details“ << time << appointment;
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern