ICS 101-Computer programming

                                     Lab # 4

                                (IF Structure)

 

 

Objectives

 

                        

  Syntax                                              Example

 

   

 

 

 

 

 

 

 If condition evaluates to .TRUE. then BLOCK 1 will be executed, otherwise BLOCK 2

  will be executed.

 

 

    IF Construct

 

Syntax                                               Example

 

 

 

 

 

 

If condition evaluates to .TRUE. then BLOCK will be executed, otherwise not.

  

 

     IF- ELSEIF  Construct

 

   Syntax                                                Example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If condition 1 evaluates to .TRUE. then BLOCK 1 will be executed

If condition 2 evaluates to .TRUE. then BLOCK 2 will be executed

...

...

If condition n evaluates to .TRUE. then BLOCK n will be executed

If no condition evaluates to .TRUE. then BLOCK n+1 will be executed

 

 

  Simple IF Construct

 

Syntax                                            Example

 

 

 

 

 

If condition evaluates to .TRUE. then STATEMENT will be executed

 

 

Example

 

 

 

Following is the output of the program.

 

 

 

 

 

Exercise 1

 

Write a program that prompts for and reads three integer numbers it  then prints the minimum number.

  

 

Exercise 2

 

A salesperson is given commission on the following basis:

 

SALES

COMMISSION

Under 500

2 % of SALES

500 and under 5000

5 % of SALES

5000 and over

8 % of SALES

 

Write a program, which prompts for and reads SALES and prints the corresponding commission.

 

Exercise 3

 

Write a program, which prompts for and reads a positive integer N. It then determines whether the integer N is divisible by 3 or not. The program then prints one of the messages DIVISIBLE BY 3 or NOT DIVISIBLE BY 3 accordingly.

 

Exercise 4

 

Write a program that reads the coordinates X and Y of a point in a plane then it determines whether the point is in quadrant 1, 2, 3 or  4 or whether it is the origin.