Q2-LG03-Selection-Structure-If
Overview of Logical Control Structures in C++
Subject: CS2 - Introduction to Computational Thinking
Module Code: 4.0
Lesson Code: 4.2.1.1 - Selection Structure (If)
Time Frame: 30 minutes
Learning Objective
Understand Boolean operations and their application in branching structures.
Construct selection structure programs to solve specific tasks.
Introduction to Control Structures
Programs execute statements sequentially in their physical order until completion.
A need arises to change execution order based on conditions (input validation).
This requires the ability to ask questions and decide on next actions based on answers.
The Selection Control Structure
Definition: The selection or branching control structure allows the program to execute statements based on conditions.
If a condition is true, execute one statement.
If false, execute another or none.
Types of Selection Structures
One-way (Single) Selection: Executes one block if the condition is true.
Two-way (Double) Selection: Executes one block if true, another if false (if/else).
Multi-selection (Compound Block): Uses a switch statement to handle multiple choices.
Selection in C++
IF Statement: Executes actions based on a condition:
Structure:
if (condition) statement;If the condition is true, execute the statement; otherwise, proceed to the next.
IF/ELSE Statement: Chooses between two actions:
Structure:
if (condition) { statement1; } else { statement2; }
SWITCH Statement: Chooses actions based on an integer value; supports multiple selections.
Decision-Making in Programming
Conditions and Questions: In C++, questions are framed as conditions.
Example: Instead of asking "Are we having mass testing?", say, "We are having mass testing".
A true condition affirms the question (yes); false denies it (no).
Logical Expressions
Conditions are expressed as logical expressions that evaluate to true or false.
Forms include:
Boolean variables/constants.
Variables/values with relational operators (e.g.,
x > 5).Logical expressions combined with logical operators (e.g.,
(y < 10) && (b > 2)).
Relational Operators
Essential for selection control; meanings include:
==: Equal to!=: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
The IF Statement
Basic structure:
if (boolean_expression) statement;For compound statements (multiple lines):
if (boolean_expression) { statement1; statement2; }Real-life examples of IF statements to illustrate condition-based actions:
If it is hot outside, bring an umbrella.
If you are thirsty, drink something.
If the storm signal is #2, no classes.
If your gas is empty, fill up.
Example of IF Statement in C++ Programming
Example 1: Calculating average scores.
#include <iostream> using namespace std; int main() { int grade1, grade2, grade3; double average; cout << "Input 3 scores: "; cin >> grade1 >> grade2 >> grade3; average = (grade1 + grade2 + grade3) / 3; if (average > 60) { cout << "PASSED"; } }Sample output: "PASSED" for average score >= 60.
Example 2: Positive integer check.
#include <iostream> using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; if (number > 0) { cout << "You entered a positive integer: " << number << endl; } cout << "This statement is always executed."; return 0; }Sample outputs illustrate behavior for both positive and negative inputs.
Conclusion
The C++ selection control structure is crucial for executing code conditionally based on user input or other criteria.
Comprises one-way (IF), two-way (IF-ELSE), and multi-way (switch) options.
Encourages logical thinking similar to human decision-making processes.