Tasks submission is available below the tasks please scroll down. Tasks submission dead line is 11/27/2020 at 11:59 pm
Objective of this lab
Manual is to get hands on experience of switch statements using C++ on Visual
Studio 2012-onward.
Switch statement allows a variable to be tested for equality
against a list of values. Each value is called a case, and the variable being
switched on is checked for each case. For example, if we have integer variable
in the switch then it will check each case for equality if it find the relevant
case it will only execute that case otherwise default will work.
Syntax of Switch Statements:
switch(expression)
{
case constant-expression :
statement(s);
break; //optional
//you
can have multiple cases as well
// you can have any number of case
statements.
default : //Optional
statement(s);
}
Code Example of Switch Statements:
Two numbers addition and Subtraction using Switches
#include<iostream>
using namespace std;
int main()
{
int
number=0,number1=0;
cout<<"enter number 1=";
cin>>number;
cout<<"enter number 2=";
cin>>number1;
char
choice=NULL; //NULL '/0'
cout<<"enter operation
+/-=";
cin>>choice;
switch(choice)
{
case
'+':
cout<<"the sum of two
numbers is="<<number+number1<<endl;
break;
case
'-':
cout<<"the minus of two
numbers is="<<number-number1<<endl;
break;
default:
cout<<"you have entered value other than 1 & 2"<<endl;
}
system("pause");
return 0;
}
Lab Tasks:
- Create a project and compile the given code example above and see the output of the code.
- Write a program in C++ which perform multiplication *, addition +, minus -, division / on two number. Your program should take two numbers from user and operation such as +, -, *, / and display results on the screen.
- Write a program in C++ which prints on * if user enter 1, print ** if user enters 2 and print *** if user enter 3 and print “you have entered wrong value” otherwise.
Post a Comment