Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Overview Go over parts of quiz? Another iteration structure for loop.
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 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
while Repetition Structure
CS161 Introduction to Computer Science
for Repetition Structures
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 5: Loops and Files.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Iteration with While You can say that again.
CS 1430: Programming in C++ No time to cover HiC.
Unary Operators ++ and --
3 Control Statements:.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
CS150 Introduction to Computer Science 1
Computing Fundamentals
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
2.6 The if/else Selection Structure
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true i.e. While there are more items on my shopping list purchase next item and cross it off my list

while loop while (condition) action; while (condition) { statement1; statement2; }

while loop example // print the first 10 integers int number = 1; while (number <= 10) { cout << number << endl; number = number + 1; }

loop control variable (lcv) // print the first 10 integers int number = 1;// loop control variable initialized while (number <= 10) {// loop control variable tested cout << number << endl; number = number + 1;// loop control variable updated }

Other ways to update lcv number += 1; number ++; //post increment operator by itself - same as number = number + 1; in an expression - increment happens after variable is used in the expression. int number = 4; (number ++ > 4) result is false, number = 5 after condition is evaluated.

Other ways to update lcv ++ number; //preincrement operator by itself - same as number = number + 1; in an expression - increment happens before variable is used in the expression. int number = 4; (++ number > 4) result is true, number = 5 before condition is evaluated) number -- // post decrement (number = number -1;) -- number // pre decrement

even/odd while loop int count, times, number; cout << “Enter the number of integers to test: “; cin >> times; count = 0; // Initialize lcv while (count < times) { cout << “Enter an integer: “’ cin >> number; if (number % 2) cout << number << “is an odd number” << endl; else cout << number << “is an even number” << endl; count ++; // Update lcv }

Using while to validate input cout << “Enter a non-zero value for denominator: “; cin >> denominator; while (0 == denominator) { cout << “Denominator must be non-zero!” << endl; cout << “Try again. “ << endl; cout << “Enter a non-zero value for denominator: “; cin >> denominator; } quotient = numerator/denominator;

using boolean variable as lcv int grade, sum = 0, average = 0, count = 0; bool notDone = true; while (notDone) { cout << “Enter next grade (enter -1 if done): “; cin >> grade; if (grade != -1) { sum = sum + grade; count ++; } else { average = sum / count; notDone = false; }

common programming errors Infinite loop. Not providing in the body of the loop an action that eventually causes the loop to terminate Not figuring out the boundary conditions correctly and executing the loop once too often or once less than needed forgetting to initialize the loop control variable

Group exercises 1) Write a program designed to find the first number which is a power of two and is larger than Use a while loop 2) Which integers does the following code print out? int number = 0; while (number < 10) { cout <<number << endl; }

5 quizzes and drop the lowest grade Each of the 4 remaining is 16% of your grade. closed books, closed notes, no calculators or computers. one 8.5 by 11.5 sheet of notes. both sides (precedence chart) review exercises in handouts review practice quizzes on web. only info covered in class will be on quiz. any material you want to go over now? Quiz on Thursday. No makeups!

Homework Questions?