+ All Categories
Home > Documents > 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else...

1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else...

Date post: 19-Dec-2015
Category:
View: 218 times
Download: 1 times
Share this document with a friend
33
1 CIS 230 25-Jan-06
Transcript
Page 1: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

1

CIS 230

25-Jan-06

Page 2: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

2

Overview

• Selection Statements– If Statement– Else– Nested If-Else– Switch

• Repetition Statements– While statement– For Statement– Infinite loops– Do-While Statement– Break– Continue

• Formatted Output

Page 3: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

3

Selection Statements

• if-else– Decision making statement

• switch– Allows choice: one out of several options

Page 4: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

4

If Statement

if ( test )

statement;

if ( test )

{

statements;

}

if true (i.e. != 0) do statementif false (i.e. == 0) skip it

Page 5: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

5

If Statement

if ( x == 7 ) cout << “you win\n”;

if ( value != 0){ statements;}SAME AS:if (value){ statements;}

Remember to use == to compare.Using = will assign the value, and will ALWAYS be true.

Page 6: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

6

Else

if ( test )

true statement;

else

false statement;

if ( test )

{

true statements;

}

else

{

false statements;

}

Page 7: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

7

Else

cout << “Please enter a positive integer”;cin >> value;if (value > 0) cout << “Thank you!\n”;else{ cout << “The value entered was not positive\n”; cout << “I will use the value 10 \n”; value = 10;}

Page 8: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

8

Nested If-Else

if (x > 0)

cout << "x is positive";

else

if (x < 0)

cout << "x is negative";

else cout << "x is 0";

Page 9: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

9

Nested If-Else

cin >> command;if ( command == ‘u’ ) cout << “Move up” << endl;else if ( command == ‘d’ ) cout << “Move down” << endl;else if ( command == ‘l’ ) cout << “Move left” << endl;else if ( command == ‘r’ ) cout << “Move right” << endl;else cout << “Invalid command received << endl;

Page 10: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

10

Nested If-Elsecin >> command;if ( command == ‘u’ ) cout << “Move up” << endl;else if ( command == ‘d’ ) cout << “Move down” << endl; else if ( command == ‘l’ ) cout << “Move left” << endl; else if ( command == ‘r’ ) cout << “Move right” << endl; else cout << “Invalid command received << endl;

Page 11: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

11

Switch

switch (value){ case constant1: statement1; statement; break; case constant2: statement2; break; default: statement; break;}

• Cases may be in any order• “case” is terminated with a :

(colon)• “constants” can be integers or

characters• Cases may have several

statements• break jumps out of the

construct• default is optional

Page 12: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

12

Switchcin >> choice;switch (choice){ case 0: cout << “Just a beginner huh?\n”; moves = 15; break; case 1: case 2: case 3: cout << “OK, let’s begin \n”; moves = 10; break; case 4: cout << “You only have 7 moves \n”; moves = 7; break; default: cout << “Incorrect selection \n”; moves = 0; break;}

Only way to have several choices do the same thing.

Page 13: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

13

If-else-if vs Switchcin >> command;if ( command == ‘u’ ) cout << “Move up” << endl;else if ( command == ‘d’ ) cout << “Move down” << endl; else if ( command == ‘l’ ) cout << “Move left” << endl; else if ( command == ‘r’ ) cout << “Move right” << endl; else cout << “Invalid command received <<

endl;

cin >> command;switch ( command ){ case ‘u’: cout << Move up” << endl; break; case ‘d’: cout << “Move down” << endl; break; case ‘l’: cout << “Move left” << endl; break; case ‘r’: cout << “Move right” << endl; break; default: cout << “Invalid command received” << endl; break;}

Page 14: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

14

Repetition Statements

• while loops– pretest loop

• for loops– pretest loop

• do-while loops– posttest loop

• pretest loop: – Test at the beginning

of the loop– Condition must be set

prior to loop entry.

• posttest loop:– Test at the end of the

loop– Always executes at

least once.

Page 15: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

15

While statement

while ( expression )

statement;

while ( expression )

{

statements;

}Evaluates the expression

if true (i.e. != 0) executes statement(s)

Whiletest

Body

true false

Page 16: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

16

While statement

while ( x < 0 )

x = x – y + z;

while ( a <= b)

{

a = a + 1;

b = b / 2;

total = total + b;

}

Page 17: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

17

For Statement

for ( initialize; expression; alter )

statement;

for ( initialize; expression; alter )

{

statement(s);

}

Initialize

fortest

Body

Alter

false

true

Page 18: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

18

For Statement

int i;

for ( I = 0; I <= 10; i++)

{

cout << i << endl;

}

int answer, a, c, b;

a = 0;

b = 100;

answer = 0;

for (c = a; c <= b; c++)

answer = answer + c;

Page 19: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

19

While vs. For

int i = 0;

while ( i <= 10)

{

cout << i << endl;

i++;

}

for (int i = 0; i <= 10; i++)

cout << i << endl;

Page 20: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

20

Beware the infinite loop!

for (c = a; c <= b; 1) answer = answer + c;

for ( c = a; 10; c++) answer = answer + c;

while (i < 10){ cout << i << endl;}

c does not change, infinite loop

Always true, infinite loop

i is never incremented; infinite loop

Page 21: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

21

Do-While Statement

do

{

body;

}

while ( expression )

executes statement(s)evaluates the expression

if true (i.e. != 0)

do

body

whiletest

true

false

Page 22: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

22

Do-While statement

do

{

cout << “Enter a number between 1 & 10 \n”;

cin >> value;

}

while ( value < 1 || value > 10 )

Page 23: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

23

Break

• break– Can also be used to exit (jump out) of other loops as

wellfor (i = 1; a <= b; i++){ a += i; //a = a + i b -= i; //b = b – i if (b <=0) break; c += a + b //c = c + (a + b)}

Can end in two ways

Page 24: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

24

Continue

• continue– Goes to the test of a loop (Does the test again)– Must be a loop not a switch

while ( a < b){ cin >> a; if (a < 0 || a > 9) continue; //process rest of loop}

Page 25: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

25

Formatted Output

• #include <iomanip>

• setw( )

• fixed

• setprecision( )

• setiosflags( )

Page 26: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

26

Formatted Output

cout << 6 << endl

<< 18 << endl

<< 124 << endl

<< "---\n"

<< (6+18+124) << endl;

Results:

6

18

124

---

148

Page 27: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

27

setw()

• Used within cout

• Sets field width for next output

• Right justifies

Page 28: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

28

setw()

//Added setw(3) to each line

cout << setw(3) << 6 << endl

<< setw(3) << 18 << endl

<< setw(3) << 124 << endl

<< "---\n"

<< (6+18+124) << endl;

Results:

6

18

124

---

148

Page 29: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

29

setw

int a = 24;

int b = 573;

cout << '|' << setw(4) << a

<< '|' << setw(6) << b

<< '|' << setw(2) << b

<< '|' << endl;

Results:

| 24| 573|573|

Page 30: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

30

fixed

• Used with cout

• Forces the display of a decimal point

Page 31: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

31

setprecision()

• Used with cout

• Sets maximum number of decimal places– Will show less if fewer decimal values

• Floating point numbers only

Page 32: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

32

setprecision()

float x = 19.1738;

float y = 6.4;

cout << '|' << setw(7) << setprecision(2)

<< y << '|' << endl;

cout << '|' << setw(7) << fixed

<< setprecision(2) << x << '|'

<< endl;

cout << '|' << setw(7) << fixed

<< setprecision(2) << y << '|'

<< endl;

Results:

| 6.4|

| 19.17|

| 6.40|

Page 33: 1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

33

setiosflags()

• ios::fixed

• ios::scientific

• ios::showpoint

• ios::showpos

• ios::left

• ios::right


Recommended