Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Saturday, February 5, 2022

Conditional statements using SUB....END SUB

 

Conditional Statements


Conditional statement transfers the control from one part to another by testing the condition. In the program may contain many blocks but any one of that has to be select. Such problems can be solved by the help of logical expression. These statements required relational and logical operators to test and select the best alternative among many alternatives.

The conditional branching statements are:

IF…….THEN

IF …..THEN……ELSE

IF ELSEIF…..ELSE (Block IF)

SELECT CASE.

 

IF…THEN Statement

IF…THEN is a conditional branching statement which is used for comparison as well as decision making. This statement results either true or false based on the evaluation of an expression. IF…THEN statement can be solved by two ways. They are:

a) Single Line IF….THEN statement

This statement makes all of the given blocks of statement in a single line. This statement terminates itself when the entire line will finished so that it does not required END IF statement like block IF…THEN.

Syntax: IF condition THEN statement_for_true

Example:

To input marks and display the result (Assume pass marks is 35)

CLS

INPUT “Enter marks”;m

IF m>=35 THEN PRINT “PASS”

IF m<35 THEN PRINT “Fail”

END

 

b) IF…ENDIF statement

The IF…ENDIF is a conditional statement that is used to check the conditions specified and executes the statement written between ENDIF if the condition is satisfied. In this type of statements, more than one statements can be written in between IF and ENDIF.

Syntax:

IF (condition) THEN

Statement

…………………

END IF

 

Example: To input marks and display the result (Assume pass marks is 35)

CLS

INPUT “Enter marks”;n

IF n>=35 THEN

PRINT “PASS”

ELSE

PRINT “Fail”

END IF

END

 

 

 c) IF…THEN…ELSE Statement

It is a two way branching statement. It first checks the Boolean expression and takes the decision based on the result obtained. It executes the statements for true if he specified condition meets the requirements, otherwise, simply executes the statements written after ELSE.

Syntax: (Single line IF….ELSE)

IF <condition> THEN <statement> ELSE <statement>

         

Example:1

To input marks and display the result (Assume pass marks is 35)

CLS

INPUT "Enter makrs”;m

IF m>=35 THEN PRINT “Pass” ELSE PRINT “Fail”

END

           

d) IF …..ELSEIF……ELSE

It is a multi way branching statement. This statement is used when a gives more than one output. It executes the statements blocks if the particular condition is met, otherwise it simply executes the statement for false written after ELSE statement.

Syntax:

IF (condition) THEN

[statement block 1]

ELSEIF (condition) THEN

[statement block 2]

ELSE

[statement block n]

END IF

 

Example:

Program to check the division

CLS

INPUT “Enter marks : “ ; m

IF M > = 60 THEN

PRINT “First division”

ELSEIF m > = 50 AND m < 60 THEN

PRINT “Second division”

ELSEIF m > = 40 AND m< 50 THEN

PRINT “Third division”

ELSE

PRINT “Fail”

END IF

 

 

SELECT CASE Statement

The IF...THEN statement is the simplest form of conditional statement. It provides a split in program flow based on whether the provided condition is true or false. Unfortunately, IF...THEN statements do not work efficiently when there must be multiple decisions made for multiple values of a variable. The SELECT CASE statement compares one expression to different values. The advantage of the SELECT CASE statement over multiple IF…THEN…ELSE a statement is that it makes the code easier to read and maintain.

The SELECT CASE statement tests a single expression, which is evaluated once at the top of the structure. The result of the test is then compared with several values and if it matches one of them, the corresponding block of statements is executed.

           

Syntax:

SELECT CASE test expression

         CASE expression 1

         [statement block 1]

         CASE expression 2

         [statement block 2]

         CASE expression 3

         [statement block 3]

         …………………….

         CASE ELSE

         [statement block]

         END SELECT

Example:

To input day code and display the day name.

DECLARE SUB day()

CLS

CALL day

END

SUB

INPUT "Enter day code="; c

SELECT CASE c

CASE 1

PRINT "Today is Sunday"

CASE 2

PRINT "Today is Monday"

CASE 3

PRINT "Today is Tuesday"

CASE 4

PRINT "Today is Wednesday"

CASE 5

PRINT "Today is Thursday"

CASE 6

PRINT "Today is Friday"

CASE 7

PRINT "Today is Saturday"

CASE ELSE

PRINT "Invalid day code"

END SELECT

END SUB

 

  

Solved examples using different conditional statements

 

1. WAP to check whether the input number is positive or negative. Specification Grid 2065, SLC Supplementary 2066

Using IF…..THEN

DECLARE SUB positive()

CLS

CALL positive

END

SUB positive

INPUT "Enter any number=";n

r=SGN(n)

IF r= 0 THEN PRINT "The number is Positive"

IF r=-1 THEN PRINT "The number is Negative"

END SUB

 

Using IF…….ELSEIF…….ELSE

DECLARE SUB positive()

CLS

CALL positive

END

SUB positive

INPUT "Enter any number=";n

r=SGN(n)

IF r= 0 THEN

PRINT "The number is Positive"

ELSE

 PRINT "The number is Negative"

ENDIF

END SUB

 

Note: IF… THEN….ELSE is used only when there are two alternatives.

 

2. WAP to test whether given number is completely divisible by 13 or not.

DECLARE SUB test ()

CLS

CALL test

END

SUB test

INPUT "enter any number="; n

r = n MOD 13

IF r = 0 THEN

PRINT "completely divisible by 13"

ELSE

PRINT "not completely divisible by 13"

END IF

END SUB

 

3. WAP to test whether entered no is  odd or even.

DECLARE SUB eo(a)

CLS

INPUT "Enter any no:-";a

CALL eo(a)

END

SUB eo(a)

r=a MOD 2

IF r= 0 THEN

PRINT "EVEN"

ELSE

PRINT "ODD"

ENDIF

END SUB

 

 

4. WAP to input any number and display whether that number is prefect  square or not.

DECLARE SUB square()

CLS

CALL squar

END

SUB square(n)

INPUT "Enter any number=";n

r=SQR(n)

IF r=INT(r) THEN

PRINT n; "is prefect square"

ELSE

PRINT n; "is not prefect square"

ENDIF

END SUB

 

5. WAP to input cost price and selling price and determine whether there is profit or loss.

DECLARE SUB pal ()

CLS

CALL pal

END

 SUB pal

INPUT "Enter cost price"; cp

INPUT "Enter selling price"; sp

IF sp > cp THEN

PRINT "Profit"

ELSE

PRINT "Loss"

END IF

END SUB

 

 

6. WAP to input three different numbers in the main module then find the greatest number.

Using IF….. THEN

CLS

INPUT "Enter any three numbers"; a, b, c

IF a > b AND a > c THEN g = a

IF b > a AND b > c THEN g = b

IF c > a AND c > b THEN g = c

PRINT "greatest no="; g

END

 

Using IF.......ELSEIF……ELSE

DECLARE SUB great (a,b,c)

CLS

INPUT "Enter any three no"; a, b, c

CALL great (a,b,c)

END

SUB great(a,b,c)

IF a > b AND a > c THEN

g = a

ELSEIF b > c THEN

g = b

ELSE

g = c

END IF

PRINT "Greatest no"; g

END SUB

 

 You can also Read:

Display addition, subtraction, multiplication, and average of input two numbers


7. WAP to check whether input year is leap year or not.

DECLARE SUB leap ()

CLS

CALL leap

END

 

SUB leap

INPUT "Enter year"; year

IF (year MOD 4 = 0) AND (year MOD 100 <> 0) THEN

PRINT "Leap year"

ELSE

PRINT "not leap year"

END IF

END SUB

8. WAP to display the middle number among input three numbers

DECLARE SUB middle ()

CLS

CALL middle

END

 

SUB middle

INPUT "enter three numbers"; x, y, z

IF (x > y AND x < z) OR (x < y AND x >z ) THEN

PRINT x; "is middle no"

ELSEIF (y > x AND y < z) OR (y < x AND y > z) THEN

PRINT y; "is middle number"

ELSE

PRINT z; "middle number"

END IF

END SUB

 

9. WAP to check for voting eligibility. (18 is the minimum age for voting eligibility)

DECLARE SUB vote ()

CLS

CALL vote

END

 SUB vote

INPUT "Enter your age"; a

IF a >= 18 THEN

PRINT "Eligible for voting"

ELSE

PRINT "Not Eligible for voting"

END IF

END SUB

10. WAP to check whether input number is positive, negative or neutral.

DECLARE SUB num()

CLS

CALL num

END

SUB num()

INPUT “Enter any number”;n

IF n>0 THEN PRINT “Positive number”

IF n<0 THEN PRINT “Negative number”

IF n=0 THEN PRINT “Neutral number”

END SUB

 

 You may also Read:

Conditional statements using SUB....END SUB


11. WAP input percentage from the keyboard and display the remarks on the basis of the following conditions.

Percentage

Remarks

Per >= 90

Per <90 and >=80

Per<80 and >=60

Per <60 and >=50

Per <50

Excellent

Very good

Good

Work hard

Poor

 

Using SELECT CASE

DECLARE SUB remarks(p)

CLS

INPUT "Enter Percentage:";p

CALL remarks(p)

END

SUB remarks(p)

SELECT CASE p

CASE IS >=90

re$="Excellent"

CASE IS >=80

re$="Very Good"

CASE IS >=60

re$="work hard"

CASE is <50

re$="Poor"

END SELECT

PRINT "Remarks=";re$

END SUB

Using If….elseif

DECLARE SUB remarks(p)

CLS

INPUT "Enter Percentage:";p

CALL remarks(p)

END

SUB remarks(p)

IF p>=90 THEN

r$="Excellent"

ELSEIF p>=80 THEN

r$="Very good"

ELSEIF p>=60 THEN

r$="Good"

ELSEIF p>=50 THEN

r$="Work hard"

ELSE

r$="Poor"

ENDIF

PRINT "Remarks:-";r$

END SUB

 

12. To find the commission earned by the salesman from the following data:

Value of sales

Commission

Up to Rs 10000

8%

Rs. 10001 to 20000

10%

Above Rs 20000

12%

CLS

INPUT "Enter sales amount="; s

IF s <= 10000 THEN c = 8

IF s > 10000 AND s <= 20000 THEN c = 10

IF s > 20000 THEN c = 12

ca = (s * c) / 100

PRINT "Commission amount="; ca

END

 

13. WAP to input number of passengers and destination place.  Calculate the total fare using the following conditions:

Destination

Rate

Attariya

Chaumala

Sukkad

Lamki

Tikapur

50

100

150

200

250

DECLARE SUB fare(d$,p)

CLS

INPUT "Enter Destination place:-";d$

INPUT "Enter Number of passengers:-";p

CALL fare(d$,p)

END

SUB fare(d$,p)

SELECT CASE d$

CASE "Attariya"

t=p*50

CASE "Chaumala"

t=p*100

CASE "Sukkad"

t=p*150

CASE "Lamki"

t=p*200

CASE "Tikapur"

t=p*259

END SELECT

CLS

PRINT TAB(35); "Faster Traveler, Dhangadhi"

PRINT: PRINT

PRINT TAB(30); "Destination Place:-";d$

PRINT: PRINT

PRINT TAB(30); "Total number of passengers:-";p

PRINT: PRINT

PRINT TAB(30); "Total fare :-";t

END SUB

 

14. WAP to input mark and display the grade according to the condition

Marks

Grade

90 to 100

A+

80 to 89.9

A

70 to 79.9

B+

60 to 69.9

B

50 to 59.9

C+

40 to 49.9

C

35 to 39.9

D

Below 35

NG (Non Grade)

 DECLARE SUB grade ()

CLS

CALL grade

END

 

SUB grade

INPUT "Enter marks"; m

SELECT CASE m

CASE 90 TO 100

PRINT "Grade: A+"

CASE 80 TO 89.9

PRINT "Grade: A"

CASE 70 TO 79.9

PRINT "Grade: B+"

CASE 60 TO 69.9

PRINT "Grade: B"

CASE 50 TO 59.9

PRINT "Grade: C+"

CASE 40 TO 49.9

PRINT "Grade: C"

CASE 35 TO 39.9

PRINT " Grade: D"

CASE ELSE

PRINT "Non Grade (NG)"

END SELECT

END SUB

Multiple sub programs using menu base.

15. WAP to input any two numbers and display their addition, subtraction, multiplication, division and reminder with the help of many structure.

DECLARE SUB add (a, b)

DECLARE SUB dif (a, b)

DECLARE SUB mul (a, b)

DECLARE SUB div (a, b)

DECLARE SUB remi (a, b)

CLS

INPUT "enter any two numbers="; a, b

PRINT "******Main menu***********"

PRINT "---------------------------"

PRINT "1 .Addition (+)"

PRINT "2. Subtraction (-)"

PRINT "3. Multiplication (X)"

PRINT "4. Division (/)"

PRINT "5. Reminder"

INPUT "Enter your choice (1-5)"; ch

SELECT CASE ch

CASE 1

CALL add(a, b)

CASE 2

CALL dif(a, b)

CASE 3

CALL mul(a, b)

CASE 4

CALL div(a, b)

CASE 5

CALL remi(a, b)

CASE ELSE

PRINT "Wrong choice"

END SELECT

END

 

SUB add (a, b)

s = a + b

PRINT "Addition of two number"; s

END SUB

 

SUB dif (a, b)

su = a - b

PRINT "Differene of two number="; su

END SUB

 

SUB div (a, b)

d = a / b

PRINT "Division="; d

END SUB

 

SUB mul (a, b)

m = a * b

PRINT "Multiplication of two numbers="; m

END SUB

 

SUB remi (a, b)

r = a MOD b

PRINT "Reminder ="; r

END SUB

Note: In above example, five different sub programs “add”, “dif”, “mul”, “div” and “remi” defined. The user can call any sub program with the help of menu structure by selecting (1 or 2 or 3 or 4 or 5) according to his/her choice.


You may Read

C program to read name and marks of 5 different subjects calculate total percentage and display them in tabular format using structure

 

 

 

 

 

 

 

 

 

 

 

1 comment:

Post Top Ad

Your Ad Spot

Pages