PF Lab Report 5
PF Lab Report 5
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.
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
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.
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.
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.
if(condition_1) {
statement(s);
else if(condition_2) {
* condition_2 is met
*/
statement(s);
}
else if(condition_3) {
*/
statement(s);
else {
*/
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.
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>
int main ()
{
int a,b,c ;
cin>>a;
cin>>b;
cin>>c;
{cout<<"Smallest Number="<<a<<endl;}
{cout<<"Smallest Number="<<b<<endl;}
{cout<<"Smallest Number="<<c<<endl;}
{cout<<"Largest number="<<a<<endl;}
{cout<<"Largest number="<<b<<endl;}
{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>
int main ()
int first_side,second_side,third_side;
cin>>first_side;
cin>>second_side;
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)
else
return 0;
Output:
Exercise-3
Write a program in C++ using if else operator to find the grade of a student.
else Grade F
Coding of exercise-3
#include<iostream>
int main ()
int marks;
cout<<" Marks="<<endl;
cin>>marks;
if (marks>=90){
else if (marks>=75)
else if (marks>=60)
else if (marks>=45)
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
Case 2:b
Case 3:b
Coding of exercise:
#include<iostream>
int main ()
{
int a,b,c ;
cin>>a;
cin>>b;
cin>>c;
int descriminent;
descriminent=b*b-4*a*c;
if(descriminent==0)
if(descriminent<0)
if(descriminent>0)
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.
(3) Plus Rs. 0.40 per call for any call beyond 200 calls.
Coding of exercise:
#include<iostream>
int main()
int calls;
float bill;
cin>>calls;
if(calls<=100)
bill=200;
calls=calls-100;
bill=200+(0.60*calls);
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);
}
return 0;
Output: