05-Unit4-CS 1 – SELECTION CONTROL STRUCTURE
05-Unit4-CS 1 – SELECTION CONTROL STRUCTURE
CONTROL
STRUCTURE
Selection control structures are programming
constructs that allow a program to make
decisions and execute different sets of
instructions based on certain conditions. These
structures enable the program to choose
between alternative paths of execution,
depending on whether a particular condition is
true or false.
There are typically two types of selection control structures: if
statements and switch/case statements.
1. If Statements:
The basic form of an if statement in most programming
languages looks like this:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Here's a breakdown of the components:
{}: Curly braces define the scope of the code blocks. The
code inside the curly braces is executed conditionally based
on the evaluation of the if statement.
Here's a simple example in pseudocode:
Start
Input age
if (age >= 18) {
Display "You are an adult.“
else if (age >= 13) {
Display "You are a teenager."
} else {
Display "You are a minor."
}
End
Here's a simple example in flowchart:
2. Switch Statements:
Switch statements are used when you have a variable
that can take on multiple values, and you want to
perform different actions based on the value of that
variable. The switch statement is a selection control
structure that allows a variable or expression to be
tested for equality against a list of values. It provides
an efficient way to handle multiple conditions.
The basic form of a switch statement in various programming languages
typically looks like this:
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
// additional cases as needed
default:
// code to be executed if none of the cases match
break;
}
Here's a breakdown of the components:
Input day
switch day
case "Monday":
Display "It's the start of the week."
break
case "Friday":
Display "It's almost the weekend."
break
default:
Display "It's a regular day."
break
end switch
Here's a simple example in flowchart:
SELECTION
CONTROL
STRUCTURE
ALGORITHM
Selection control structures are an
integral part of algorithms, helping them
make decisions based on specific
conditions. Here's how you can
incorporate selection control structures
into an algorithm:
1. Identify Decision Points:
Look for points in your algorithm where different actions
need to be taken based on certain conditions.
2. Formulate Conditions:
Define the conditions that will determine the path of
execution. These conditions are typically expressed as logical
expressions involving variables or values.
3. Use If Statements:
Introduce if statements to check the conditions. If the
condition is true, the algorithm executes one set of
instructions; if false, it executes another set.
4. Consider Else-If (Optional):
If there are multiple conditions to check, you can use else -if
statements to handle additional cases.
if (condition1) {
// code for condition1 being true
} else if (condition2) {
// code for condition2 being true
} else {
// code for no conditions being true
}
5. Integrate with the Algorithm Flow:
Incorporate the selection control structures
seamlessly into the flow of your algorithm. The selected
path of execution should align with the algorithm's
overall logic.
6. Test with Various Inputs:
Test your algorithm with different inputs to ensure
that it correctly follows the specified conditions and
executes the appropriate code blocks.
Here's a simple example of how selection control
structures can be used in an algorithm:
DetermineGrade(score):
else:
End Algorithm
ASSIGNMENT
I. Algorithms Using Selection For each of the following scenarios,
write an algorithm using pseudocode and a flowchart to represent
the decision-making process:
1. Even or Odd:
• Input: An integer number
• Output: "Even" or "Odd“
2. Voting Eligibility:
• Input: Age of a person
• Output: "Eligible to vote" or "Not eligible to vote"
START
INPUT
SCORE
If If
If
NO score NO score
score
>=80 >=70
>=90
END GRADE F