1 / 21

# Selection Structures Continued… - PowerPoint PPT Presentation

Selection Structures Continued…. Common Mistakes with Conditions (1). Consider the following code: int age(26); if (age = 18) // Line 1 { cout << “You are 18.” } else { cout << “You are not 18.”; } The output will output “You are 18.” Why?.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Selection Structures Continued… ' - dermot

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Selection Structures Continued…

The Ohio State University

• Consider the following code:

int age(26);

if (age = 18) // Line 1

{

cout << “You are 18.”

}

else

{

cout << “You are not 18.”;

}

• The output will output “You are 18.” Why?

The Ohio State University

• On Line 1: The equality operator is ==, and not =

• = is the assignment operator, so what we are actually doing on line 1 is assigning age to the value of 18.

• So the condition becomes:

if (18)

{

cout << “You are 18.”

}

• Now remember the Boolean data type. 0 evaluates to false, and any value other than 0 evaluates to true. Since 18 is not 0, this condition will be satisfied!!

The Ohio State University

else-if Statements (1)

• The if-then-else statements that we have seen thus far allows us to select from at most two alternatives.

• The code to execute if the condition succeeds

• The code to execute if the condition fails

• What if there are many alternatives to consider?

The Ohio State University

else-if Statements (2)

• Consider this example:

• We have a variable, age, that contains a person’s age.

• If 1 ≤ age ≤ 12, print “You are very young”

• If 13 ≤ age ≤ 19, print “You are a teenager”

• If 20 ≤ age ≤ 39, print “You are getting old”

• If 40 ≤ age, print “You are over the hill”

The Ohio State University

if (1 <= age && age <= 12)// Line 1

{

cout << “You are a child” << endl;

}

if (13 <= age && age <= 19) // Line 2

{

cout << “You are a teenager” << endl;

}

if (age <= 20 && age <= 39) // Line 3

{

cout << “You are getting old” << endl;

}

if (40 <= age) // Line 4

{

cout << “You are over the hill” << endl;

}

The Ohio State University

• This works, but why is it undesirable?

• If the condition at line 1 succeeds, do we need to continue checking the conditions at Lines 2, 3, and 4? Not in this case.

• So this is a waste of time that the program could be using doing something more constructive! Here’s a better solution:

The Ohio State University

else-if Statements

if (1 <= age && age <= 12) // Line 1

{

cout << “You are a child” << endl;

}

else if (13 <= age && age <= 19) // Line 2

{

cout << “You are a teenager” << endl;

}

else if (20 <= age && age <= 39) // Line 3

{

cout << “You are getting old” << endl;

}

else if (40 <= age) // Line 4

{

cout << “You are over the hill” << endl;

}

The Ohio State University

The Ohio State University

• Like else statements, else-if statements are optional, but ALWAYS sandwiched in between the if- and the else statement.

• There is no limit to the number of else-if statements succeeding an if-statement.

• It is important to realize the difference between a chain of else-ifs and a chain of ifs.

The Ohio State University

else-if failure! Error

if (1 <= age && age <= 12) // Line 1

{

cout << “You are a child” << endl;

}

else if (13 <= age && age <= 29) // Line 2 ERROR

{

cout << “You are a teenager” << endl;

}

else if (20 <= age && age <= 39) // Line 3

{

cout << “You are getting old” << endl;

}

else if (40 <= age) // Line 4

{

cout << “You are over the hill” << endl;

}

The Ohio State University

Another Example of failure!else-if

if (a > 0)

{

//do something if condition succeeds

}

else if (b < 3)

{

//here, we know a ≤ 0 because the first condition //failed, now we also test for b < 3 do something //if condition succeeds

}

else

{

//here, we know neither conditions were met

//do something else, if necessary

}

The Ohio State University

ifExample.cpp failure!

• What is the output on input:

• 1 2 3

• 3 2 1

• 1 3 2

• 2 1 3

• 3 1 2

• 2 3 1

...

int main()

{

int a(0), b(0), c(0);

cout << "Enter a, b, c: ";

cin >> a >> b >> c;

if (a < b)

{

if (b < c) { cout << "b < c" << endl; }

else { cout << "b >= c" << endl; }

}

else if (a < c)

{ cout << "a < c" << endl; }

else

{ cout << "a >= c" << endl; }

return 0;

}

The Ohio State University

Exercise failure!

• Write if-else-if statements which print:

• “You are too young to drive.” if age  14;

• “You can get a learners permit.” if age = 15;

• “You pay more for insurance.” if 16  age  25;

• “You can drive.” if age > 25;

The Ohio State University

The failure!switch Statement

• An alternative to the if-else chain, but not as general.

switch (expression)

{

case value1:

...

case value2:

...

...

}

(See text.)

The Ohio State University

### cerr and exit() failure!

The Ohio State University

Error Handling failure!

• if-then statements are often used to detect and handle errors.

• Use cerr() instead of cout() for error output messages.

The Ohio State University

cerrExample.cpp failure!

#include <cstdlib> // File cstdlib contains exit()

...

int main()

{

double x(0.0);

cout << "Enter non-negative value: ";

cin >> x;

if (x < 0)

{ // Use cerr instead of cout. Use exit instead of return.

cerr << "Error: Illegal negative value: " << x << endl;

exit(20);

}

cout << "sqrt(" << x << ") = " << sqrt(x) << endl;

return 0;

}

The Ohio State University

exit() failure!

• To use exit(), we need:

#include <cstdlib>

• To help in debugging, use a different number with each exit statement:

exit(10);

exit(20);

exit(30);

The Ohio State University

cerrExample2.cpp failure!

...

int main()

{

double x(0.0);

cout << "Enter non-negative value: ";

cin >> x;

if (x < 0)

{ // Use cerr instead of cout.

cerr << "Warning: Illegal negative value: " << x << endl;

cerr << "Changing " << x << " to " << -x << endl;

x = -x;

}

cout << "sqrt(" << x << ") = " << sqrt(x) << endl;

return 0;

}

The Ohio State University

Error Handling failure!

• cerr() instead of cout(): cerr() messages can be sent to a different place than cout(). cerr() also forces messages to be printed immediately.

• exit() instead of return: exit() quits the program and returns control to the operating system. exit() frees up resources associated with the program. “return” returns control to any calling program/function.

The Ohio State University