This document discusses selection and conditional branching in algorithms. It covers:
- There are three basic control structures: sequential, selection, and iteration. Selection means the program will do something based on a condition.
- Selection structures require relational operators like =, <, > to compare values.
- There are three levels of selection: IF-THEN for a single action, IF-THEN-ELSE for two possible actions, and IF-THEN-ELSE-IF for multiple possible actions.
- Examples are given of pseudocode and Pascal programs using IF-THEN and IF-THEN-ELSE to check conditions and perform different actions based on the results.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2K views13 pages
Selection Construct
This document discusses selection and conditional branching in algorithms. It covers:
- There are three basic control structures: sequential, selection, and iteration. Selection means the program will do something based on a condition.
- Selection structures require relational operators like =, <, > to compare values.
- There are three levels of selection: IF-THEN for a single action, IF-THEN-ELSE for two possible actions, and IF-THEN-ELSE-IF for multiple possible actions.
- Examples are given of pseudocode and Pascal programs using IF-THEN and IF-THEN-ELSE to check conditions and perform different actions based on the results.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
SELECTION
STATEMENT/CONDITIONAL BRANCHING Standard • Every algorithm can be constructed using only sequencing, selection and iteration
There are three
control structures Control Structures • Sequential means each line is executed line by line • Selection means the program will do something based on a condition • Iteration means a command is repeated multiple times Categories of Selection • There are THREE basic levels of Selection: • IF THEN: This is used when there is only one thing that may be done on a condition. IF THEN ELSE: This is used when One of TWO things may be done depending on the truthfulness or falsity of a condition IF THEN ELSE IF: This is used if TWO or MORE things can be done depending on the Truthfulness or falsity of an algorithm Relational Operators • Selection control structures requires us to use relational operators. Relational implies comparing two or more things: Algorithm Pascal Meaning = = Equal to < < Less than > > Greater than <= <= Less than or equal to
>= >= Greater than or equal
to <> <> Not equal to = = Not equal to IF THEN • We will now look at the format for writing IF THEN statements in our programs. The rest we will explore later.
• Here is the format in algorithm:
– IF (condition) THEN • Do Something – ENDIF Problem Statement Write a pseudocode and pascal program to call a person at 65 and older as being “Truly Blessed”. Pseudocode (answer) START Declare age as INTEGER PRINT ‘‘ Please enter your age” Read age IF (age >=65) THEN PRINT “You are Truly blessed” ENDIF STOP Pascal(answer) Program Blessings; VAR Age:integer; BEGIN Writeln(‘This program will determine the age of a person at 65 and older as blessed’); Writeln(‘Please enter your age’); Read(age); If(age>=65)then BEGIN Writeln(‘You are really blessed’); End; End. What if in the very same IF THEN Statement we were to execute two or more statements , then we would have to use a new pair of BEGIN and END statements. These act like brackets around statements that follow THEN. Here’s an example Write a Pascal statement that gives a 10% discount to a customer that spends 100 or more. Pascal(answer) PROGRAM LessMoney; VAR Discount,total:real; BEGIN Writeln(‘This program is designed to give customers a 10% discount if they spend $100 or more’); If(total>=100)then BEGIN Discount:=total*0.10; Total:=total-discount; Writeln(‘Customer received a total of, ‘total’); End; End. Let’s look at a few questions now in groups of two.