Professional Documents
Culture Documents
Decisions and Loops in C++
Decisions and Loops in C++
COMPARING VALUES
LESS THAN
<=
LESS THAN
OR EQUAL TO
>
GREATER
THAN
>=
GREATER
THAN OR
EQUAL TO
==
EQUAL TO
!=
NOT EQUAL
TO
The IF statement
The IF statement
Syntax
of an IF statement:
if(condition)
{
statement
}
Nested IF statements
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
There are 3
logical
operators used
for
comparisons.
They are:
&&
Logical
AND
||
Logical
OR
Logical
negation
(NOT)
Logical AND
if((letter>=A)&&(letter<=Z))
cout<<You have entered a capital letter<<endl;
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
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