Intensive Programming Lab 4
Intensive Programming Lab 4
Learning Objectives:
The objective of this exercise is to get you to write, compile and run a number of simple programs
in C which make use of basic decision constructs/selection statements.
Introduction:
Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.
Following is the general form of a typical decision making structure found in mos t of the
programming languages:
if statement:
An ‘if’ statement consists of a Boolean expression followed by one or more statements.
if...else statement:
An ‘if’ statement can be followed by an optional ‘else’ statement, which executes when
the Boolean expression is false.
nested if statements:
You can use one ‘if’ or ‘else if’ statement inside another ‘if’ or ‘else if’ statement(s).
Syntax:
if(boolean_expression) {
// statement(s) will execute if the boolean expression is true
}
Example 1:
Write a C++ program that input two numbers from user and tells the relationship they satisfy.
Code:
#include <iostream>
using namespace std;
cout<<"Enter two integers, and I will tell you the relationships they satisfy:\n";
if (num1!=num2) {
cout<<num1<< "is not equal to "<<num2<<endl;
} /* end if */
if (num1<=num2 ) {
cout<<num1<<" is less than "<<num2<<endl;
} /* end if */
if (num1>=num2) {
cout<<num1<<" is greater than "<<num2<<endl;
} /* end if */
if ( num1==num2) {
cout<<num1<<" is equal to "<<num2<<endl;
} /* end if */
Task 1: Write a C++ program to enter temperature in Fahrenheit and convert to Celsius. If value
of Celsius temperature is 25 print ‘Normal’ , if greater than 25 the print ‘High’ and if less than
25 print ‘Low’.