Upcoming SlideShare
×

# Labsheet 4

839 views

Published on

Published in: Technology, Business
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

### Labsheet 4

1. 1. FP 201- PROGRAMMING FUNDAMENTALSLAB 4: Selection Control StructuresLearning OutcomesThis Lab sheet encompasses activities 4A, 4B, 4C, 4D, 4E and 4FBy the end of this lab, students should be able to :• Describe the structure of if and switch statements• Explain the need of break statements• Write program using selection control structuresActivity 4AActivity Outcome: Write and compile a program using C++.Program to illustrate the execution of a simple if statement.Procedure:Step 1: Type the programs given below#include <iostream>using namespace std;int main(){ int x =7; int y = 5; if (x>y) cout<<”x is greater than y”;}Step 2: Compile the program.Step 3: Write the output.Step 4: Save the program.Activity 4BActivity Outcome: Write and compile a program using C++.Program to illustrate the execution of a simple if statement.Procedure:Step 1: Type the programs given below/* Program to check whether the given number is less than 50 */#include <iostream>using namespace std;
2. 2. FP 201- PROGRAMMING FUNDAMENTALS main(){ int num; cout<<”Enter your lucky number: ”; cin >> num; if (num < 50) cout << "Number is less than 50" << endl; return 0;}Step 2: Compile the program.Step 3: Write the output.Step 4: Save the program.Activity 4CActivity Outcome: Write and compile a program using C++.Program to illustrate the execution of a simple if statement based on two conditions.Procedure:Step 1: Type the programs given below/* Program to check whether the marks is in between 0 and 50 */#include <iostream>using namespace std;void main(){ int marks; cout << "Enter mark = "; cin >> marks; if ((marks > 0) && (marks < 50)) cout << "failed" << endl;}Step 2: Compile the program.Step 3: Write the output.Step 4: Save the program.Activity 4DActivity Outcome: Write and compile a program using C++.Program to illustrate the execution of the if-else statement.Procedure:
3. 3. FP 201- PROGRAMMING FUNDAMENTALSStep 1: Type the programs given below#include <iostream>using namespace std;void main(){ int num; cout << "Enter 1 number = "; cin >> num; if (num < 0) cout << "Negative number" << endl; else if (num > 0) cout << "Positive number" << endl; else cout << "Zero number" << endl;}Step 2: Compile the program.Step 3: Write the output.Step 4: Save the program.Activity 4EActivity Outcome: Write and compile a program using C++.Program to illustrate the switch case execution.Procedure:Step 1: Type the programs given below#include <iostream>using namespace std;void main(){ int thisMonth; cout << "Enter 1 number between 1 to 6 : "; cin >> thisMonth; switch (thisMonth) { case 1: cout << "January" << endl; break; case 2: cout << "February" << endl; break; case 3: cout << "March" << endl; break; case 4: cout << "April" << endl; break; case 5: cout << "May" << endl; break; case 6: cout << "June" << endl;
4. 4. FP 201- PROGRAMMING FUNDAMENTALS break; default: cout << "Wrong input" << endl; }}Step 2: Compile the program.Step 3: Write the output.Step 4: Save the program.Activity 4FActivity Outcome: Write and compile a program using C++.The following program illustrate the syntax of switch..case statements.Procedure:Step 1: Type the programs given below#include <iostream>using namespace std;void main(){ int option; cout << “Enter your option 1, 2, 3 : “; cin >> option; switch (option) { case 1 : cout << “You entered option 1” << endl; break; case 2 : cout << “You entered option 2” << endl; break; case 3 : cout << “You entered option 3” << endl; break; default : cout << “You entered invalid value” << endl; }}Step 2: Compile the program.Step 3: Write the output.Step 4: Save the program.
5. 5. FP 201- PROGRAMMING FUNDAMENTALSLab Exercise 4Ex. 4.1Jusco Supermarket is now offering some discount on selected items for Carnival Megasale. Help the supermarket cashiers to calculate the net price of the selected product’sbelow. a. Write a programme based on the discount shown below using switch …case statement. Position Discount 1. Food Item 5% 2. Health Item 6% 3. Electric Item 9% 4. Cloth Item 4% (5 marks) b. Change above program from switch case statement to if … else statement. (5 marks)Ex. 4.2Accepts four integers from the user and finds the lowest among the three. Write downthe pseudocode and the write the program. (5 marks)