If Else
If Else
Structure II Sessional
If-Else
if-else form:
if (boolean_expression1)
statement1;
else
statement2;
if-else if form:
if (boolean_expression1)
statement1;
else if (boolean_expression2)
statement2;
.
.
else if (boolean_expressionn)
statementn;
else
statementn+1;
Example 01
Write a program where the user provides the radius as input, and the program outputs the
area of a circle
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double radius;
cout << "Please type the radius: ";
cin >> radius;
if (radius < 0.0)
cout << "A negative radius is invalid" << endl;
else
cout << "The area of this circle is " << 3.1416 * pow(radius,2) << endl;
return 0;
}
Example-02
Write a program where the user provides the temperature as input, and the program
outputs the desired temperature in Fahrenheit or Celsius.
#include <iostream> // Set output formats
using namespace std; if (tempType == 'f')
// A temperature conversion program {
int main() celsius = (5.0 / 9.0) * (temp - 32.0);
{ cout << "\nThe equivalent Celsius temperature is
char tempType; " << celsius << endl;
double temp, fahren, celsius; }
cout << "Enter the temperature to be else
converted: "; {
cin >> temp; fahren = (9.0 / 5.0) * temp + 32.0;
cout << "Enter an f if the temperature is in cout << "\nThe equivalent Fahrenheit temperature
Fahrenheit"; is " << fahren << endl;
cout << "\n or a c if the temperature is in }
Celsius: "; return 0;
cin >> tempType; }
Nested Statement
Example-03
Write a program where the user provides the temperature as input, and the program
outputs the desired temperature in Fahrenheit or Celsius using Nested If Statements
Switch Statement
Example-04 and 05
Write a program where the user provides the temperature as input, and the program
outputs the desired temperature in Fahrenheit or Celsius using Switch Statement
Write a C++ program to find if an integer is positive, negative or zero using Switch
Statement
Project Example
Three of the most commonly used beams in structural engineering are the I-beam,
rectangular beam, and cylindrical beam. In determining the stress a given weight places on
a symmetrical beam, an important design parameter is the beam’s rectangular moment of
inertia, I, which is typically given in units of in4. The computation of I depends on the
beam’s geometry, and for the three beam types shown, the values of I are calculated as
follows:
BH 3 bh3
For an I-beam: I
12
bh3
For a rectangular beam: I
12
r4
For a cylindrical beam: I
4
Project Example
Using this information, design, write, compile, and run a C++ program that prompts the
user for the type of beam and the necessary data (based on the input), and then computes
and displays the beam’s rectangular moment of inertia.
QUIZ 01
A fluid particle flowing through a pipe can flow in a smooth, constant manner, called
laminar flow; in a chaotic manner, called turbulent flow; or in an intermediate transitional
stage between smooth and turbulent flow. As a practical design parameter, the Reynolds
number can be used to determine the type of flow. For a Reynolds number below 2000, the
flow is laminar, and for a Reynolds number above 4000, the flow is turbulent. For a
Reynolds number between 2000 and 4000, the flow is in transition from laminar to
turbulent.
Using this information, write and run a C++ program that accepts a Reynolds number as
user input; determines whether the flow is laminar, turbulent, or in transition; and displays
a message indicating the type of flow based on the input Reynolds number.
THANK
YOU