111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet

Preview:

Citation preview

04/21/23 CS150 Introduction to Computer Science 1

1

Announcements

I have not graded your exam yet

04/21/23 CS150 Introduction to Computer Science 1

2

Multiple Alternative Ifs

if (condition1)

statement1;

else if (condition2)

statement2;

else

defaultstatement;

04/21/23 CS150 Introduction to Computer Science 1

3

Program

Write a program that displays a letter grade corresponding to an exam score90 - 100 A

80 - 89 B

70 - 79 C

60 - 69 D

0-59 F

04/21/23 CS150 Introduction to Computer Science 1

4

Example

if (salary < 0.00)

tax = -1;

else if (salary < 15000.00)

tax = 0.15 * salary;

else if (salary < 30000.00)

tax = (salary - 15000.00)*0.16 + 2250.00;

else

tax = salary * 0.26;

04/21/23 CS150 Introduction to Computer Science 1

5

What’s the difference?

if (x >= 0)

x = x + 1;

else if (x >= 1)

x = x + 2;

if (x >= 0)

x = x + 1;

if (x >= 1)

x = x + 2;

04/21/23 CS150 Introduction to Computer Science 1

6

Selection Structure Review

Write a program to solve the following problem:A police department needs to calculate fees for speeding tickets.

The fees are as follows:Speed Fee

75 or greater $60.00

51 - 74 $40.00

36 - 50 $20.00

04/21/23 CS150 Introduction to Computer Science 1

7

Switch Statements

Another form of selection statement

Similar to if’s

Useful for lots of alternatives

04/21/23 CS150 Introduction to Computer Science 1

8

Example

switch (watts){

case 25: life = 2500;

break;case 40:case 60:

life = 1000;break;

case 75:case 100:

life = 750;break;

default:life = 0;

}

04/21/23 CS150 Introduction to Computer Science 1

9

Form

switch (selector){case label1: statements1;

break;case label2: statements2;

break;…case labeln: statementsn;

break;default: statements;

}

04/21/23 CS150 Introduction to Computer Science 1

10

Example

switch (musical_note)

{

case ‘c’:

cout << “do” << endl;

break;

case ‘d’:

cout << “re” << endl;

break;

case ‘e’:

cout << “mi” << endl;

break;

case ‘f’:

cout << “fa” << endl;

break;

case ‘g’:

cout << “sol” << endl;

break;

case ‘a’:

cout << “la” << endl;

break;

case ‘b’:

cout << “ti” << endl;

break;

default:

cout << “An invalid note was read.”;

}

04/21/23 CS150 Introduction to Computer Science 1

11

Important!

Selector must be ordinal type

Each possible value is a separate case

break stops statements for case, otherwise continue with statements for next case

04/21/23 CS150 Introduction to Computer Science 1

12

Example

switch (color){case ‘R’: case ‘r’:

cout << “red” << endl;case ‘B’: case ‘b’:

cout << “blue” << endl; case ‘Y’: case ‘y’:

cout << “yellow” << endl;}

What happens when color is ‘r’? ‘B’? ‘Y’?

04/21/23 CS150 Introduction to Computer Science 1

13

Example

switch (x > y){case 1:

cout << “x greater” << endl;break;

case 0:cout << “y greater or equal” << endl;break;

}

Write as if statement

04/21/23 CS150 Introduction to Computer Science 1

14

Questions

Can you write any switch statement as an if?

Can you write any if statement as a switch?

04/21/23 CS150 Introduction to Computer Science 1

15

Problem

Write a switch statement to convert a character digit to an integer

04/21/23 CS150 Introduction to Computer Science 1

16

Change to switch

if (speed > 35)

fee = 20.00;

else if (speed > 50)

fee = 40.00;

else if (speed > 75)

fee = 60.00;

04/21/23 CS150 Introduction to Computer Science 1

17

Examples

Write an if statement that prints out the level of schooling. (0, none; 1 through 6, elementary; 7 through 8, middle school; 9 through 12, high school; > 12, college)

Write a switch statement to do the same

04/21/23 CS150 Introduction to Computer Science 1

18

Write a Program

Input an integer

If the integer is positive, increment a variable poscount by 1.

If the integer is negative, increment a variable negcount by 1.

If neither, increment zerocount by 1.

04/21/23 CS150 Introduction to Computer Science 1

19

Unary Operators ++ and --

++ is increment operatorx++;

is the same as x = x + 1;

-- is decrement operatorx--;

is the same as x = x - 1;

04/21/23 CS150 Introduction to Computer Science 1

20

Prefix and PostfixUnary ++ and --Prefix Postfix

k = --x; k =x--;

k = ++x; k = x++;

Increment/ Assign value of x to

decrement x k, then increment

then assign or decrement x

value of x to k

04/21/23 CS150 Introduction to Computer Science 1

21

Example

cout << “Value of i is” << i;

cout << “Value of i++ is” << i++;

cout << “Value of ++i is” << ++i;

cout << “Value of --i is” << --i;

cout << “Value of i-- is” << i--;

04/21/23 CS150 Introduction to Computer Science 1

22

Program

Write a program that outputs the following:

*****

*****

*****

*****

*****

04/21/23 CS150 Introduction to Computer Science 1

23

count = 0;

while (count < 5)

{

cout << “*****” << endl;

count++;

}

How many times (iterations) does loop run?

Loops

Loop Control Variable

Initialize LCV

Change the value of count

04/21/23 CS150 Introduction to Computer Science 1

24

While loopswhile (logical expression is true)

statement;

while (logical expression is true)

{

statement1;

statement2;

}

04/21/23 CS150 Introduction to Computer Science 1

25

Key ingredients Initialize

MUST initialize loop control variable

TestThe value of loop control variable is tested during each

iteration of loop

UpdateLoop control variable is changed during each loop iteration

If any one of these is missing or incorrect, your loop won’t run properly--not at all, too many/few times or infinitely.

04/21/23 CS150 Introduction to Computer Science 1

26

Examples

Write a while loop that outputs each integer from 1 to 5 Write a program that inputs the following data for 5

students: name, id# and grade Write a program that inputs the following data for a user

specified number of students: name, id# and grade

Recommended