| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Lesson 2-6: Programming Constructs   IF  THEN  ELSE

Page history last edited by Ms. R. Singh 11 years, 6 months ago

PROGRAMMING CONSTRUCTS

 

* Sequential – all statements are executed in the order they arewritten.

* Selection – a statement/set of instructions that is/are executed if a condition is satisfied

Repetition/Iteration – a statement/set of instructions thatis/are executed until a condition is satisfied a specific number of times.

 

 

 

 

SELECTION  STRUCTURES

 

Selection (decision)- This construct is used when no action needs to be performed if the expression returns false.

 

The IF…THEN   selection statement

 

Syntax:

 

 IF (expression) THEN

                        {Statement (s)}            Execute statement(s) if logical expression is TRUE

 

 

Eg.1) Write an algorithm to read the score of a student in an examination and determine whether the student has passed. If the score is greater than or equal to 50, the student has passed. Print whether the student has passed.

 

 

Answer

  Start                                                                                                     

     Print (‘Please enter the score’)

     Input Score

     IF Score >= 50 THEN

              Print ‘PASS’

Stop

 

 

 

 

  

The IF … THEN … ELSE construct syntax:

 

IF (expression) THEN

                        {Statements}    executed only if condition is TRUE

            ELSE                                                                                                                                     

                        {Statements}    executed only if condition is FALSE

ENDIF

 

Only one group of statements could be executed each time the program is executed

 

 

 

Eg.2) Using the scenario in Example1, determine whether the student has passed or failed and print the result.

 

Answer 

Start

     Print (‘Please enter the score’)

     Input Score

     IF Score >= 50 THEN

              Print ‘PASS’

     ELSE

              Print ‘FAIL’

Stop

 

 

 

 

 

Eg.3) Write an algorithm which accepts a number and determines whether the number is ‘odd’ or ‘even’.  Print the result.

 

Answer

Start

     Print(‘Please enter a number’)

     Input num

     IF (num MOD 2 = 0) THEN

               Print(num,’  is even’)

     ELSE

               Print(num,’ is odd’)

Stop

 

 

 

Eg.4) Write an algorithm which reads the age of a person and determine whether he/she is a student. The person is considered a student if he/she is between the ages of 5 and 18. If the person is under the age of 5 then he/she is a ‘toddler’. Otherwise, the person is an adult.  Print the age of the person.

 

 

Answer

Start

     Print(‘Please enter the person’s age’)

     Input age

     IF (age <= 18) AND (age >=5) THEN

                        Print(‘You are a student’)

     ELSE  IF (age < 5) THEN

                        Print(‘You are a toddler’)

     ELSE

                        Print(‘You are an adult’)

Stop

Comments (0)

You don't have permission to comment on this page.