0% found this document useful (0 votes)
10 views

05-Unit4-CS 1 – SELECTION CONTROL STRUCTURE

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

05-Unit4-CS 1 – SELECTION CONTROL STRUCTURE

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

SELECTION

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:

if: The keyword that starts the if statement.

(condition): The condition that is evaluated. If it is true, the


code inside the first block is executed; otherwise, the code
inside the else block is executed (if an else block is present).

{}: 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:

switch: The keyword that starts the switch statement.


(expression): The expression whose value is being compared against
different cases.
case value1: : A specific value that the expression is compared
against. If the expression equals `value1`, the corresponding code
block is executed.
break;: This statement is used to exit the switch statement after a
case is matched and executed. It prevents fall -through to subsequent
cases.
default: : Optional. If none of the cases match the expression, the
code block under `default` is executed.
break; (after default): Also optional, but used to exit the switch
statement after the `default` case is executed.
Here's a simple example in pseudocode:

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):

if score >= 90:


ACTIVITY:
Display "Grade: A"
Create the flowchart
else if score >= 80:

Display "Grade: B" of the pseudocode


else if score >= 70:
given on the left side.
Display "Grade: C"

else:

Display "Grade: F"

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

YES YES YES


GRADE A GRADE B GRADE C NO

END GRADE F

You might also like