Lab Task 6 Operators , Switch Loops Question Paper
Lab Task 6 Operators , Switch Loops Question Paper
Lab 06
Introduction
In the previous lectures, we have studied the following topics:
1. switch Statement
Conditional Operator
Conditional operator is an alternative to simple if-else structure. Instead of writing four
lines, we can just write the same thing in one line.
Syntax:
condition ? true_case : false_case
condition represents any simple condition that is made up with the help of relational
operators that we studied in class. For example, a > b is a valid condition. The question
mark (?) that comes after the condition separates the statements from the condition while the
colon (:) that comes between true_case and false_case separates the statements that are
going to be executed based on the condition evaluation.
true_case triggers when the condition we have provided evaluates to true (it is just like the
statement that comes right after if condition).
false_case triggers when the condition we have provided evaluates to false (it is just like
the statement that comes in the else part of if-else structure).
Loops are one of the basic structures of a programming language. Loops are performed
to repeat a step or steps for a certain number of times. There are three kinds of loops:
1. for Loop
2. while Loop
3. do-while Loop
We can also use a while loop inside the while loop, the syntax of inside while loop will
remain the same as the outside loop.
Example 1 – Nested while Loop
Arrays (1-Dimensional)
For the basics of 1D arrays, declaration and assignment of values, Go to Lecture 15
that we have covered in the class.
Tasks
1. Write a program that ask the user to enter a number in the range of 1—10. Use a
switch statement and display corresponding Roman Number against each entered
decimal value. E.g.,
1 -> I
2 -> II
3 -> III
4 -> IV and so on
3. Repeat the previous task again but instead of using 1, 2, 3, 4 as case values try using +
-*/
a. The menu should be:
i. Press + for Addition
ii. Press - for Subtraction
iii. Press * for Multiplication
iv. Press / for Division
v. What is your choice?
b. Adjust the code accordingly.
4. Write a program to input the marks of infinite number of students with the help of loop
and show their grades for each subject. The program should not stop until the user says
so.
5. Write the program given as Example-1 and check the output. Modify the conditions one
by one for outer and inner loops and see what happens. Note the differences with you.
6. Write a program to input the marks of infinite number of students with the help of loop
and restrict the user to enter the marks greater than or equal to 0 and less than 100. If the
user doesn’t enter such value, prompt the user about wrong input and take the input again.
Then show their grades for each subject. The program should not stop until the user says
so.
8. Write a program that declare the integer array to store 10 numbers. Assign the values in
such a way that it stores the results of “Table of 2” in increasing order. E.g, 2, 4, 6, 8, …,
20. (Hint: we can use for loop to assign values in such order) Print it in the following
format:
2*1=2
2*2=4
.
.
.
9. Write a program to declare an array of any size. Assign all values as you like. Print the
sum of all elements in the array.
10. Write a program to declare an array of any size. Assign all values as you like. Print the
largest and smallest numbers present in the array.
----------------------------------------------------------------------------------------------------------------
Best of Luck 😊