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

## Presentation on theme: "Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true."— 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 1000. 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?

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

Similar presentations