2. Objec7ve
• A:er
completely
this
chapter,
the
students
will
be
able
to
• Use
control
structure
• Examine
rela7onal
and
logical
operators
• Discover
how
to
use
the
selec7on
control
structures
if
,if
..else
and
switch
3. Control
Structures
• What
is
Control
Structures?
– Used
to
change
the
flow
of
programs
a:er
a
decision
is
taken.
• A
computer
can
proceed:
-‐
In
sequence
-‐
Selec7vely
–
making
a
choice
-‐
Repe77vely
-‐
looping
4. Control
Structures
a.
Sequence
b.
Selec7on
c.
Repea7on
Statement1
T
F
Exp
T
Exp
statement
Statement2
F
Statement1
Statement2
StatementN
5. Rela7onal
Operators
A
condi7on
is
represented
by
a
logical
(Boolean)
expression
that
can
be
true
(1)
or
false
(0)
Rela7onal
operators:
-‐ Allow
comparisons
-‐ Require
two
operands
(binary)
-‐ Evaluate
to
true
or
false
Rela+onal
Operators
in
C++
:
==
,
!=
,
<
,
<=
,
>,
>=
6. Logical
Operators
Logical
Operators:
!
:
NOT
&&
:
AND
||
:
OR
Example:
Expression
Value
19<
8
false
45
>
41
True
7. Control
Structures
Compound
statement
or
block
:
is
a
group
of
statements
which
are
separated
by
semicolons
(;),
but
grouped
together
in
a
block
enclosed
in
braces:
{
}:
{
statement1;
statement2;
…
}
8. If
statement
The
if
statement
:
is
used
to
execute
a
statement
or
block
only
if
a
condi7on
is
sa7sfied.
Form:
if
(condi7on)
statement
Ex:
if(x
==
5)
cout
<<
“
x
is
5”
;
9. if
–else
statement
if-‐else
statement
allows
us
to
specify
two
alterna7ve
statements:
one
which
is
executed
if
a
condi7on
is
sa7sfied
and
one
which
is
executed
if
the
condi7on
is
not
sa7sfied.
Form:
if
(condi7on
)
statement1
else
s
tatement2
Ex:
if
(x==
5)
cout<<“x
is
5”;
else
cout
<<
“x
is
not
5”
10. if
..
Else
if
…else
Syntax:
if
(
exp1)
if
exp1
is
sa7sfied,
do
statement
1;
something.
else
if
(exp
2)
Otherwise,
check
if
exp2
is
sa7sfied
and
if
so,
do
statement
2;
something
else.
…
Otherwise
(else)
,
do
else
completely
Something
else
statement
n;
11. if
..
Else
if
…else
#include
<iostream.h>
void
main(){
int
x;
cout
<<
“Enter
a
number”;
cin
>>
x;
if
(x>10){
cout
<<“You
have
enter
number
more
than
10”;
}
else{
cout
<<“You
have
entered
a
very
small
number”;
}
12. Nested
if
has
another
if
in
it’s
body
or
in
its
else’s
body
or
in
It
both
Syntax:
if
(exp1){
if
(exp1
)
if
(exp2)
statement
1;
statement
1;
else{
else
if
(exp2)
statement
2;
statement
2;
}
else
else
statement
3;
statement
3;
}
13. Switch
case
mul7-‐way
decision-‐making
• Switch
– Useful
when
variable
or
expression
is
tested
for
mul7ple
values
– Test
whether
an
expression
number
of
constant
values
– According
consists
of
a
series
of
case
labels
and
an
op7onal
default
case
– break
is
necessary
14. switch
case
mul7-‐way
decision-‐making
switch
(expression){
case
value
1:
statement
1;
break;
case
value
2:
statement
2;
break;
default:
statements;
}
15. Switch
–
Example1
switch(op){
case
‘+’:
cout<<“The
sum
is
“
<<
a+b;
break;
case
‘-‐’:
cout<<“The
substract
is”<<a-‐b;
break;
case’*’:
cout<<“The
product
is
“
<<
a*b;
break;
default:
cout
<<“n
Wrong
operator”;
}
16. Switch
–
Example1
switch
(grade)
{
case
‘A’:
cout
<<
“Grade
is
between
90
&
100”;
break;
case
‘B’:
cout
<<
“Grade
is
between
80
&
89”;
break;
case
‘C’:
cout
<<
“Grade
is
between
70
&
79”;
break;
case
‘D’:
cout
<<
“Grade
is
between
60
&
69”;
break;
case
‘E’:
cout
<<
“Grade
is
between
0
&
59”;
break;
default:
cout
<<
“You
entered
an
invalid
grade.”;
}
17. Itera7on
• Itera7on
control
statements
allow
you
to
execute
one
or
more
program
statements
repeatedly.
• Itera7on
control
statements:
– while
– do-‐while
– for
18. The
while
statement
• The
while
statement
causes
one
or
more
statements
to
repeat
as
long
as
a
specified
expression
remains
true.
while(expression)
{
F
condi7on
statements;
T
}
statement
20. Do-‐while
statement
• This
loop
is
executed
at
least
once.
• It
is
executed
7ll
the
condi7on
remains
true.
do{
statements;
statement
}while
(expression);
T
F
condi7on
22. for
statement
• for
statement
-‐
is
used
to
execute
one
or
more
statements
a
specified
number
of
7mes.
for
(ini7al
expression;
con7nues
condi7on;
incrementdecrement)
{
statements;
}
23. for
statement
Wrong
statement
for
(i
=
0,
i
<
n,
i
=
i
+2
)
//
semicolons
needed
for
(i
=
0;
i
<
n)
//
three
parts
needed
True
statement
for
(i=0;
i<n;
i
=i+2)
24. for
statement
• Using
a
comma-‐separated
list
of
expressions
for ( int num = 2;num <= 20;total +=
num, num += 2 ) ;
This is equal to :
for ( int num = 2;num <= 20;num += 2 ){
total += num
}
25. for
statement-‐
Example1
//Sum
of
1+2+3+….+100
#include<iostream.h>
void
main(){
int
s=0;
for(int
i=1;i<=100;i++)
s
=
s+i;
cout<<”s
=
”<<s;
}
26. for
statement-‐
Example2
//Program:
S
=
2+4+6+….+2*n
#include<iostream.h>
void
main(){
int
s=0,n;
cout<<”Input
n:”;
cin>>n;
for(int
i=1;i<=n;i++)
s
=
s+2*i;
cout<<”s
=
”<<s;
}