2. • If Else Statement
• The if-else structure directs the computer to
select between two statements based on the
result of a comparison.
For example:
If (n<0)
cout”The number is negative”;
Else
Cout<<“The number is positive”;
4. • Use Of Else if:
• If there are more than 2 conditions then we can
use else if with the condition inside ().
• For Example:
if (grade>80 && grade<100)
Cout<<“Excellent”;
Else if ( grade>70 && grade<80)
Cout<<“Good”
Else
cout<<“Average”
5. • Another Problem
Write a Program by using if-else statement for
the following data:
Specification Status Code
Space exploration S
Military grade M
Commercial grade C
Toy grade T
6. • Solution:
#include <iostream>
using namespace std;
void main()
{
char code; // the variable is named code and it is in the form of a character or alphabet
cout << "Enter a specification code: "; //output on the screen
cin >> code; //input
if (code == 'S') // if we enter S the following will be the o/p and likewise for
other
cout << "The item is space exploration grade."; //alphabet(code)
else if (code == 'M')
cout << "The item is military grade.";
else if (code == 'C')
cout << "The item is commercial grade.";
else if (code == 'T')
cout << "The item is toy grade.";
else
cout << "An invalid code was entered."; // if we type an alphabet other than S,M,C,T this will be
the o/p
cout << endl; // on the screen
system ("pause");
}
7. • Homework
1-If an angle is equal to 90 degrees, print the message “The angle is a
right angle”;
else, print the message “The angle is not a right angle.”
2-(Data Processing) A student’s letter grade is calculated according to
the following schedule:
Numerical Grade Letter Grade
Greater than or equal to 90 A
Less than 90 but greater than or equal to 80 B
Less than 80 but greater than or equal to 70 C
Less than 70 but greater than or equal to 60 D
Less than 60 F
Using this information, write a C++ program that accepts a student’s
numerical grade,converts the numerical grade to an equivalent letter
grade, and displays the letter grade.
8. Lets Practice This Problem….
• A certain waveform is 0 volts for time less
than 2 seconds and 3 volts for time equal to or
greater than 2 seconds. Write a C++ program
that accepts time in the variable named time
and displays the appropriate voltage,
depending on the input value.
9. • Solution
#include<iostream>
using namespace std;
void main ()
{
int time; //variable is called time and is in the form of integer
cout<<"Enter time :"; //display on the screen
cin>>time; //input
if (time<2) //selection statement
cout<<"0 volts";
else if (time>=2)
cout<<"3 volts";
cout<<endl; //output
system ("pause");
}