Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment No 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Fundamentals of Programming

Lab Report 06

 Introduction to Control selection, Conditional


operator and Switch

1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section: B Group: _
SUBMITTED BY
Name CMS Objective Lab Lab
Theory
(1) Work(3) Task Total
(3)
(3)
Malik Haseeb Ullah 428302

School of Mechanical and Manufacturing


Engineering
Objectives:
 Control selection
 Conditional operator
 Switch
Theory:
1. Control selection:
The selection control structure allows one set of statements to be executed if a
condition is true and another set of actions to be executed if a condition is false.

If <conditional expression> Then


<one or more statements to be executed if condition is true>
Else
<one or more statements to be executed if condition is false>
End If

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

expression 1 ? expression 2 : expression 3

 If expression 1 is true then result of conditional expression is


expression 2
 If expression 1 is false then the result of conditional expression is
expression 3

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;
}

Figure 1 Output of program no 1

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;
}

Figure 2 1st output of program no 2


Figure 3 2nd output of program no 2

Figure 4 3rd output of program no 2

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

Figure 6 2nd output of program no 3

Figure 7 3rd output of program no 3

Figure 8 4th output of program no 3

Figure 9 5th output of program no 3


Program No 4:
#include<iostream>
using namespace std;
char toupper();
int main()
{
char b;
b=toupper();
cout<<b;
}

char toupper()
{
char a;
cin>>a;
if(a>=97&&a<=122)
return (a-32);
else
return a;
}

Figure 10 output of program no 4


Lab Task:
Program No 1:

#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
}

Figure 11 Output of Program No 1

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);

cout << "enter Pakistani rupees amount" << endl;


cin >> pakistani_rupees;
cout << "enter currency code:\n" << "E => Euros\nP => Mexican Pesos\nS =>
British Pounds Sterling\n" ;
cin >> currencyCode;
switch(toupper(currencyCode))
{
case 'E':
cout << "converting Pakistani rupees to euros..\n" ;
equivalentCurr = pakistani_rupees*ECONVERTION;
break;
case 'P':
cout << "converting Pakistani rupees to pesos..\n" ;
equivalentCurr = pakistani_rupees*PCONVERTION;
break;
case 'S':
cout << "converting Pakistani rupees to pounds sterling..\n" ;
equivalentCurr = pakistani_rupees*SCONVERTION;
break;
default:
cout << currencyCode << "not supported at this time\n" ;
equivalentCurr = pakistani_rupees;
}
cout << "Equivalent amount: "<< equivalentCurr << endl;

return (EXIT_SUCCESS);

}
Figure 12 Pakistani Rupees to Euros

Figure 13 Pakistani Rupees to pesos

Figure 14 Pakistani Rupees to Pounds sterling


Program No 2 part 2:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()

{
double indian_rupees, equivalentCurr;
char currencyCode;
const double ECONVERTION(0.012), PCONVERTION(0.24),
SCONVERTION(0.011);

cout << "enter indian rupees amount" << endl;

cin >> indian_rupees;

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);
}

Figure 15 indian rupee to euros


Figure 16 indian rupee to pesos

Figure 17 indian rupee to pound sterling

Program No 2 Part 3:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()

{
double australlian_dollar, equivalentCurr;
char currencyCode;

const double ECONVERTION(0.65), PCONVERTION(12.66),


SCONVERTION(0.57);

cout << "enter australlain dollars amount" << endl;

cin >> australlian_dollar;

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);

Figure 18 Australian dollar to euros

Figure 19 Australian dollar to pesos


Figure 20 Australian dollar to pounds sterling

Figure 21 Not Available

You might also like