Cards (48)

  • A computer can process a program in different ways, such as sequence, selection or branching, and repetition or looping; these are called control structures. These are the building blocks that dictate the flow of a program’s execution.
  • In using these control structures, the applications or programs are dynamic and responsive to different scenarios.
  • Different Selection Structures:
    • One-Way Selection
    • Two-Way Selections
    • Multiple Selections
    • Compound (Block) Statements
    • Nested Selections
    • Switch Structure
    • Conditional Operator
  • A one-way selection structure, also called a single alternative structure, is a type of control structure where a certain block of statements is executed when the condition is true. However, if the condition is false, it will not perform anything and will skip the control structure block and proceed to the next statement.
  • if is a reserved word
  • In One-Way Selection Structure, an expression is also called a decision maker or condition; this is the logical expression within parentheses that results in either TRUE or FALSE.
  • In One-Way Selection Structure, A statement is also called an action statement, which is executed when the result of the expression is TRUE.
  • Fill in the Blank
    A) if
    B) (expression)
    C) statement
    D) expression
    E) true
    F) statement
    G) false
  • What selection structure is this?
    A) One-Way Selection Structure
  • A two-way selection structure, also called a double alternative structure, is a type of control structure where there are two alternatives to select from. A certain block of statements is executed when the condition is true; however, if the condition is false, it will execute the statements under the false condition and then proceed to the next statement.
  • Fill in the Blank
    A) if
    B) (expression)
    C) statement1
    D) else
    E) statement2
    F) expression
    G) true
    H) statement1
    I) false
    J) statement2
  • In Two-Way Selection Structure, if and else are reserved words.
  • In Two-Way Selection Structure, If the expression result is TRUE, it will execute statement1. If the expression result is FALSE, it will execute statement2.
  • What selection structure is this?
    A) Two-Way Selection Structure
  • Every if statement does not require a semicolon because it is control structure
  • A multiple selections structure, also called a multiple alternatives structure, is a type of control structure where there are more than two alternatives to select from. It is useful if the problem has multiple conditions or possible outcomes to evaluate from.
  • What selection structure is this?
    A) Multiple Selections Structure
  • Both programs A and B will produce the same output; however, in Program A, if the first condition is evaluated as true, it will skip the rest of the else if and else statements. Unlike in Program B, it will evaluate every if statement, which will cause the program to be slower compared with Program A.
  • In C++, the if and else structures control only one statement at a time. To permit multiple or complex statements, C++ provides a structured construct called compound statements or blocks of statements.
  • A compound statements consists of one or more statements enclosed in curly braces.
  • What statement is this?
    A) Compound statement
  • A nested if in C++ is an if statement within another if statement. The nested if statements allow complex decision-making because they create additional layers of conditions that check the values.
  • Nested if syntax
  • What selection structure is this?
    A) Nested if Selections Structure
  • A switch structure is a control statement that executes different parts of the code based on a single value. It is an alternative way of using the multiple if-else structure. It is typically used for integers, characters, or any enumerated types of values.
  • In One-Way Selection Structure, if is a reserved words.
  • In Switch Structure, switch, case, default, and break are reserved words.
  • In Switch Structure, expression is an identifier (variable) or an equation to be evaluated and then compared to each case; it is also called a selector.
  • In Switch Structure, value1, value2, ... valuen are constant values that are possible to be equal to the result of the expression or the value of an identifier.
  • In Switch Structure, the break statement will cause the program to exit or end the switch structure. Without break, C++ will continue to execute the succeeding case(s) even if it is FALSE; this is called fall-through.
  • In Switch Structure, default is optional; it is similar to else in the if-else structure, where C++ executes default if no case matches the value of the expression.
  • What structure is this?
    A) Switch Structure
  • A switch structure can also perform an expression with a result of true and false values.
    What structure is this?
    A) Nested Switch Structure
  • The conditional operator is also called a ternary operator, which takes three (3) arguments. It is a shorthand for the if-else statement in a single expression using the ?: symbol. It is an efficient way to make simple decisions in the program code.
  • Conditional Operator Syntax
  • Fill in the Blank
    A) expression1
    B) expression2
    C) expression3
  • What operator is used in this picture?
    A) Conditional Operator
  • In C++, the cin.fail() function is used to check and detect if the input value is valid. It typically happens when the type of the input value does not match the type of the variable, such as non-numeric characters being input into an integer variable.
  • Input Validation
    Parameters:
    • cin.fail() – Used to check if the input failed.
    • cin.clear() – Resets the failure state of the cin object.
    • cin.ignore(n, delim) – Skips or discards characters in the input buffer.
    • n – Maximum number of characters (optional).
    • delim – Delimiter character used to stop ignoring; by default, it is '\n' (optional).
  • Input Validation of cin.fail()