Professional Documents
Culture Documents
3-Conditional Statemnts-D
3-Conditional Statemnts-D
3-Conditional Statemnts-D
1
Conditional statements
2
Conditional statements
Relational Expressions
It is written to find a relation between two expressions
and it will return only one value. True or false.
Consists of variables, constants or arithmetic operators.
These are combined by a "relational operator"
For example, 10>6
It is a relation between two constants
Since 10 is greater than 6, this expressions returns a
true value. Means that the statement is true.
">" is a relational operator here.
3
Conditional statements
Relational operators
As explained, the operators used to show relation between two
expressions is a relational operator.
“>" greater than, if the first value is greater than the second one.
e1>e2
True or false
">=" Greater than or equal to
e1>=e2
True or false
“<" less than, similar to greater than
“<=" less than or equal to, which is similar to ">="
4
Conditional statements
Relational operators
“==‟ is equal to
e1==e2
True or false
“ !=‟ is not equal to
e1!=e2
True or false
5
Conditional statements
Relational operators
Example, if x=10, y=20, z=5 what will be the result of
following expressions
Relational Output
Expression
x>y
y>z
y != x
x ==z
x <= y
z >= y
6
Conditional statements
Relational operators
7
Conditional statements
The "if" statement:
Exampl
e x=10 and y=4
Let
if(x>y)
x+y;
x*y;
The above
statement will
see if x is
greater, as in
this case it is,
the result will
be something
like:
x+y=14 8
Conditional statements
The "if" statement:
Used to execute (or ignore) a set of statements
After the "if‟ condition is tested
Syntax
if
(condition)
statement-
1;
statement-2;
if(10>6); Here
the condition is
‟10>6‟
If 10 is indeed greater than 6, then the statements under
the if condition are followed. 9
Conditional statements
The "if" statement:
If there is a set of instructions, the statements are
enclosed within “{ }“.
The Syntax then would be
if (condition)
{
statement-1
statement-
2
statement-3
statement-
m
}
1
0
Conditional statements
The "if" statement:
All the statements within "{ }‟ are called "compound
statements“.
If the condition given in "if statement‟ is true, the
compound statements in the braces are executed.
If in the case the condition is false, the statements
within
the brackets are ignored and statement-n is
executed.
Noteworthy to remember that statement-n will be
executed in both the cases.
11
Conditional statements
1
2
Conditional statements
Example 2-01
{
int
a,b;
a=100;
b=50;
if(a>b
)
cout << "Islamabad!" <<
endl; cout<<"ok"<<endl;
return 0;
} 1
3
Conditional statements
Example 2-01 (cout)
1
4
Conditional statements
Example 2-02
{
int n;
cout<<“enter the value of n”<<endl;
cin>>n;
if(n>10)
{
cout<<"Islamabad"<<endl;
cout<<"ok"<<endl;
}
cout << "Hello world!" << endl;
return 0;
} 1
5
Conditional statements
Example 2-02 (cout)
1
6
Conditional statements
Example 2-02 (cout)
1
7
Conditional statements
Example 2-03
{
int n;
cout<<"Enter a Number?
"; cin>>n;
if(n%3==0)
{
cout<<"the number is
divisible by 3"<<endl;
}
cout << "Hello world!" <<
endl; return 0;
} 1
8
Conditional statements
Example 2-03 (cout)
1
9
Conditional statements
Example 2-03 (cout)
2
0
2
1
Conditional statements
Example 2-04
{
int a,b;
cout<<"Enter first value =
"; cin>>a;
cout<<"Enter second
value = ";
cin>>b;
if(a>b)
cout<<"First value is
greater"; if(b>a)
cout<<"Second value is
greater"; return 0;
} 2
2
Conditional statements
Example 2-08 (cout)
2
3
Conditional statements
Example 2-08 (cout)
2
4
Example 2-08:
2
5
Conditional statements
Example 2-08 (cout)
2
6
Conditional statements
Example 2-08:
int a, b;
cout<<"Enter first number? "<<endl;
cin>>a;
cout << "Enter second number? " <<endl;
cin>>b;
if(a>b)
cout<<"First number is greater";
if (b>a)
cout<<"Second number is greater";
if (a==b)
cout<<“two numbers are equal"; 2
7
Example 2-08:
2
8
Conditional statements
Example 2-05
float cu, pu, units,
cout<<"Enter the current reading ? ";
bill; //2460
cin>>cu;
cout<<"Enter the previous reading ? "; //2000
cin>>pu;
units=cu-pu;
if(units>300)
bill=units*3.5+units*(0.05/100);
if(units<=300)
bill=units*3.0;
cout<<"This month units are = "<<units<<endl;
cout << "Electricity bill is = "<<bill<<endl;
2
9
Conditional statements
Example 2-05 (cout)
3
0
Conditional statements
Example 2-05 (cout)
3
1
Conditional statements
Extra points:
3
3
Conditional statements
The "if-else" Statement:
34
Conditional statements
3
5
Conditional statements
Example:
#include <iostream>
using namespace std;
int main() // Most important part of the program!
{
int age; // Need a variable...
cout<<"Please input your age: "; // Asks for age
cin>> age; // The input is put in age
// Throw away enter
if ( age < 60 ) // If the age is less than 60
cout<<"You are young!\n"; // Just to show you it works...
else
cout<<"You are old\n"; // Executed if no other statement is there
return 0;
}
3
6
Example (output):
3
7
Example (output):
3
8
Conditional statements
Example 2-07
{
int n;
cout<<"Enter an integer value ? "; cin>>n;
if(n>100)
cout<<"number is greater than 100";
else
cout<<"number is equal to or less than 100";
return 0;
}
39
Conditional statements
Example 2-07 (cout)
4
0
Conditional statements
Example 2-07 (cout)
4
1
Conditional statements
The "if-else" Statement:
Syntax-1
If (condition)
Statement-1;
else
Statement-2;
4
2
The "if-else" Statement:
Syntax-2
if (condition) First Block
{
Statement-1;
Statement-2;
Statement-z;
}
else
{ Second Block
Statement-1;
Statement-2;
Statement-z;
} 4
3
Example 2-09:
int basic,ma,fa,hr,net;
cout<<"Enter your basic pay = “<<endl;
cin>>basic;
if (basic>5000)
{
ma=basic*2/100;
ca=193;
hr=basic*45/100;
}
else
{
ma=basic*5/100;
ca=196;
hr=basic*45/100;
}
net=basic+ma+fa+hr;
cout<<"your net pay is = "<<net;
4
4
Conditional statements
Example 2-09 (cout)
4
5
Conditional statements
Example 2-09 (cout)
4
6
Conditional statements
Example 2-09 (cout)
4
7
Conditional statements
Example 2-10 (odd/even number)
{
int n;
cout<<"Enter an integer value ?
"<<endl; cin>>n;
if (n%2==1)
cout<<"it is an odd
number"<<endl; else
cout<<"it is an even
number"<<endl; return 0;
}
4
8
Conditional statements
Example 2-10 (cout)
4
9
Conditional statements
Example 2-10 (cout)
5
0
Conditional statements
Example 2-11(equal or different numbers)
{
int a,b;
cout << "enter the first value = " << endl;
cin>>a;
cout<<"enter the second value = "<<endl;
cin>>b;
if (a%3 == b%3)
cout<<“C++”;
else
cout<<"Programming fundamentals";
return 0;
} 5
1
Conditional statements
Example 2-11 (cout)
5
2
Conditional statements
Example 2-11 (cout)
5
3
5
4
Conditional statements
The “nested if"
Statement:
When an “if statement” is used within another “if statement” it is
called “nested if statement“.
It is used for multi-way decision making.
Syntax
if (condition-1)
{
if (condition-2)
{statement-1;
}
Statement-n;
5
5
FLOW
CHART
5
6
FLOW CHART
5
7
Conditional statements
5
8
Conditional statements
Example 2-12(three similar or different values)
int a,b,c;
cout<<"Enter first value = "<<endl;
cin>>a; //15,15,15
cout << "Enter second value = " << endl;
cin>>b; //15,20,15
cout<<"Enter third value = "<<endl;
cin>>c; //15,25,20
If (a==b)
{
if(a==c)
cout<<"All values are equal";
}
else
cout<<"these values are different“;
5
9
Conditional statements
Example 2-12 (cout)
6
0
Example 2-12 (cout)
6
1
Conditional statements
Example 2-12 (cout)
6
2
Example 2-13 (which number is greater)
cout<<"Enter first no. "; cin>>a;//5,15,30
cout<<"Enter second no. "; cin>>b; //4,20,25
cout<<"Enter third no. "; cin>>c; //3,18,35
if (a>b)
{
if(a>c)
cout<<"First no. is greater ";
else
cout<<"Third no. is greater ";
}
else if(b>c)
{
cout<<"Second no. is greater ";
} 63
6
3
Conditional statements
Example 2-13(cout)
6
4
Example 2-13 (which number is greater)
cout<<"Enter first no. "; cin>>a; //5,15,30
cout<<"Enter second no. "; cin>>b; //4,20,25
cout<<"Enter third no. "; cin>>c; //3,18,35
if(a>b)
{
if(a>c)
cout<<"First no. is greater ";
else
cout<<"Third no. is greater ";
}
else if(b>c)
cout<<"Second no. is greater ";
else
cout<<"Third no. is greater “;
6
5
Conditional statements
Example 2-13 (cout)
6
6
Conditional statements
Example 2-13 (cout)
6
7
Conditional statements
Example 2-13 (cout)
6
8
6
9
Example 2-14 (Grading of students)
int s1,s2,s3,avg;
char grade;
cout<<"Enter the marks of subject 1 ";cin>>s1; //25,50,90
cout << "Enter the marks of subject 2 "; cin>>s2; //26,70,80
cout<<"Enter the marks of subject 3 "; cin>>s3; //50,65,80
avg=(s1+s2+s3)/3;
if(avg>33) //if no1
{
if (avg>50) //if no 2
{
if(avg>80) //if no 3
grade='A';
else //else no 3
grade='B';
}
else //else no 2
grade='C';
}
else //else no1
grade='F';
cout<<"Avg of subjects is = "<<avg<<endl;
cout<<"Grade = "<<grade<<endll; 7
0
Conditional statements
Example 2-14 (cout)
7
1
Conditional statements
Example 2-14 (cout)
7
2
Conditional statements
Example 2-14 (cout)
7
3
Example: (username
+password)
#include <iostream>
using namespace std;
int main()
{
char username;
int password;
cout<<"Enter username:";
cin>>username;
cout<<"enter password:";
cin>>password;
if(username=='a')
{
if (password==12345)
cout<<"Login successful";
else
cout<<"Password is incorrect, Try again.";
}
else
cout<<"Username is incorrect, Try again.";
return 0;
} 7
4
Output:
7
5
Output:
7
6
Output:
7
7
Conditional statements
The nested "if-else“ or "else-if"
Statement:
“if" condition followed by one or more "else if‟ statement(s) then
the block/set of statements under the "if‟ condition is called "nested
if- else" statement.
This can be used for a multiple selection problem, i.e. if we
have more than one condition but only one option to select.
Syntax
if(condition-1)
statement-1;
else if(condition-
2)
statement-2;
else if(condtion-
3)
statement-
3; and so
on…
else
if(condition- 7
8
Conditional statements
Example 2-15 (Arithmetic
operations) int a, b;
char op;
cout<<"Enter first integer, operator and then second integer\n
"; cout<<"And press Enter key for each input ";
cin>>a >>op >>b
; if(op == '+')
cout<<"Addition = "<<
(a+b); else if(op == '-')
cout<<"Subtraction = "<< (a-
b); else if(op == '*')
cout<<"Multiplication = "<<
(a*b); else if(op == '/')
cout<<"Division = "<<
(a/b);
else
cout<<"Invalid input"; 7
9
Conditional statements
Example 2-15 (cout)
8
0
Conditional statements
Example 2-15 (cout)
8
1
Conditional statements
Example 2-15 (cout)
8
2
Conditional statements
Example 2-15 (Arithmetic
operations) int a, b;
char op;
cout<<"Enter first integer, operator and then second integer\n
"; cout<<"And press Enter key for each input ";
cin>>a >>op >>b // 78 / 3 = 26
; if(op == '+') // ignored
cout<<"Addition = "<<
(a+b); else if(op == '-') // ignored
cout<<"Subtraction = "<< (a-
b); else if(op == '*') // ignored
cout<<"Multiplication = "<<
(a*b); else if(op == '/') // true & executes
cout<<"Division = "<< // shows this
(a/b); output
else // ignored
cout<<"Invalid input"; 8
3
Conditional statements
Example 2-15 (cout)
8
4
Example: (two numbers relation)
#include <iostream>
using namespace std;
int main()
{
int number1, number2;
cout<<"Enter two integers: “;
cin>>number1>>number2;
if (number1 == number2) //checks if two integers are equal.
{
cout<<“number1 is equal to number2”;
}
else if (number1 > number2) //checks if number1 is greater than number2.
{
cout<<“number1 is greater than number2”;
}
else // if both test expressions are false
{
cout<<“number1 is smaller than number2”;
}
return 0; 8
5
Example: (two numbers relation)
#include <iostream>
using namespace std;
int main()
{
int number1, number2;
cout<<"Enter two integers: “;
cin>>number1>>number2;
if (number1 == number2) //checks if two integers are equal.
{
cout<<“number1 is equal to number2”;
}
else if (number1 > number2) //checks if number1 is greater than number2.
{
cout<<“number1 is greater than number2”;
}
else if (number1<number2) // if both test expressions are false
{
cout<<“number1 is smaller than number2”;
}
return 0; 8
6
Example (output):
8
7
Example (output):
8
8
Example (output):
8
9
Example: (positive or negative number)
{
int num;
/* Input number from user */
cout<<"Enter any number: “;
cin>>num;
if(num > 0)
cout<<"Number is POSITIVE";
else if (num < 0)
cout<<“Number is NEGATIVE";
else
cout<<“ Number is ZERO";
return 0;
}
9
0
Example: (positive or negative number)
{
int num;
/* Input number from user */
cout<<"Enter any number: “;
cin>>num;
if(num > 0)
cout<<"Number is POSITIVE";
else if (num < 0)
cout<<“Number is NEGATIVE";
else if(num==0)
cout<<“ Number is ZERO";
return 0;
}
9
1
Example: (output)
9
2
Example: (output)
9
3
Example: (output)
9
4
9
5