Assignment No 6
Assignment No 6
Assignment No 6
Lab Report 06
1st SEMESTER
If the conditional expression is true, the statements between the keywords Then and
Else will be executed (and the statements between the keywords Else and End If will
be bypassed). If the conditional expression is false, the statements between the
keywords Else and End If will be executed (and the statements between the
keywords Then and Else will be bypassed). In any case, program control will
resume with the statement following End If
A Boolean variable, which can hold the value True or False, can be used anywhere a
relational expression can be used
2. Conditional Operator:
It takes 3 arguments
Syntax for using conditional operator is as follows
3. Switch:
It is alternate to if – else
Switch expression is evaluated first
Value of expression determines which corresponding action is to be
taken
Expression is sometimes called as a selector
One or more statement follow a case
Braces are not needed to convert multiple statements into a
acompoud single statement
Break statement may or maynot appear after every statement
Switch, case, deafult and break are reserved words
Syntax is given as;
switch(expression)
{
case value1:
statement1
break;
case value2:
statement2
break;
.
.
.
case valuen:
statementn
break;
deafault:
statement
}
Lab Work:
Program No 1:
#include<iostream>
using namespace std;
int main()
{
int a,b;
repeat:
cout<<"Put Numbers"<<endl;
cin>>a;
cin>>b;
switch(a-b)
{
case 1:
cout<<"Difference is 1";
cout<<"\nThank you"<<endl;
break;
case 2:
cout<<"Difference is 2";
cout<<"\nThank you"<<endl;
break;
default:
cout<<a-b;
cout<<"\nThank you"<<endl;
break;
}
goto repeat;
}
Program No 2:
#include<iostream>
using namespace std;
int main()
{
cout<<"Your university\n1)NUST\n2)UET\n3)others"<<endl;
int a;
cin>>a;
switch(a)
{
case 1:
cout<<"How unlucky";
cout<<"\nThank you";
break;
case 2:
cout<<"UET>NUST";
cout<<"\nThank you";
break;
default:
cout<<"Your degree is useless";
cout<<"\nThank you";
break;
}
Program No 3:
#include<iostream>
using namespace std;
int main()
{
int a,b;
char s;
xy:
cin>>a>>s>>b;
switch(s)
{
case '+':
cout<<"Answer="<<a+b<<endl;
break;
case '-':
cout<<"Answer="<<a-b<<endl;
break;
case 'x':
cout<<"Answer="<<a*b<<endl;
break;
case '/':
cout<<"Answer="<<a/b<<endl;
break;
default:
cout<<"invalid operation";
cout<<"Please re-enter the expression";
break;
}
system("Pause");
system("CLS");
goto xy;
}
Figure 5 1st output of program no 3
char toupper()
{
char a;
cin>>a;
if(a>=97&&a<=122)
return (a-32);
else
return a;
}
#include<iostream>
using namespace std;
int main()
{
repeat:
cout<<"Your rank";
int rank ;
cin>>rank;
switch (rank)
{
case 1:
case 2:
cout<<"Lower Divison"<<endl;
break;
case 3:
case 4:
cout<<"Upper Divison"<<endl;
break;
case 5:
cout<<"Graduate Student"<<endl;
break;
default:
cout<<"Invalid rank"<<endl;
}
goto repeat
}
Program No 2 Part 1:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
double pakistani_rupees, equivalentCurr;
char currencyCode;
const double ECONVERTION(0.0046), PCONVERTION(0.091),
SCONVERTION(0.0041);
return (EXIT_SUCCESS);
}
Figure 12 Pakistani Rupees to Euros
{
double indian_rupees, equivalentCurr;
char currencyCode;
const double ECONVERTION(0.012), PCONVERTION(0.24),
SCONVERTION(0.011);
cout << "enter currency code:\n" << "E => Euros\nP => Mexican Pesos\nS =>
British Pounds Sterling\n" ;
cin >> currencyCode;
switch(toupper(currencyCode))
{
case 'E':
cout << "converting indian rupees to euros..\n" ;
equivalentCurr = indian_rupees*ECONVERTION;
break;
case 'P':
cout << "converting indian rupees to pesos..\n" ;
equivalentCurr = indian_rupees*PCONVERTION;
break;
case 'S':
cout << "converting indian rupees to pounds sterling..\n" ;
equivalentCurr = indian_rupees*SCONVERTION;
break;
default:
cout << currencyCode << "not supported at this time\n" ;
equivalentCurr = indian_rupees;
}
cout << "Equivalent amount: "<< equivalentCurr << endl;
return (EXIT_SUCCESS);
}
Program No 2 Part 3:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
double australlian_dollar, equivalentCurr;
char currencyCode;
cout << "enter currency code:\n" << "E => Euros\nP => Mexican Pesos\nS =>
British Pounds Sterling\n" ;
cin >> currencyCode;
switch(toupper(currencyCode))
{
case 'E':
cout << "converting australlian dollar to euros..\n" ;
equivalentCurr = australlian_dollar*ECONVERTION;
break;
case 'P':
cout << "converting australlian dollar to pesos..\n" ;
equivalentCurr = australlian_dollar*PCONVERTION;
break;
case 'S':
cout << "converting australlian dollar to pounds sterling..\n" ;
equivalentCurr = australlian_dollar*SCONVERTION;
break;
default:
cout << currencyCode << "not supported at this time\n" ;
equivalentCurr = australlian_dollar;
}
cout << "Equivalent amount: "<< equivalentCurr << endl;
return (EXIT_SUCCESS);