3 Conditional Statemnts A
3 Conditional Statemnts A
3 Conditional Statemnts A
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
Itis a relation between two constants
Since 10 is greater than 6, this expression 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 brackets 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
1
8
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
9
Conditional statements
Example 2-03 (cout)
2
0
Conditional statements
Example 2-03 (cout)
2
1
2
2