1 cs161 introduction to computer science topic #8

21
1 CS161 CS161 Introduction to Introduction to Computer Science Computer Science Topic #8

Upload: jacob-simon

Post on 18-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS161 Introduction to Computer Science Topic #8

1

CS161 CS161

Introduction to Introduction to Computer ScienceComputer Science

Topic #8

Page 2: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 2

Today in CS161• Review for Midterm

– Variable Definitions

– Conditional Expressions (if, else)

– Loops (while, do while, for)

– Sample Questions

• Switch Statements• Questions?

Page 3: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 3

Review for Midterm• The midterm is a closed book and closed notes

exam• There will be questions asking you to

determine the output of a program, indicate what is wrong with a program, evaluate conditional expressions, and write program fragments.

Page 4: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 4

Understand these concepts:• The difference between designing an algorithm

and implementing C++ code.• The basic data types: int, float, and char. • The difference between dividing integers versus

floating point numbers.• The if/else control structures. • The difference between relational operators,

equality operators, and logical operators. • The while, do-while, for loops

Page 5: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 5

Understand these concepts:• What does a loop allow us to do?• Where do we put loops in our program?• What is a C++ statement?• How do you read information from the keyboard?• How do you write information to the screen?• Why would you use a while loop rather than a do-

while loop?

Page 6: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 6

Sample Questions:• The following is supposed to output all positive

odd numbers less than 10. • It contains some errors.• What are they and how can they be corrected?int x = 1;while (x != 10) {

x += 2;cout << x << endl;

}

Page 7: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 7

Practice Questions....• Write a for loop to output all positive odd

numbers less than 10, starting at 1

int i; //loop control variablefor (i = 1; i < 10; i = i + 2)

cout << i;

Page 8: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 8

Sample Questions:• What is the output of the following program

fragment?for (i = -1; i <= 5; i = i + 1)

cout << 2*i;cout << endl;

• How would you fix the appearance of the output?

cout << 2*i << ‘\n’;or, cout << setw(5) << 2*i;

Page 9: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 9

Sample Questions:• Change the following while loop to a do-

while loop:int i; int i;

cin >> i; cin >> i;while (i < 20) { if (i < 20)

cout << i << ‘ ‘; do {i += 5; cout << i << ‘ ‘;

} i += 5;} while (i < 20);

answer:

Page 10: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 10

Understand these concepts:

• What data type would you use to store– your age– your gpa– your first name’s initial– a test score (A, B, C)

• Write C++ code to display each upper case letter of the alphabet

Page 11: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 11

Understand these concepts:• What will be the output for the following:

if (x >= 0.0)

if (x < 1000.0) {y = 2*x;if (x <= 300)

x = x/10;}

else y = 3*x;else y = x;cout << x << “ and “ << y << endl;

Page 12: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 12

Understand these concepts:• What will be the output for the following:

for (k = 2; k <= 4; k = k + 1) {

for (j = 5; j <= 8; j = j + 1)cout << k+j;

cout << endl;}

Page 13: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 13

Understand these concepts:• What is the escape sequence that is the

same as a carriage return?• Where should we include comments?• Name some examples of whitespace• Write a cout statement to display your name• Which of the following are not legal integers:

-32.0 +256 256 3,24032000• Explain why it is important to prompt

Page 14: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 14

Understand these concepts:• Write a small program to read in two integer

values and then display them in numerical order, regardless of the order in which they are entered:

int main() {

int first, second;cout << “Enter 2 whole numbers: “;cin >> first >> second;if (first <= second)

cout << first << “ “ << second << endl;else

cout << second << “ “ << first << endl;return 0;

}

Page 15: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 15

Switch Statements• Another C++ control statement is called the

switch statement

• It allows you to pick the statements you want to execute from a list of possible statements, instead of just two different alternatives (as is available with an if/else) or a set of nested if/elses!

• It allows for multi-way decisions.

Page 16: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 16

Switch Statementschar grade;cout << "Enter the grade..." << endl;cin >> grade;switch (grade) {

case 'A': cout << "Excellent" << endl; cout << “Keep up the good work!”;

break;case 'B': cout << "Very Good";

break;case 'C': cout << "Passing";

break;case 'D': case 'F': cout << "Too Bad";

break;default :

cout << "No match was found...try again";break;

}

Page 17: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 17

Switch Statements• C++ provides a "default" clause so

that if there isn't a match something is done. If the default is left off...and there is no match...no action takes place at all.

• When a case statement is executed, the value of Grade is checked and then depending on which of the cases it matches -- the statement following the colon for that case will be executed.

Page 18: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 18

Switch Statements• To exit from a switch statement...use

break. • Unlike Pascal, with C++ once you have a

match...• It will fall thru (ignoring any additional

case or default labels that are encountered and continue executing code until a break is encountered.

• If you intend to use “fall thru”, then you should have a comment saying that the drop through is intentional

Page 19: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 19

Switch Statements• The rule of thumb is that you can use these

to switch on integers and characters. • It is not permitted to use the switch with

floating point types or a string of characters. • The type of the expression following a switch

keyword must be the same as the expressions following each case keyword....and no two expressions following the case keywords can be the same.

Page 20: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 20

What Fall Thru means...int count;

cout << "Please enter the number of asterisks:";cin >> count;switch (count) { //these { } are mandatory!

case 1: cout << "*"; //intentional drop thrucase 2: cout << "**";case 3: cout << "***";case 4: cout << "****";default: cout << "!";

}cout << endl;

Page 21: 1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #8 21

The CORRECT version....int count;

cout << "Please enter the number of asterisks:";cin >> count;switch (count) { //these { } are mandatory!

case 1: cout << "*"; break;case 2: cout << "**"; break;case 3: cout << "***"; break;case 4: cout << "****"; break;default: cout << "!"; break;

}cout << endl;