Introduction To Programming 2
Introduction To Programming 2
to
Programming
Further
Selec)on
Control
of
Flow
SEQUENCE
SELECTION
REPETITION
SELECTION
Control
structures
IF
IF
(condi)on
is
true
)
THEN
{do
this}
IF
ELSE
IF
(condi)on
is
true)
THEN
{do
this}
ELSE
{do
alterna)ve
ac)on}
if
in
C
Selec)on
statements
allow
the
program
to
choose
between
alternate
ac)ons
during
execu)on
if
(<condi)onal
expression>)
<statement>
if
(age
>=
18)
prinO(You
can
drive);
if
else
in
C
if
(condi)onal
expression)
<statement1>
else
<statement2>
if
(age
>=
18)
prinO(You
can
drive);
else
prinO(Sorry,
wait
un)l
you
are
18);
Using
Blocks
{
}
To
include
more
than
one
statement
to
be
executed
if
a
condi)on
is
true,
put
them
in
a
block,
i.e.
{
}
if(age
>=
18)
{prinO(You
can
drive);
prinO(You
can
go
to
University);
}
Rela%onal
Operators
Rela%onal
Operator
Test
Condi%on
==
a
==
b
equal
to
!=
a
!=
b
not
equal
to
<
a<b
less
than
<=
a<=b
less
than
or
equal
to
>
a>b
greater
than
>=
a>=b
greater
than
or
equal
to
Driving
Program
PROMPT
Enter
your
age
GET
age
STORE
age
IF
age
>=
18
OUTPUT
You
can
drive
ELSE
OUTPUT
Wait
un)l
you
are
18
Driving
Program
in
C
#include
<stdio.h>
main()
{
int
age;
prinO(please
enter
your
age);
scanf(%d,
&age);
if
(age
>=
18)
{
prinO(
You
can
drive);
}
else
{
prinO(Wait
un)l
you
are
18
to
drive);
}
}