Lesson # 5 Making decision in C++
Before talking about making decision in C++, it is important to become
familiar with relational, equality and logical operators. Like arithmetic
operators; relational operators, equality operators and logical operators are
also used in C++. These operators are
often encountered in if-statements. In an if-statement a condition is checked
with the help of these operators which return Boolean value i.e. True (1) or False (0). The following table summarizes
these operators.
RELATIONAL OPERATORS
|
|||
Operator
|
Algebraic representation
|
C ++ Representation
|
Example
|
Greater
Than
|
>
|
>
|
x > y
|
Less
than
|
<
|
<
|
x < y
|
Greater
than or equal to
|
≥
|
>=
|
x>=y
|
Less
than or equal to
|
≤
|
<=
|
x<=y
|
EQUALITY OPERATORS
|
|||
Equal
|
=
|
==
|
x==y
|
Not
equal
|
≠
|
!=
|
x!=y
|
LOGICAL OPERATORS
|
|||
AND
|
AND
|
&&
|
x&&y
|
OR
|
OR
|
||
|
x||y
|
Here is a simple program which illustrate the use relational, equality
and logical operators.
using namespace std;
#include<iostream>
main()
{
int x=15,y=20;
int a=50,b=60;
string myname="Umar",yourname="Ali";
cout<<(x>y)<<endl;
cout<<(x<y)<<endl;
cout<<(x>=y)<<endl;
cout<<(x<=y)<<endl;
cout<<(x==y)<<endl;
cout<<(x!=y)<<endl;
cout<<(x>y&&a>b)<<endl;
cout<<(x<y||a<b)<<endl;
cout<<(myname==yourname);
cout<<endl;
system("pause");
}
Let’s discuss the code quickly.
using namespace std;
#include<iostream>
main()
{
int x=15,y=20; //
Declares two integer variables x and y and initializes them with 15 and 20
int a=50,b=60; //
Declares two integers variables a and b and initializes them with 50 and 60
string myname="Umar",yourname="Ali"; // Declares two string variables
myname and yourname
cout<<(x>y)<<endl; // Displays 0 or 1, if the statement
x>y is true then it returns 1
otherwise 0 which means the statement is false. In this case it displays 0
because x is NOT greater than y.
cout<<(x<y)<<endl; // Displays 0 or 1, if the statement
x<y is true then it returns 1
otherwise 0 which means the statement is false. In this case it displays 1
because x is less than y.
cout<<(x>=y)<<endl; // Displays 0 or 1, if any of the statements
i.e. x>y or x=y is true then it
returns 1 otherwise 0 which means the
statement is false. In this case it displays 0 because x is NOT greater than or
equal to y.
cout<<(x<=y)<<endl; // Displays 0 or 1, if any of
statements i.e. x<y or x=y is true then it returns 1 otherwise 0 which means the statement is
false. In this case it displays 1 because x is less than y. (If one condition
is fulfilled, the other not need to be fulfilled.)
cout<<(x==y)<<endl;
// Displays 0 or 1, if the statement x==y
is true then it returns 1 otherwise 0
which means the statement is false. In this case it displays 0 because x is not
equal to y.
cout<<(x!=y)<<endl; // Displays 0 or 1, if the statement x!=y
is true then it returns 1 otherwise 0
which means the statement is false. In this case it displays 1 because x is not
equal to y.
cout<<(x>y&&a>b)<<endl; // Displays 0 or 1, if both of the
statements i.e. x>y and a>b are true then it returns 1 otherwise 0 which means the statement is
false. In this case it displays 0 because both of the statements are false.
cout<<(x<y||a<b)<<endl; // Displays 0 or 1, if any of the
statements i.e. x<y OR a<b is true then it returns 1 otherwise 0 which means the statement is
false. In this case it displays 1 because any of the statements is true.
cout<<(myname==yourname); // Displays 0 or 1, if the statements
myname==yourname is true then it returns
1 otherwise 0 which means the statement is false. In this case it
displays 0 because these two strings are not equal
cout<<endl;
system("pause");
}
using namespace std;
#include<iostream>
main()
{
int x=15,y=20; //
Declares two integers variables x and y and initializes them with 15 and 20
int a=50,b=60; //
Declares two integers variables a and b and initializes them with 50 and 60
string myname="Umar",yourname="Ali"; // Declares two string variables
myname and yourname
cout<<(x<y)<<endl; // Displays 0 or 1, if the statement
x<y is true then it returns 1
otherwise 0 which means the statement is false. In this case it will display 0
because x is greater than y.
cout<<(x>y)<<endl;
// Displays 0 or 1, if the statement
x>y is true then it returns 1
otherwise 0 which means the statement is false.
Code & Output:
5.2 If-Statement
If-statement lets a program to make a decision based on the value of a
condition. If the condition is true then body of the if-statement is executed
otherwise the body of the if-statement is not executed, in case the condition
is false. If-statement is made of relational, equality and logical operators and
other statements in the body of the if-statement. For if-statement the keyword
if is used, after if a specific condition is defined. The simple if-statement
is given below.
If(condition)
{
Statement a;
Statement b;
Statement c;
}
Above code is executed only, when the condition within braces is met
i.e. it is true.
Let’s see a real example in C++.
using namespace std;
#include<iostream>
main()
{
int num1=300,num2=250;
if(num1>num2)
{
cout<<"Num1 is greater than num2";
}
cout<<endl;
system("pause");
}
Let’s discuss how this code
works.
using namespace std;
#include<iostream>
main()
{
int
num1=300,num2=250; // Declares
two integer variables num1 and num2 and initializes them with 300 and 250
if(num1>num2)
// Checks,
whether num1 is greater than num2. In this case the condition is true, the body
of the if-statement is executed. Therefore Num1 is greater than num2 is
displayed on the screen.
{ // indicates the
start of the if-statement body
cout<<"Num1 is greater than num2"; // A statement in if-statement body.
} //
indicates the send of the if-statement body.
cout<<endl;
system("pause");
}
Code & Output:
5.3 If else statement
The if else statement can be used to define a different block of code
to be executed if the condition is false. The simple form of the if else
statement is given below.
If(condition)
{
Statement a;
Statement b;
Statement c;
}
Else
{
Statement d;
Statement e;
Statement f;
}
In the above code, the condition is checked if it is true then body of
the if-statement is executed otherwise the body of the else is executed.
Let’s see an example.
using namespace std;
#include<iostream>
main()
{
int num1,num2;
cout<<"Enter First Number: ";
cin>>num1;
cout<<"Enter Second Number: ";
cin>>num2;
if(num1>num2) {
cout<<"The Large Number is : "<<num1;
}
else
{
cout<<"The Large Number is : "<<num2;
}
cout<<endl;
system("pause");
}
Discussion about code is given below.
using namespace std;
#include<iostream>
main()
{
int
num1,num2; // Declares two integer
variables num1 and num2
cout<<"Enter First Number: "; // Displays “Enter First Number” on the screen
cin>>num1; //
Gets data from keyboard and stores it in num1 variable
cout<<"Enter Second Number: "; // Displays “Enter
Second Number” on the screen
cin>>num2; //
Gets data from keyboard and stores it in num2 variable
if(num1>num2)
// Checks, whether num1 is greater
than num2. If the condition
is met, the body of
the if-statement is executed otherwise the control is given to else statement.
{ //Indicates
the start of if-statement body.
cout<<"The Large Number is : "<<num1;
} //Indicates
the end of if-statement body.
else
//In
case the if-statement condition is false then else block of code is executed.
{ //Indicates
the start of if-statement body.
cout<<"The Large Number is : "<<num2;
} //Indicates
the end of else body.
cout<<endl;
system("pause");
}
Code & Output:
5.4 Nested if-statement
An if-statement within another if-statement
is called nested if-statement which is used to check more than one condition. We
can use as many if-statements in a program as we want but they make our code complicated
which not easy to understand for others. In a nested if-statement, every if-statement
must have its own opening and closing brace. To make the code clear, we should
use proper indentation. The general
syntax is given below;
If (condition-1)
{
If(condition-2)
{
Statement a;
Statement b;
Statement c;
}
Else
{
If(condition -3) {
Statement d;
Statement e;
Statement f;
}
}
}
Let me illustrate it with help of an example.
This code compares three integers and displays the largest integer using nested
if-statement.
using namespace std;
#include<iostream>
main()
{
int num1,num2,num3;
cout<<"Enter First
Number: ";
cin>>num1;
cout<<"Enter Second
Number: ";
cin>>num2;
cout<<"Enter Third
Number: ";
cin>>num3;
if(num1>num2)
{
if(num1>num3)
{
cout<<"The Large
Number is : "<<num1;
}
else
{
if(num2>num1)
{
if(num2>num3)
{
cout<<"The Large
Number is : "<<num2;
}
}
else
{
if(num3>num1)
{
if(num3>num2)
{
cout<<"The Large Number is : "<<num3;
}
}
}
}
}
cout<<endl;
system("pause");
}
Let me discuss the code briefly.
using namespace std;
#include<iostream>
main()
{
int num1,num2,num3; // Declares two integer variables
num1,num2 and num3
cout<<"Enter First Number: "; // Displays “Enter First
Number” on the screen
cin>>num1; // Gets
data from keyboard and stores it in num1 variable
cout<<"Enter Second Number:
"; // Displays
“Enter Second Number” on the screen
cin>>num2; //
Gets data from keyboard and stores it in num2 variable
cout<<"Enter Third Number: "; // Displays “Enter Third
Number” on the screen
cin>>num3; //
Gets data from keyboard and stores it in num3 variable
if(num1>num2) // Checks,
whether num1 is greater than num2. If the condition
is met, the body of
the if-statement is executed .
{ //Indicates
the start of first if-statement body.
if(num1>num3) //
Checks, whether num1 is greater than num3. If the condition
is met, the body of
the if-statement is executed.
{ //Indicates
the start of second if-statement body.
cout<<"The Large
Number is : "<<num1;
} //Indicates
the end of second if-statement body.
Else
//In case the first and second if-statement conditions are false then
else block of code is executed.
{ //Indicates
the start of else body.
if(num2>num1) //
Checks, whether num2 is greater than num1. If the condition is met, the body of
the if-statement is executed.
{ //Indicates
the start of third if-statement body.
if(num2>num3) // Checks,
whether num2 is greater than num3. If the condition
is met, the body of
the if-statement is executed.
{ //Indicates the start of fourth if-statement
body.
cout<<"The Large Number is : "<<num2;
} //Indicates
the end of fourth if-statement body.
} //Indicates
the end of third if-statement body.
else //In
case the third and fourth if-statement conditions are false then else block of
code is executed.
{ //Indicates the start of else body.
if(num3>num1) //
Checks, whether num3 is greater than num1. If the condition
is met, the body of
the if-statement is executed.
{ //Indicates
the start of fifth if-statement body.
if(num3>num2) // Checks,
whether num3 is greater than num2. If the condition
is met, the body of
the if-statement is executed.
{ //Indicates
the start of sixth if-statement body.
cout<<"The Large Number is : "<<num3;
} //Indicates
the end of sixth if-statement body.
} //Indicates
the end of fifth if-statement body.
} //Indicates
the end of second else body
} //Indicates
the end of first else body
} //Indicates
the end of first if-statement body.
cout<<endl;
system("pause");
}
Code & Output:
5.5 If else if statement.
It is another way to handle nested if, by
using if else if statement code is easy to maintain and easy to understand. The
following few lines explain the general syntax of “if else if statement.”
If (condition-1)
{
Statements
}
Else if(condition-2)
{
Statements
}
Else if(condition -3)
{
Statements
}
Else
{
Statements
}
Here
is an example of if else if statement
using
namespace std;
#include<iostream>
main()
{
int
score;
string
grade;
cout<<"Enter
your score: ";
cin>>score;
if(score>=90)
{
grade=" A";
}
else
if(score>=80)
{
grade=" B";
}
else
if(score>=70)
{
grade=" C";
}
else
if(score>=60)
{
grade=" D";
}
else
{
grade=" F";
}
cout<<"Grade
for score "<<score<<" is :"<<grade;
cout<<endl;
system("pause");
}
Let’s
discuss the code briefly.
// Gets data from keyboard and stores it in num1 variable
//Indicates the start of fourth if-statement body.
using
namespace std;
#include<iostream>
main()
{
int score;
// Declares an integer
variables score
string
grade; // Declares a string
variables grade
cout<<"Enter
your score: "; //
Displays “Enter your score” on the screen
cin>>score; // Gets data from keyboard and
stores it in score variable
if(score>=90) // Checks, whether score
is greater than or equal to 90. If the condition
is
met, the body of the if-statement is executed.
{
grade=" A";
}
else
if(score>=80) //
Checks, whether score is greater than or equal to 80. If the condition
is
met, the body of the if-statement is executed.
{
grade=" B";
}
else
if(score>=70) //
Checks, whether score is greater than or equal to 70. If the condition
is
met, the body of the if-statement is executed.
{
grade=" C";
}
else
if(score>=60) //
Checks, whether score is greater than or equal to 60. If the condition
is
met, the body of the if-statement is executed.
{
grade=" D";
}
else { // If
all the conditions above are false then the body of the else-statement is
executed.
grade=" F";
}
cout<<"Grade
for score "<<score<<" is :"<<grade; // Displays grade on the screen.
cout<<endl;
system("pause");
}
{ //Indicates
the start of third if-statement body.
if(num2>num3) // Checks,
whether num2 is greater than num3. If the condition
is met, the body of
the if-statement is executed.
{ //Indicates the start of fourth if-statement
body.
cout<<"The Large Number is : "<<num2;
} //Indicates
the end of fourth if-statement body.
} //Indicates
the end of third if-statement body.
else //In
case the third and fourth if-statement conditions are false then else block of
code is executed.
{ //Indicates the start of else body.
if(num3>num1) //
Checks, whether num3 is greater than num1. If the condition
is met, the body of
the if-statement is executed.
{ //Indicates
the start of fifth if-statement body.
if(num3>num2) // Checks,
whether num3 is greater than num2. If the condition
is met, the body of
the if-statement is executed.
{ //Indicates
the start of sixth if-statement body.
cout<<"The Large Number is : "<<num3;
} //Indicates
the end of sixth if-statement body.
} //Indicates
the end of fifth if-statement body.
} //Indicates
the end of second else body
} //Indicates
the end of first else body
} //Indicates
the end of first if-statement body.
cout<<endl;
system("pause");
}
Code & Output:
Using logical operator with if-statement.
We can
combine many if-statements into a single if-statement by using logical
operators such as && and ||. && operator returns true when both
the conditions are true otherwise false.
An OR operator ( ||) returns true when any of the condition is true,
false is return only if both the conditions are false.
using
namespace std;
#include<iostream>
main()
{
int
num1,num2,num3;
cout<<"Enter
First Number: ";
cin>>num1;
cout<<"Enter
Second Number: ";
cin>>num2;
cout<<"Enter
Third Number: ";
cin>>num3;
if(num1>num2&&num1>num3)
{
cout<<"The Large
Number is : "<<num1;
}
else
if(num2>num1&&num2>num3)
{
cout<<"The Large Number is :
"<<num2;
}
else
if(num3>num1&&num3>num2)
{
cout<<"The Large Number is :
"<<num3;
}
else
{
cout<<"Two numbers are
equal";
}
cout<<endl;
system("pause");
}
Here is how
this code works.
using
namespace std;
#include<iostream>
main()
{
int num1,num2,num3; // Declares two integer variables
num1,num2 and num3
cout<<"Enter First Number: "; // Displays “Enter First
Number” on the screen
cin>>num1; // Gets
data from keyboard and stores it in num1 variable
cout<<"Enter Second Number:
"; // Displays
“Enter Second Number” on the screen
cin>>num2; //
Gets data from keyboard and stores it in num2 variable
cout<<"Enter Third Number: "; // Displays “Enter Third
Number” on the screen
cin>>num3; //
Gets data from keyboard and stores it in num3 variable
if(num1>num2&&num1>num3)
// Checks whether two
conditions i.e. num1>num2&&num1>num3 are true. If true is return
the body of the if-statement is executed.
{
cout<<"The Large
Number is : "<<num1;
}
else
if(num2>num1&&num2>num3) // Checks whether two conditions i.e. num2>num1&&num2>num3are
true. If true is return the body of the if-statement is executed.
{
cout<<"The Large Number is :
"<<num2;
}
else
if(num3>num1&&num3>num2) // Checks whether two conditions i.e. num3>num1&&num3>num2 are true.
If true is return the body of the if-statement is executed.
{
cout<<"The Large Number is :
"<<num3;
}
else { //
If all of the conditions are false then the else body is executed.
cout<<"Two numbers are
equal";
}
cout<<endl;
system("pause");
}
No comments:
Post a Comment