CS149D Elements of Computer Science

Slides:



Advertisements
Similar presentations
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
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 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Lecture 18: 10/31/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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,
Chapter 05 (Part III) Control Statements: Part II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Lecture 13: 10/10/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
COMP Loop Statements Yi Hong May 21, 2015.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
Chapter 6. Loops A control structure that causes a statement or group of statements to be executed repeatedly There are 3 types of loops –while –for –do.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
UCT Department of Computer Science Computer Science 1015F Iteration
C++ Iterative Constructs
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Branching Constructs Review
Chapter 2.2 Control Structures (Iteration)
CS149D Elements of Computer Science
Lecture 4B More Repetition Richard Gesick
Conditinoal Constructs Review
CS149D Elements of Computer Science
For & do/while Loops.
Additional Control Structures
Conditional Construct
Conditinoal Constructs Review
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 2
CS149D Elements of Computer Science
CS149D Elements of Computer Science
CS148 Introduction to Programming II
CS149D Elements of Computer Science
CS148 Introduction to Programming II
Presentation transcript:

CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 20: 11/7/2002 Lecture 20: 11/7/2002 CS149D Fall 2002

Outline Looping do-while loop for loop Break and continue statements within loops Nested Loops Lecture 20: 11/7/2002 CS149D Fall 2002

The do-while Loop1/2 Condition True ? Execute while Statement(s) Continue to next statement No Yes do statement; while (condition); { statement(s); }while (condition); Lecture 20: 11/7/2002 CS149D Fall 2002

The do-while Loop2/2 Print 0, 4,16,36,64,100 (0, 22,42,62,82,102) // while loop solution int x = 0; while (x <= 10) { cout << x*x <<endl; x +=2; } //do-while loop solution int x = 0; do { cout << x*x <<endl; x +=2; } while (x <= 10); Lecture 20: 11/7/2002 CS149D Fall 2002

The for Loop1/2 Execute init statement for (x = 0; x <=10 ; x+=2) Exit Condition Execute for_statement(s) Execute x +=2 Execute next statement True False for (x = 0; x <=10 ; x+=2) cout << x*x <<endl; x = 0  init statement x <=10  Exit condition x +=2  executed before starting a new iteration Lecture 20: 11/7/2002 CS149D Fall 2002

The for Loop2/2 Print 0, 4,16,36,64,100 (0, 22,42,62,82,102) // while loop solution int x = 0; while (x <= 10) { cout << x*x <<endl; x +=2; } //do-while loop solution int x = 0; do { cout << x*x <<endl; x +=2; } while (x <= 10); // for loop solution int x; for (x = 0; x <=10 ; x+=2) cout << x*x <<endl; Lecture 20: 11/7/2002 CS149D Fall 2002

Variations of the for Loop Init statement, Exit Condition, and expression 2 can all be null //infinite Loop for ( ; ; ) cout << “Hi”; //Equivalent to while (true) for ( ; inputVal != -1; ) cin >> inputVal; //Equivalent to while (inputVal != -1) Lecture 20: 11/7/2002 CS149D Fall 2002

The break and continue statements1/2 The break statement Exit immediately from its enclosing loop The continue statement Skip any remaining statements in the current iteration, and start a new iteration of the enclosing loop In while or do-while loop, the condition is evaluated to determine whether or not to start a new iteration In a for loop, the second expression is executed, then condition is evaluated Lecture 20: 11/7/2002 CS149D Fall 2002

The break and continue statements2/2 // break example sum = 0; for (int k=1;k<21;k++) { cin >>x; if (x > 10) break; sum += x; } cout << sum; // continue example sum = 0; for (int k=1;k<21;k++) { cin >>x; if (x > 10) continue; sum += x; } cout << sum; Lecture 20: 11/7/2002 CS149D Fall 2002

Nested Loops for (i = 1; i <=5; i ++) { for (j = 1; j <=i; j++) Print the following triangle 1 12 123 1234 12345 for (i = 1; i <=5; i ++) { for (j = 1; j <=i; j++) cout << j; cout <<endl; } Lecture 20: 11/7/2002 CS149D Fall 2002