15
Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. Includes Quiz 1 Topics (terminology, variables, constants, basics) – if-then-else Compound statements Relational operators Logical operators Switch/Case Statements Precedence of Operators DOES NOT INCLUDE LOOPS (Lect 06/07) 1

Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

Embed Size (px)

Citation preview

Page 1: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

Announcements

• Exam 1 Tuesday July 14• 60 minutes• 10 % of Total Grade• Covers Everything through Lecture 05.

– Includes Quiz 1 Topics (terminology, variables, constants, basics)– if-then-else– Compound statements– Relational operators– Logical operators– Switch/Case Statements– Precedence of Operators– DOES NOT INCLUDE LOOPS (Lect 06/07)

1

Page 2: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

Iteration

• Iteration (Looping): Performing a Series of Statements Multiple Times until Some Condition Is Met.

• Eliminates the Need for Redundant Coding or Function Calls

• Many Ways to Loop in C++

2

Page 3: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

The while Loop

• Syntax:

while (condition) statement;while (condition)

{

statement; statement; }

3

Page 4: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

while Example

const int MAX = 100;

int counter = 0;

while (counter < MAX)

{

cout << counter << “ “ << endl;

counter++;

}

4

Page 5: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

Example while Loop

int numEmployees,curNum = 0;

cout << “Enter Number of Employees: “;

cin >> numEmployees;

if (numEmployees > 0)

{

while (curNum < numEmployees)

{

cout << “Welcome to CorpLand!” << endl;

curNum++;

}

}

5

Page 6: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

The for Loop• Syntax:for (expression1;condition; expression2) statement;for (expression1;condition; expression2){ statement; statement;}

• Same as:expression1;while (condition){ statement; expression2;}

6

Page 7: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

Example for Loop

int numEmployees,curNum;cout << “Enter Number of Employees: “;cin >> numEmployees;if (numEmployees > 0){ for (curNum = 0; curNum < numEmployees;curNum++)

{ cout << “Welcome to CorpLand!” << endl; }}

7

Page 8: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

Looping for Input

char letterEntered = ‘A’;

while (letterEntered != ‘Z’)

{

cout << “Enter a letter: “;

cin >> letterEntered;

}

8

Page 9: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

Looping until Flagbool noMoreData();bool done = false;…while (!done){ … // do a bunch of stuff … if (noMoreData() == true) done = true;}cout << “We’re all through – thank you!”;

9

Page 10: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

Nested Loops

int i , j ;for (i=0; i < 10; i++){ for (j=0; j < 10; j++) { cout << i << j << “ “; } cout << endl; }

10

Page 11: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

The do-while Loop

• Syntaxdo statement;while (condition);do{ statement; statement;}while (condition);

11

Page 12: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

do-while Example

int inputNum = 0;

do

{

cout << “Please Input Number, –1 to quit: “;

cin >> inputNum;

}

while (inputNum != -1);

12

Page 13: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

What Is This?while (stringEntered != “apple”){ cout << “Enter a red fruit: “; cin >> stringEntered;}while (stringEntered != “banana”){ cout << “Enter a yellow fruit: “; cin >> stringEntered;}while (stringEntered != “pomegranate”){ cout << “Enter a random fruit: “; cin >> stringEntered;}

13

Page 14: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

What Is This?for (i = 0; i < 10 ; i++) cout << “Interesting, isn’t it?” << endl;

while (answer != ‘D’){ cout << “Enter an answer: “; cin >> answer;}while (answer != ‘D’){ cout << “Enter an answer: “; cin >> answer;}

14

Page 15: Announcements Exam 1 Tuesday July 14 60 minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,

What Is This?void

kingsChair(int loopCounter)

{

int num;

for(num = 0; num < loopCounter; num++)

cout << “AAAAAAAAAAAAAAAAAAAAAAAAAA” << endl;

}

15