control structures (selection)

23
CONTROL STRUCTURES (SELECTION)

Upload: lamont

Post on 21-Jan-2016

41 views

Category:

Documents


1 download

DESCRIPTION

CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS. SEQUENCE Groups Of Sequential Steps SELECTION Making Choices IF-THEN (one way) IF-THEN-ELSE (two way or multi way) SWITCH (multi way) ITERATION(Looping) Repeating Steps WHILE – DO (top tested) DO – WHILE (bottom tested) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CONTROL STRUCTURES (SELECTION)

CONTROL STRUCTURES(SELECTION)

Page 2: CONTROL STRUCTURES (SELECTION)

PROGRAM COMPONENTS

SEQUENCE Groups Of Sequential Steps

SELECTION Making Choices

IF-THEN (one way) IF-THEN-ELSE (two way or multi way)SWITCH (multi way)

ITERATION(Looping) Repeating Steps

WHILE – DO (top tested)DO – WHILE (bottom tested)FOR (fixed iteration)

Page 3: CONTROL STRUCTURES (SELECTION)

BOOLEAN DATA TYPE

Two Values – true (1) or false (0) Example:

bool isOvertime = true;

cout << “Is overtime “ << isOvertime << endl;

isOvertime = false;

cout << “Is overtime “ << isOvertime << endl;

Internal program value Cannot be read in via keyboard entry Only displayed as 1 or 0

Page 4: CONTROL STRUCTURES (SELECTION)

BOOLEAN DATA TYPE

Boolean names should reflect true valueFor example:

bool done = false;

bool even = false;

bool error = true;

Can be initialized with integer: Using 0 intializes to false Anything Else initializes to true

It is best to use values true or false

Page 5: CONTROL STRUCTURES (SELECTION)

RELATIONAL / EQUALITY OPERATORS

Boolean expression is:A condition in which a relational operator tests therelationship between two values or expressions andreturns a boolean result (true or false)

Relational / Equality operators:Allow comparison of the size, magnitude or equality of data items that are the same or compatible types.

For example: less than ( < )…. 3 < 5 evaluates to true bool isLessThan = 3 < 5;

Page 6: CONTROL STRUCTURES (SELECTION)

RELATIONAL / EQUALITY OPERATORS

Operator Meaning True Example False Example

< Less Than 3 < 5 4 < 2 or 6 < 6

<= Less Than OR Equal To 3 <= 4 or 3 <= 3 10 <= 5

> Greater Than 8 > 7 9 > 10 or 4 > 4

>= Greater Than OR Equal To

5 >= 4 or 5 >= 5 5 > = 6

== Equal To 20 == 20 10 == 11

!= Not Equal To 10 != 20 20 != 20

Evaluation of either a relational or equality operator always results to either true or false.

Page 7: CONTROL STRUCTURES (SELECTION)

RELATIONAL / EQUALITY OPERATORS

Relational Operator Priority(<, <=, >, >=) Are below the arithmetic operator

Equality Operator Priority( ==, !=) Are below the the relational operators

Page 8: CONTROL STRUCTURES (SELECTION)

LOGICAL OPERATORS

Logical Operators ( !, &&, || ):Allow the combination of boolean values according to the rules of mathematical logic. Logical expressions evaluate to a boolean result (true or false)

Not (!) Inverts the boolean value

true becomes false or false become true

Page 9: CONTROL STRUCTURES (SELECTION)

LOGICAL OPERATORS

AND (&&) Evaluates to true only if both values are true

bool x;

bool y;

X && Y ==

True && True True

True && False False

False && True False

False && False False

Page 10: CONTROL STRUCTURES (SELECTION)

LOGICAL OPERATORS

OR ( || ) Evaluates to false only if both values are false

bool x;

bool y;

X || Y ==

True || True True

True || False True

False || True True

False || False False

Page 11: CONTROL STRUCTURES (SELECTION)

OPERATOR PRECEDENCE

All operator precedence:

Page 12: CONTROL STRUCTURES (SELECTION)

BOOLEAN, RELATIONAL, EQUALITY AND LOGICAL OPERATIORS

Example:

bool flag1, flag2, flag3;

int num1, num2;

flag1 = (98 % 13 != 98 / 13) || (2 * 5 != 10);

cout << "The truth value of variable flag1 is " << flag1 << endl;

num1 = 12 + 3 * 7;

num2 = 10 + num1 % 3;

flag2 = (num1 > num2) && (num2 < 12);

cout << "The truth value of variable flag2 is " << flag2 << endl;

flag3 = flag1 || !flag2;

cout << "The truth value of variable flag3 is " << flag3 << endl;

Page 13: CONTROL STRUCTURES (SELECTION)

SELECTION

MAKING CHOICES One-Way (choose to do or not to do)

if

Two-Way (choose to do one or the other)if – elseconditional operator

Multiple Selection(choose to do one of many)nested if - else switch

Page 14: CONTROL STRUCTURES (SELECTION)

ONE-WAY SELECTION

SIMPLE if ( RELATIONAL EXPRESSION(s) )

STATEMENT;

COMPOUND if ( RELATIONAL EXPRESSION(s) )

{STATEMENT;STATEMENT;STATEMENT(s);

}

Page 15: CONTROL STRUCTURES (SELECTION)

ONE WAY SELECTION

RULES1. The statement(s) is/are executed if

and only if the relational expression(s) evaluate(s) to true.

Page 16: CONTROL STRUCTURES (SELECTION)

ONE WAY SELECTION

EXAMPLES:if (current_balance > 1000)

interest = current_balance * 0.015;

if (day == 7 && hours_worked > 40)

{

ot_pay = (hours_worked – 40) * pay_rate * 2;

total_pay = 40 * pay_rate + ot_pay;

}

Page 17: CONTROL STRUCTURES (SELECTION)

ONE WAY SELECTION EXAMPLE:

int main()

{   bool isEven;   int num;

  cout << "Enter an integer ";   cin >> num;   cout << endl;

  if (num > 10)       cout << num << " is greater than 10" << endl;

  isEven = (num % 2 == 0);   if (isEven)       cout << num << " is an EVEN number." << endl;

  if (!isEven)       cout << num << " is an ODD number." << endl;

  system ("pause");   return 0;

}

Page 18: CONTROL STRUCTURES (SELECTION)

TWO WAY SELECTION

SIMPLE if ( RELATIONAL EXPRESSION(s) )

Statement;

else

Statement;

Page 19: CONTROL STRUCTURES (SELECTION)

TWO WAY SELECTION

COMPOUND if ( RELATIONAL EXPRESSION(s) )

{Statement;Statement(s);

}else{

Statement;Statement(s);

}

Page 20: CONTROL STRUCTURES (SELECTION)

TWO WAY SELECTION

RULES1. First statement(s) will be executed if and

only if the evaluation of the relational expression(s) is/are true.

2. Second statement(s) will be excuted if and only if the evaluation of the relational expression(s) is/are false.

Page 21: CONTROL STRUCTURES (SELECTION)

TWO WAY SELECTION

EXAMPLESchar employee;

string state;

float ot_pay, pay_rate, gross_pay;

float hours_worked, tax;

Example 1:

if (state == “CO”)

tax = 0.065;

else

tax = 0.05;

Page 22: CONTROL STRUCTURES (SELECTION)

TWO WAY SELECTION

EXAMPLESExample 2:

if (employee == ‘E’ || employee == ‘S’)

gross_pay = 40 * pay_rate;

else

{

reg_pay = pay_rate * 40;

ot_pay = (hours_worked – 10) * pay_rate * 1.5;

gross_pay = reg_pay + ot_pay;

}

Page 23: CONTROL STRUCTURES (SELECTION)

CONDITIONAL OPERATOR

Can Accomplish Two Way Selection Using The Conditional Operator (?:) expression1 ? expression2 : expression3

Examplestate == “CO” ? tax = 0.065 : tax = 0.05;

Cannot Include Compound Statements