0% found this document useful (0 votes)
56 views12 pages

PF Lab Report 5

The document is a lab report that discusses control structures in C++ programming, specifically if-else statements and operators. It provides examples of if, nested if, if-else, and if-else-if statements. It also describes 3 exercises completed in the lab involving if-else operators to find the largest/smallest number, determine if a triangle is a right triangle, and assign a grade based on marks. The conclusion states that control structures specify program flow and make code more clear and understandable.

Uploaded by

huzaifa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views12 pages

PF Lab Report 5

The document is a lab report that discusses control structures in C++ programming, specifically if-else statements and operators. It provides examples of if, nested if, if-else, and if-else-if statements. It also describes 3 exercises completed in the lab involving if-else operators to find the largest/smallest number, determine if a triangle is a right triangle, and assign a grade based on marks. The conclusion states that control structures specify program flow and make code more clear and understandable.

Uploaded by

huzaifa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 12

LAB REPORT

Name:
Roll No:
Class:
Lab name:Programming Fundamental
Experiment No.5
Control Structures: If-else statement &Operators
Introduction:
Control Structure:
Control Structures can be considered as the building blocks of computer programs. They are commands
that enable a program to “take decisions”, following one path or another. A program is usually not
limited to a linear sequence of instructions since during its process it may bifurcate, repeat code or
bypass sections.

If-else statement & operators:


Sometimes we need to execute a block of statements only when a particular condition is met or
not met. This is called decision making, as we are executing a certain code after making a
decision in the program logic. For decision making in C++, we have four types of control
statements (or control structures), which are as follows:

a) if statement

b) nested if statement

c) if-else statement
d) if-else-if statement

Objective:
(1) To be able to evaluate relational expressions

(2) To know the precedence rules of logical operators

Procedure:
(1)If statement in C++
If statement consists a condition, followed by statement or a set of statements as shown below:

if(condition){

Statement(s);

The statements inside if parenthesis (usually referred as if body) gets executed only when the
given condition is true. If the condition is false then the statements inside if body are completely
ignored.

(2)Nested if statement in C++


When there is an if statement inside another if statement then it is called the nested if
statement.

The structure of nested if looks like this:

if(condition_1) {

Statement1(s);

if(condition_2) {

Statement2(s);

Statement1 would execute if the condition_1 is true. Statement2 would only execute if both the
conditions( condition_1 and condition_2) are true.
(3)If else statement in C++
Sometimes you have a condition and you want to execute a block of code if condition is true
and execute another piece of code if the same condition is false. This can be achieved in C++
using if-else statement.

This is how an if-else statement looks:

if(condition) {

Statement(s);

else {

Statement(s);

The statements inside “if” would execute if the condition is true, and the statements inside
“else” would execute if the condition is false.

(4)f-else-if Statement in C++


if-else-if statement is used when we need to check multiple conditions. In this control structure we have
only one “if” and one “else”, however we can have multiple “else if” blocks. This is how it looks:

if(condition_1) {

/*if condition_1 is true execute this*/

statement(s);

else if(condition_2) {

/* execute this if condition_1 is not met and

* condition_2 is met

*/

statement(s);

}
else if(condition_3) {

/* execute this if condition_1 & condition_2 are

* not met and condition_3 is met

*/

statement(s);

else {

/* if none of the condition is true

* then these statements gets executed

*/

statement(s);

Note: The most important point to note here is that in if-else-if, as soon as the condition is met, the
corresponding set of statements get executed, rest gets ignored. If none of the condition is met then the
statements inside “else” gets executed.

Exercises attepmted in lab

Exercise-1
Write a program in C++ that take input of three integer’s numbers from the user. Find the largestand
smallest among three of them. The program should takes three numbers as input and returnthe largest
and smallest number.

Coding of exercise-1
#include<iostream>

using namespace std;

int main ()

{
int a,b,c ;

cout<<"Enter value of a="<<endl;

cin>>a;

cout<<"Enter value of b="<<endl;

cin>>b;

cout<<"Enter value of c="<<endl;

cin>>c;

if(a<b && a<c)

{cout<<"Smallest Number="<<a<<endl;}

else if (b<a && b<c)

{cout<<"Smallest Number="<<b<<endl;}

else if(c<a && c<b)

{cout<<"Smallest Number="<<c<<endl;}

if(a>b && a>c)

{cout<<"Largest number="<<a<<endl;}

else if(b>a && b>c)

{cout<<"Largest number="<<b<<endl;}

else if(c>a && c>b)

{cout<<"Largest number="<<c<<endl;}

return 0;

Output:
Exercise-2
In a right triangle, the square of the length of one side is equal to the sum of the squares of
thelengths of the other two sides.Write a program that prompts the user to enter the lengths of
three sides of a triangle and thenoutputs a message indicating whether the triangle is a right
triangle.

Coding of exercise-2
#include<iostream>

using namespace std;

int main ()

int first_side,second_side,third_side;

cout<<"length of first side"<<endl;

cin>>first_side;

cout<<"length of second side"<<endl;

cin>>second_side;

cout<<"length of third side"<<endl;

cin>>third_side;

int square_of_first_side;

square_of_first_side=first_side*first_side;

int square_of_second_side;

square_of_second_side=second_side*second_side;
int square_of_third_side;

square_of_third_side=third_side*third_side;

if (square_of_first_side==square_of_second_side+square_of_third_side)

{cout<<"It is a right triangle"<<endl;}

else

{cout<<"it is not a right triangle"<<endl;}

return 0;

Output:

Exercise-3
Write a program in C++ using if else operator to find the grade of a student.

The details are as follow

marks >= 90 Grade A

marks >= 75 Grade B

marks >= 60 Grade C

marks >= 45 Grade D

else Grade F

Coding of exercise-3
#include<iostream>

using namespace std;

int main ()

int marks;

cout<<" Marks="<<endl;

cin>>marks;

if (marks>=90){

cout<<" Grade A"<<endl;}

else if (marks>=75)

{cout<<" Grade B"<<endl;}

else if (marks>=60)

{cout<<" Grade C"<<endl;}

else if (marks>=45)

{cout<<" Grade E"<<endl;}

else

{cout<<" Fail"<<endl;}

return 0;

Output:

Issues:
In exercise 1 i was putting 'or' signs instead of 'and' so my answer was wrong.I rechecked my
code and exluded my problem.

Application:
Control Systems are used in domestic applications, general industry, military and virtually every
modern vehicle in the world. Control Systems are very common in SCADA and Industrial
Automation systems. Control Systems are used in Industrial Automation to regulate how devices
operate in real time.

Conclusion:
Control Structures are just a way to specify flow of control in programs. Any algorithm or
program can be more clear and understood if they use self-contained modules called as logic or
control structures. It basically analyzes and chooses in which direction a program flows based
on certain parameters or conditions.

Post lab:
A. The roots of the quadratic equation ax2 + bx + c = 0, are given by the following

formula:

−b ± √b2 − 4ac/2a

Write a program that prompts the user to input the value of ‘a’ (the coefficient of x2), ‘b’

(the coefficient of x), and ‘c’ (the constant term) and outputs the type of roots of the

equation.

Case 1: b

2 − 4ac = 0 Single repeated roots

Case 2:b

2 − 4ac > 0 Two real roots

Case 3:b

2 − 4ac < 0 Two Complex roots

Coding of exercise:
#include<iostream>

using namespace std;

int main ()

{
int a,b,c ;

cout<<"Enter value of A="<<endl;

cin>>a;

cout<<"Enter value of B="<<endl;

cin>>b;

cout<<"Enter value of C="<<endl;

cin>>c;

int descriminent;

descriminent=b*b-4*a*c;

if(descriminent==0)

cout<<"Single repeated roots"<<endl;

if(descriminent<0)

cout<<"Two Complex roots"<<endl;

if(descriminent>0)

cout<<"Two real roots"<<endl;

return 0;

Output:

B. Write a program to calculate the monthly telephone bills as per the following rule:
Minimum Rs. 200 for upto 100 calls.

(1)Plus Rs. 0.60 per call for next 50 calls.

(2)Plus Rs. 0.50 per call for next 50 calls.

(3) Plus Rs. 0.40 per call for any call beyond 200 calls.

Coding of exercise:
#include<iostream>

using namespace std;

int main()

int calls;

float bill;

cout<<"Enter number of calls : ";

cin>>calls;

if(calls<=100)

bill=200;

else if (calls>100 && calls<=150)

calls=calls-100;

bill=200+(0.60*calls);

else if (calls>150 && calls<=200)

calls=calls-150;

bill=200+(0.60*50)+(0.50*calls);

else

calls=calls-200;

bill=200+(0.60*50)+(0.50*50)+(0.40*calls);
}

cout<<" Your bill is Rs"<<bill<<endl;

return 0;

Output:

You might also like