Conditinoal Constructs Review

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5: Control Structures II (Repetition)
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Review 1.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Unit 3 Lesson 9 Repetition Statements (Loops)
Branching Constructs Review
Control Structures II (Repetition)
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 8 Repetition Statements
For & do/while Loops.
COMS 261 Computer Science I
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Expression Review what is the result and type of these expressions?
Alternate Version of STARTING OUT WITH C++ 4th Edition
Additional Control Structures
Conditinoal Constructs Review
Repetition Control Structure
Chapter 6: Repetition Statements
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
2.6 The if/else Selection Structure
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4 Repetition Structures
Presentation transcript:

Conditinoal Constructs Review what is a block? what is special about declaring a variable inside a block? what is a scope of a variable? what are conditional constructs? what type of conditional constructs have we studied? what is nested if? what is multiway-if? How does multiway-if relate to nested if? what is a switch statement? is it better than multiway-if? what does break inside switch do? what is conditional operator? conditional assignment? what construct can be used instead? what is programming idiom? what is a unary, binary, ternary operator?

Iterative Constructs while, for, do-while

Iterative Constructs provide ability to execute the same code multiple times three constructs while statement do-while statement for statement

The while Statement expression body true false syntax while (expression) body semantics if expression is true then execute body body is either a single statement or a block iteration: single execution of body iterate until expression evaluates to false example while (n != 0) { cin >> n; if (n > max) max = n; } expression body true false

The do-while Statement while (n != 0 ) { cin >> n; if (n > max) max = n; } The do-while Statement syntax do body while (expression); semantics execute body if expression is true then iterate again iterate until expression evaluates to false example int max=0, n; do { cin >> n; if (n > max) max = n; } while (n == 0); body expression true false

The for Statement initStatement false expression true body postStatement syntax for(initStatement; expression; postStatement) body semantics execute initStatement evaluate expression, if true: iterate iteration: execute body execute postStatement repeat expression evaluation example for (int i = 0; i < 20; ++i) cout << "i is " << i << endl; loop variable - declared inside for its scope is body of the loop modifying loop variable inside body is poor style, use while instead

Iterate and Keep Track Idiom what is idiom again? often need to iterate while keep track of some value across iterations – maximum value found, sum, if all positive, etc. idiom before loop, declare tracking variable to keep track, initialize it what is initialization again? inside loop, update tracking variable, use branching if necessary to examine after loop, use the tracking variable that accumulated the result example: cout << "Input number [0 to quit]: "; int max, n; cin >> n; max = n; while (n != 0 ) { cin >> n; if ( n > max) max = n; } cout << ”Maximum number: ” << max << endl;

Break and Continue with Iterative Constructs break - exits innermost loop int sum=0; while(sum < 100) { int i; cin >> i; if (i< 0) { cout << ”found negative number\n”; break; } sum +=i; avoid break with loops as they make code less readable (makes regular loop exit unnecessary): first try to code loop without it continue - skip the remaining statements and start a new iteration (evaluate expression) for (int i = 0; i < 20; ++i) { int intVar; cin >> intVar; if(intVar < 0) continue;

Nesting of Iterative Constructs iterative constructs can be nested: one iterative construct may be inside the body of another example: for (int i = 0; i < 10; ++i) // outer loop for (int j = 0; j < 10; ++j) // inner loop cout << i << j << endl; what would this code output? note, no need for curly brackets nesting may be more than two loops deep for/while/do-while can be mixed in nesting besides nested loops, loop body may contain other code including branching constructs: a branching construct nested in the loop

Iteration Key Points make sure there is a statement that will eventually falsify the looping construct expression (i.e., the loop must stop) make sure that all counters and tracking variables are initialized have a clear purpose for the loop good way to know if you do: can write clear comments above loop