Presentation on theme: "Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’"— Presentation transcript:
Topics: Selection Statements
Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’ command. The IF statement contains a condition that is made up of three parts: Variable to be tested Relational operator Variable or constant to which the variable to be tested is compared to.
If statements: IF-THEN IF-THEN-ELSE Flowchart Symbols: Decision making and branching Connector or joining two parts of a program. It is also used to connect sections of a flowchart when a flowchart is long and cannot fit on a page. A letter or digit can be placed in the small circle to indicate the link on the first page. Another identically labeled connector is placed on the next page to indicate the continuation of flow.
IF-THEN When one option is available and a selection may or may not be made, the IF-THEN is used. Pseudocode IF (condition) then One or more instructions which will be carried out if the condition is true ENDIF
Flowchart no yes Condition Statement - True
Problem 1: If a student’s grade is greater than 85%, then she is given 3 merits. Solution: Pseudocode IF (grade > 85) then merit = 3 ENDIF Examples IF-THEN Flowchart no yes grade > 85 merit = 3
Problem 2: Read the time. If the time is 11:00, output “Ring the bell”. Solution Pseudocode Read time IF (time = 11) then Print “Ring the bell” ENDIF Flowchart Students is required to do the flowchart
Problem 3: Write a program to read a number N. If N is greater than or equal to 100, add 10 to the number and display the result. Print the number. Requirements: Write the Pseudocode Draw the flowchart
Pseudocode Start Declare N, result as Integer Print “A program that accepts a number, and if the number is greater than 100 then 10 is added to the number.” Print “Enter a number: ” Read N IF (N >= 100) then result = N + 10 Print “Result is ” result ENDIF Print “Number is ” N Stop
Flowchart START STOP Print “Enter a number” Read N N >= 100 Print “Result is ”, result result = N + 10 Print “Number is ”, N No Yes
Problem Write a program that calculates the total price of a ruler and a pencil. If the total price is more than $150, a discount of 5% is given. Compute and print the discount amount. Input the prices for the ruler and pencil and display the total price and the amount payable.
Requirements List the operations Write the Pseudocode Draw the Flowchart.
IF-THEN-ELSE When two options are available and a selection must be made, the IF-THEN-ELSE is used. Pseudocode IF (condition) then One or more instructions which will be carried out if the condition is true else One or more instructions which will be carried out if the condition is false ENDIF
Flowchart yes no Condition Statement - TrueStatement - False
Examples IF-THEN-ELSE Problem 1: If sales is greater than $7000, commission is $500 otherwise commission is $250 Solution: Pseudocode IF (sales > 7000) then commission = 500 else commission = 250 ENDIF Flowchart yes no Sales > 7000 Commission = 500Commission = 250
Examples IF-THEN-ELSE Problem 2: Read the age of a person. If age is greater than 35, output “old person” otherwise output “young person” Solution: Pseudocode Read age IF (age > 35) then Print “Old person” else Print “Young person” ENDIF Flowchart Students is required to do the flowchart
Problem 3: Write a program that accepts employee’s salary. If the salary is less than or equal to $44000, then a bonus of 15% is given, otherwise 10% is given. Calculate and display the bonus amount given. Requirements: Write the Pseudocode Draw the Flowchart
Pseudocode Start Declare salary, bonus as Real Print “A program that accepts the salary for an employee, and calculate the bonus given” Print “Enter salary” Read salary IF (salary <= 44000) then bonus = salary * 0.15 else bonus = salary * 0.10 ENDIF Print “Bonus amount is ” bonus Stop
Problem A credit union pays 4% interest on shares that are greater than $25000 and 3% on all other shares. No interest is paid on deposits. Write a program that prompts the user to input a share a deposit. The program should calculate and output the interest amount and the total savings (Total savings = shares + deposit + interest amount).
Requirements List the operations Write the Pseudocode Draw Flowchart
Home Work Cambridge – Information Technology for CSEC Exercise 22 – Numbers 1 - 5