1 / 23

CONTROL STRUCTURES (SELECTION)

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)

lamont
Download Presentation

CONTROL STRUCTURES (SELECTION)

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CONTROL STRUCTURES(SELECTION)

  2. 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)

  3. 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

  4. BOOLEAN DATA TYPE • Boolean names should reflect true value For 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

  5. RELATIONAL / EQUALITY OPERATORS • Boolean expression is: A condition in which a relational operator tests the relationship between two values or expressions and returns 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;

  6. RELATIONAL / EQUALITY OPERATORS Evaluation of either a relational or equality operator always results to either true or false.

  7. RELATIONAL / EQUALITY OPERATORS • Relational Operator Priority • (<, <=, >, >=) Are below the arithmetic operator • Equality Operator Priority • ( ==, !=) Are below the the relational operators

  8. 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

  9. LOGICAL OPERATORS • AND (&&) • Evaluates to true only if both values are true bool x; bool y;

  10. LOGICAL OPERATORS • OR ( || ) • Evaluates to false only if both values are false bool x; bool y;

  11. OPERATOR PRECEDENCE • All operator precedence:

  12. 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;

  13. SELECTION • MAKING CHOICES • One-Way (choose to do or not to do) • if • Two-Way (choose to do one or the other) • if – else • conditional operator • Multiple Selection(choose to do one of many) • nested if - else • switch

  14. ONE-WAY SELECTION • SIMPLE • if ( RELATIONAL EXPRESSION(s) ) STATEMENT; • COMPOUND • if ( RELATIONAL EXPRESSION(s) ) { STATEMENT; STATEMENT; STATEMENT(s); }

  15. ONE WAY SELECTION • RULES 1. The statement(s) is/are executed if and only if the relational expression(s) evaluate(s) to true.

  16. 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; }

  17. 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; }

  18. TWO WAY SELECTION • SIMPLE • if ( RELATIONAL EXPRESSION(s) ) Statement; else Statement;

  19. TWO WAY SELECTION • COMPOUND • if ( RELATIONAL EXPRESSION(s) ) { Statement; Statement(s); } else { Statement; Statement(s); }

  20. TWO WAY SELECTION • RULES 1. 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.

  21. TWO WAY SELECTION • EXAMPLES char 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;

  22. TWO WAY SELECTION • EXAMPLES Example 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; }

  23. CONDITIONAL OPERATOR • Can Accomplish Two Way Selection Using The Conditional Operator (?:) • expression1 ? expression2 : expression3 • Example state == “CO” ? tax = 0.065 : tax = 0.05; • Cannot Include Compound Statements

More Related