0% found this document useful (0 votes)
3 views6 pages

Algorithm 3

The document explains selection control structures in programming, which allow computers to make decisions based on conditions using if-then-else statements. It provides examples of decision-making processes, such as determining the larger of two numbers and evaluating student marks for pass or fail status. Key concepts include condition evaluation, branching, and comparison operators.

Uploaded by

9db7m95yxv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Algorithm 3

The document explains selection control structures in programming, which allow computers to make decisions based on conditions using if-then-else statements. It provides examples of decision-making processes, such as determining the larger of two numbers and evaluating student marks for pass or fail status. Key concepts include condition evaluation, branching, and comparison operators.

Uploaded by

9db7m95yxv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Created by Turbolearn AI

Selection Control Structures


Selection control structures involve instructing a computer to make a decision based
on conditions.

Analogy to a Three-Year-Old
When programming a computer (or instructing a three-year-old), you typically
provide instructions sequentially: statement one, statement two, statement three.
However, to enable decision-making, you must program how the decision will be
made.

The Fork in the Road


The decision-making process can be visualized as a fork in the road, leading to
option one or option two. The computer must decide which path to take based on a
specific condition.

Using if-then-else statements allows the computer (or a three-year-old) to


make the correct choice.
The condition determines which direction to take.

Condition: A key factor used to determine whether a particular decision is


made.

Example: Getting a Bottle of Water


Consider telling a child to fetch a bottle of water from the fridge.

Instruction: "Go to the fridge and get me a bottle of water."


Adding a condition: "If it is cold, bring it. Else, bring a cup with ice and the
bottle."

Condition Example: Is it Raining?


Consider the condition "if rain is falling." The possible answers are yes or no.

Page 1
Created by Turbolearn AI

If the answer is yes, the instruction is "stay inside."


If the answer is no, another instruction might be followed.
Everything hinges on the status of the condition.

Computer's Understanding
Conditions must be given in a way that the computer understands, using variables
and logic (equal to, less than, greater than, etc.).

Finding the Larger of Two Numbers


Demonstration of how a computer determines the larger of two numbers using
sequential instructions and conditional logic.

Sequential Instructions
1. Enter first number (displayed to the user).
2. Read number into num1.
The computer creates a box (memory location) called num1.
3. Enter second number (displayed to the user).
4. Read number into num2.
The computer creates a box called num2.

User Input
The user (not the programmer) enters the numbers. For example:

num1 =7
num2 = 2

Decision-Making Process
The condition is: "if num1 is greater than num2?"

The computer checks if the value in num1 is greater than the value in num2.
The answer can be yes or no.

Page 2
Created by Turbolearn AI

Scenario 1: 7 and 2
Is 7 > 2? Yes.
The computer prints: "The larger number is num1" (which is 7).
The algorithm works correctly.

Scenario 2: 2 and 7
Values are reversed: num1 = 2, num2 = 7
Is 2 > 7? No.
The computer prints: "The larger number is num2" (which is 7).
The algorithm still works correctly.## Selection Control Structures

Selection control structures allow a computer to make decisions based on


conditions. This enables programs to execute different code paths depending on
whether a condition is true or false.

How Selection Works


1. Condition Evaluation: A condition is evaluated.
2. Branching:
If the condition is true, one block of code is executed.
If the condition is false, a different block of code is executed (or no code is
executed).

Condition: A statement that evaluates to either true or false, used to


determine which code path to follow.

Example: Determining the Lowest Price


The following algorithm accepts the prices of two computers and determines the
lowest price:

Page 3
Created by Turbolearn AI

1. Start
2. Enter first price (p1)
3. Enter second price (p2)
4. If p1 < p2:
low = p1

5. Else:
low = p2

6. Display low

Explanation:

The algorithm starts by accepting two prices, p1 and p2.


The condition $p1 < p2$ is evaluated.
If the condition is true, the value of p1 is assigned to the variable $low$.
If the condition is false, the value of p2 is assigned to the variable $low$.
Finally, the value of $low$ is displayed.

Example:

p1 = 10

p2 = 12

Is p1 < p2? Yes, because 10 is less than 12. Therefore, low = 10, and 10 is displayed.

Example: Pass or Fail Algorithm


This algorithm prompts the user to input a student's mark and determines whether
the student has passed or failed. The passing mark is 60.

1. Variables:
mark (or m)
2. Start
3. Print "Please enter the mark the student got."
4. Read m
5. If m > 59:
Print "You have passed."
6. Else:
Print "You failed to pass this time."
7. Stop

Page 4
Created by Turbolearn AI

Explanation:

The algorithm starts by prompting the user to enter the student's mark.
The input is stored in the variable m.
The condition m > 59 is checked.
If m is greater than 59, the algorithm prints "You have passed."
Otherwise, it prints "You failed to pass this time."
The algorithm then stops.

Alternative Condition: Instead of using m > 59, you can use m >= 60. Both conditions
achieve the same result because the passing mark is 60.

Key comparison operators

Operator Meaning

< Less than


> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to

Simple Selection Algorithm Example


Here's an example of a selection algorithm:

1. Input: Ask the user to enter a mark (m).


2. Check:
If m is greater than or equal to 60, print "You have passed."
Otherwise, print "You have failed."

This algorithm checks whether a student has passed or failed based on their mark.

Determining Pass or Fail Based on Average

Page 5
Created by Turbolearn AI

Let's consider a more complex problem: Ask the user to input three numbers, find
their average, and determine if the average indicates a pass or fail. The passing mark
is 50.

1. Algorithm Name: pass_or_fail

2. Variables:

m1 : Mark 1
m2: Mark 2

m3: Mark 3

average: The calculated average of the three marks

3. Start:

Print "Enter your three marks."


Read m1, m2, m3.

4. Calculate Average:

average = (m1 + m2 + m3) / 3

The average is calculated by summing the three marks and dividing by 3.

5. Check Pass or Fail:

If average is greater than or equal to 50, print "You have passed."


Else, print "You did not pass."

6. Stop.

This selection algorithm determines if a student has passed or failed based on the
average of three marks.

Page 6

You might also like