Here are some lab work questions related to QBasic using control flow structures such as IF
, IF...ELSE
, IF...ELSEIF...ELSE
, and SELECT CASE
statements, along with answers and explanations:
Lab Work Questions and Answers in QBasic
Question 1: Using the IF
statement
Write a program to check if a number entered by the user is positive, negative, or zero using the IF
statement.
Answer:
CLS
INPUT "Enter a number: ", N
IF N > 0 THEN
PRINT "The number is positive."
ELSEIF N < 0 THEN
PRINT "The number is negative."
ELSE
PRINT "The number is zero."
END IF
END
Explanation:
- This program uses a simple
IF
statement to check if a number is greater than zero (positive), less than zero (negative), or equal to zero. Based on the condition, it prints the respective message.
Question 2: Using the IF...ELSE
statement
Write a program to check whether a student has passed or failed based on their marks. Assume the passing marks are 50. If the marks are greater than or equal to 50, the student passes; otherwise, they fail.
Answer:
CLS
INPUT "Enter the student's marks: ", marks
IF marks >= 50 THEN
PRINT "The student has passed."
ELSE
PRINT "The student has failed."
END IF
END
Explanation:
- The
IF...ELSE
statement checks if the student's marks are greater than or equal to 50. If true, it prints "The student has passed"; otherwise, it prints "The student has failed."
Question 3: Using the IF...ELSEIF...ELSE
statement
Write a program that checks the grade of a student based on their marks:
- 80 and above: "A"
- 60 to 79: "B"
- 40 to 59: "C"
- Below 40: "Fail"
Answer:
CLS
INPUT "Enter the student's marks: ", marks
IF marks >= 80 THEN
PRINT "Grade: A"
ELSEIF marks >= 60 THEN
PRINT "Grade: B"
ELSEIF marks >= 40 THEN
PRINT "Grade: C"
ELSE
PRINT "Fail"
END IF
END
Explanation:
- The
IF...ELSEIF...ELSE
statement is used to check multiple conditions and print the grade according to the marks entered by the student.
Question 4: Using the SELECT CASE
statement
Write a program that takes an integer input (1 to 7) from the user and prints the corresponding day of the week (1 = Sunday, 2 = Monday, ..., 7 = Saturday).
Answer:
CLS
INPUT "Enter a number (1-7): ", day
SELECT CASE day
CASE 1
PRINT "Sunday"
CASE 2
PRINT "Monday"
CASE 3
PRINT "Tuesday"
CASE 4
PRINT "Wednesday"
CASE 5
PRINT "Thursday"
CASE 6
PRINT "Friday"
CASE 7
PRINT "Saturday"
CASE ELSE
PRINT "Invalid input! Please enter a number between 1 and 7."
END SELECT
END
Explanation:
- The
SELECT CASE
statement is used here to check the number entered by the user and display the corresponding day of the week. If the number is not between 1 and 7, it prints "Invalid input."
Question 5: Using the IF
statement with logical operators
Write a program that checks if a given number is within a specific range (for example, between 10 and 20 inclusive).
Answer:
CLS
INPUT "Enter a number: ", N
IF N >= 10 AND N <= 20 THEN
PRINT "The number is between 10 and 20."
ELSE
PRINT "The number is outside the range."
END IF
END
Explanation:
- The
IF
statement uses the logical operatorAND
to check if the number is between 10 and 20, inclusive. If true, it prints the corresponding message.
Question 6: Using the SELECT CASE
statement for multiple conditions
Write a program to enter a month number (1 to 12) and print the number of days in that month. Assume February has 28 days (not accounting for leap years).
Answer:
CLS
INPUT "Enter the month number (1-12): ", month
SELECT CASE month
CASE 1, 3, 5, 7, 8, 10, 12
PRINT "31 days"
CASE 4, 6, 9, 11
PRINT "30 days"
CASE 2
PRINT "28 days"
CASE ELSE
PRINT "Invalid month number!"
END SELECT
END
Explanation:
- The
SELECT CASE
statement is used to check which month is entered and print the number of days in that month. It covers months with 31 days, 30 days, and February (28 days).
Summary of Control Flow Statements Used
-
IF
Statement:- Used to check a single condition and execute a block of code if the condition is true.
-
IF...ELSE
Statement:- Used to check a condition and execute one block of code if true, and another block if false.
-
IF...ELSEIF...ELSE
Statement:- Used to check multiple conditions in sequence and execute the corresponding block of code for the first true condition.
-
SELECT CASE
Statement:- Used to check a variable against several possible values and execute the corresponding block of code based on the value.
These lab work questions help practice decision-making structures in QBasic, allowing for a deeper understanding of control flow in programming.
No comments:
Post a Comment