Docsity

Selection Statements in Computer Programming: IF, THEN, ELSE, Slides of Computer science

Partial preview of the text

Download Selection Statements in Computer Programming: IF, THEN, ELSE and more Slides Computer science in PDF only on Docsity!

AQA

AS Level

Computer

Science

Paper 1

Unit 1 Fundamentals of programming

Objectives

  • (^) be able to use relational operators
  • (^) be able to use Boolean operations AND, OR
  • (^) be able to use nested selection statements

Unit 1 Fundamentals of programming Relational operators

> greater than

< less than

=> greater than or equal to

=< less than or equal to

== equal to (= in other languages)

!= not equal to (<> in other languages)

  • (^) Operators vary according to programming languages

Unit 1 Fundamentals of programming Complete the table: X Y condition True or False? 5 4 X > Y 4 4 X <= Y 10 11 X >= Y 10 10 X >= Y 8 9 X != Y

Unit 1 Fundamentals of programming IF.. THEN.. END IF

In the example below OUTPUT “Y is less than X”

would not be executed as the condition Y < X is not

true

X  3

Y  4

IF X < Y THEN

OUTPUT “X is less than Y” END IF IF Y < X THEN OUTPUT “Y is less than X” END IF

Unit 1 Fundamentals of programming IF.. THEN.. END IF

What would be output by this program?

X  4

Y  4

IF X < Y THEN

OUTPUT “X is less than Y” END IF IF Y < X THEN OUTPUT “Y is less than X” END IF IF X == Y THEN OUTPUT “Y is the same as X” END IF

Unit 1 Fundamentals of programming Complex Boolean expressions

  • (^) In maths BIDMAS shows the order of operation in a

mathematical expression

  • (^) If more than one logical operator is used: Operator Description

NOT Evaluated first

AND Evaluated second

OR Is evaluated third

Unit 1 Fundamentals of programming Examples IF age > 12 AND height > 1.5 THEN OUTPUT “You can ride the roller coaster” END IF IF age <13 OR age > 19 THEN OUTPUT “You are not eligible for teen discount” END IF IF NOT ((age > 12) AND (height > 1.5)) THEN OUTPUT “You cannot ride the roller coaster” END IF

Unit 1 Fundamentals of programming Boolean expressions

Answers:

X Y Z Condition True or False? 3 4 5 X > Y AND Z > Y FALSE 3 4 5 X + 3 > Y OR X -2 <=Y TRUE 4 5 6 (X <= Y AND Y=Z) OR (Z>X) TRUE 4 5 6 (X <= Y) AND (Y=Z OR Z<X) FALSE 3 9 11 Y % X = 0 AND Z % X = 2 TRUE 3 9 11 NOT ((Z > X) AND (Z > Y)) FALSE

Unit 1 Fundamentals of programming IF .. THEN.. ELSE.. END IF

  • (^) Often, alternative statements need to be executed if

the given condition is not met

  • (^) The IF .. THEN .. ELSE statement provides this

option

IF condition c THEN statements a, b, c,.. ELSE statements x, y, z,.. END IF

Unit 1 Fundamentals of programming Worksheet 2

  • (^) Now work through the questions in Task 1 on the

worksheet

Unit 1 Fundamentals of programming IF .. THEN .. ELSE IF .. ELSE

  • (^) Use multiple IF ..THEN.. ELSE IF statements to

evaluate more than one condition

  • (^) What would the output be for each of the following

values?

  • (^) age = 11, age = 12 and age = 13 IF age < 12 THEN OUTPUT “Age is less than 12” ELSE IF age = 12 THEN OUTPUT “Age is 12” ELSE OUTPUT “Age is greater than 12” END IF

Unit 1 Fundamentals of programming Sample problem

  • (^) Suppose you need to calculate grades from exam

marks and print out the grade for each student

  • (^) What grade boundaries would you select?
  • (^) What input would you need?
  • (^) What conditions would you evaluate?
  • (^) What output statements would you need?

Unit 1 Fundamentals of programming Worksheet 2

  • (^) Now work through Task 2 on the worksheet