Branching Programs: - The Simplest Branching Statement Is A Condi&onal
Branching Programs: - The Simplest Branching Statement Is A Condi&onal
programs
The
simplest
branching
statement
is
a
condi&onal
A
test
(expression
that
evaluates
to
True or
False)
A
block
of
code
to
execute
if
the
test
is
True! An
op<onal
block
of
code
to
execute
if
the
test
is
False!
A
simple
example
x = int(raw_input('Enter an integer: '))! if x%2 == 0:! print()! print('Even')! else:! print()! print('Odd')! print(Done with conditional')!
Some
observa<ons
The
expression
x%2 == 0 evaluates
to
True when
the
remainder
of
x divided
by
2 is
0! Note
that
== is
used
for
comparison,
since
= is
reserved
for
assignment
The
indenta<on
is
important
each
indented
set
of
expressions
denotes
a
block
of
instruc<ons
Note
how
this
indenta<on
provides
a
visual
structure
that
reects
the
seman<c
structure
of
the
program
For example, if the last statement were indented, it would be executed as part of the else block of code