Sie sind auf Seite 1von 16

1

STRUCTURED
PROGRAMMING
Control Structures II (Switch)
First Semester / 2014-2015
Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
2

ORDER OF PRECEDENCE
• Relational and logical operators are evaluated from left to right.
• Parentheses can override precedence.
• ()
• ++Pre-increment, --pre-decrement, ! not (unary operators)
• *,/,%
• +,-
• <,<=,>=,>
• &&
• ||
• ==,!=
• Compound operators
• = assignment
• Post-increment++, post –decrement--

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
3

SHORT-CIRCUIT EVALUATION

• Short-circuit evaluation: evaluation of a logical


expression stops as soon as the value of the
expression is known
• Example:
(age >= 21) || ( x == 5) //Line 1
(grade == 'A') && (x >= 7)//Line 2

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
4

EXAMPLE

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
5

EXAMPLE (CONT’D.)

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
6

EXAMPLE (CONT’D.)

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
7

MULTIPLE SELECTIONS: NESTED IF

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
8

CONDITIONAL OPERATOR (?:)


• Conditional operator (?:)
• Ternary operator: takes 3 arguments
• Syntax for the conditional operator:
expression1 ? expression2 : expression3
• If expression1 is true, the result of the
conditional expression is expression2
• Otherwise, the result is expression3
• Example: max = (a >= b) ? a : b;
Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
9

SWITCH STRUCTURES

• Switch structure: alternate


to if-else

• Value of the expression determines


which corresponding action is taken

• Expression is sometimes
called the selector
Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
10

SWITCH STRUCTURES (CONT’D.)

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
11

SWITCH STRUCTURES (CONT’D.)


• One or more statements may follow a case label.
• Braces are not needed to turn multiple statements
into a single compound statement.
• When a case value is matched, all statements after
it execute until a break is encountered.
• The break statement may or may not appear after
each statement
• switch, case, break, and default are
reserved words.
Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
12

SWITCH STRUCTURES (CONT’D.)

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
13

SWITCH STRUCTURES (CONT’D.)


switch (score / 10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
grade = 'F';
break;
case 6:
grade = 'D';
break;
case 7:
grade = 'C';
break;
case 8:
grade = 'B';
break;
case 9:
case 10:
grade = 'A';
break;
default:
cout << "Invalid test score." << endl;
}
Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
14

TERMINATING A PROGRAM WITH


ASSERT FUNCTION
• Certain types of errors are very difficult to catch.
Example: division by zero.
• assert function: useful in stopping program
execution when certain elusive errors occur.
• Syntax:

• expression is any logical expression


• If expression evaluates to true, the next statement
executes. Otherwise, the program terminates and
indicates where in the program the error occurred.

Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
15

THE ASSERT FUNCTION (CONT’D.)


• To use assert, include cassert header file
• assert is useful for enforcing programming
constraints during program development
• After developing and testing a program, remove or
disable assert statements
• The preprocessor directive #define NDEBUG
must be placed before the directive #include
<cassert> to disable the assert statement
Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh
16

EXAMPLE
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
int x, y;
cin >> x >> y;
assert(y!=0);
cout << x/y << endl;
return 0;
}
Copyright © 2014 Applied Science University33. All Rights Reserved | Dr. Mahmood G. Al-Bashayreh

Das könnte Ihnen auch gefallen