Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

BY RITESH KUMAR

COMPARING VALUES

COMPARING AND DECISION MAKING


IS CARRIED OUT BY RELATIONAL
OPERATORS.
THE SIX FUNDAMENTAL OPERATORS
FOR COMPARING TWO VALUES ARE:
<

LESS THAN

<=

LESS THAN
OR EQUAL TO

>

GREATER
THAN

>=

GREATER
THAN OR
EQUAL TO

==

EQUAL TO

!=

NOT EQUAL
TO

The IF statement

The basic if statement allows


your program to execute a
single statement or a block
of statements if a given
condition or expression
evaluates to be true or just
skip it if it evaluates to be
false.

The IF statement

Syntax

of an IF statement:

if(condition)
{
statement
}

Nested IF statements

The statement to be executed


in an if statement can also
be an if. This arrangement is
called a nested if.
The condition for the inner
if is tested only if the
outer if is true.

Example:

PRORAM FOR

#include <iostream>
using namespace std;
int main()
{
char letter('0');
cout<<"Enter your letter:"<<endl;
cin>>letter;
if (letter>='A')
if (letter<='Z')
{
cout<<"YOUR LETTER IS A CAPITAL LETTER"<<endl;
letter += 'a'-'A';
cout<<letter<<endl;
return 0;
}
return 0;
}

IF

Example:

PRORAM FOR

#include <iostream>
using namespace std;
int main()
{
char letter('0');
cout<<"Enter your letter:"<<endl;
cin>>letter;
if (letter>='A')
if (letter<='Z')
{
cout<<"YOUR LETTER IS A CAPITAL LETTER"<<endl;
letter += 'a'-'A';
cout<<letter<<endl;
return 0;
}
return 0;
}

IF

The extended IF

It is a version of if that
allows one statement to be
executed if the condition
returns true and a different
statement to be executed if
the condition returns false.
This is done by the use of
else

The Nested if-else


statements

Just like the nested if


statement, one can nest and
if-else statement too, i.e.
we can nest if-else
statements within ifs
statements and vice-versa
We can also nest if-else
statements within else
statements and vice-versa

Logical Operators and


expressions

There are 3
logical
operators used
for
comparisons.
They are:

&&

Logical
AND

||

Logical
OR

Logical
negation
(NOT)

Logical AND

The AND operator(&&) is used where


there are two conditions and both
must be true for a true result.
For ex:

if((letter>=A)&&(letter<=Z))
cout<<You have entered a capital letter<<endl;

Note that if the left operand for


the && operator is false, the
right operand will not be
evaluated.

Logical OR
The OR operator(||) applies when
you have two conditions and either
one of them or both of them are
true.
For ex
If((letter=A)||(letter=I))
cout<<The letter is a vowel<<endl;
Note that if the left operand for
the || operator is true the right
operand will not be evaluated

Logical NOT
The third logical operator !
Takes one operand of type
bool and inverts its value.
For ex:
If x has a value>10 then
!(x>5) will result as false,
because x>5

Conditional operator

The conditional operator is


also called ternary operator
as it involves 3 operands.
The conditional operator is
generally written as:
condition? expression1:expression2

The switch statement

The switch statement enables


us to select from a multiple
choices based on a set of
fixed values for a given
expression.
Selection is determined by
the value of expression that
we specify.

The switch statement

Particular switch positions


are selected by the case
values which must be the same
as the expression value.
Each case value must be
distinct.
In case no value matches then
default(if specified or else
nothing) is selected.

Unconditional branching
The goto statement enables us to
branch to a specified program
statement unconditionally.
The statement to be branched must
be identified by a statement
label.
For ex:
mylabel:cout<<mylabel....<<endl;
This statement

Loop

A loop executes a particular


statement until a particular
condition is true or false.

You might also like