0% found this document useful (0 votes)
22 views6 pages

Branching Programs: - The Simplest Branching Statement Is A Condi&onal

This document discusses branching programs and conditional statements in Python. It explains that conditional statements contain a test expression that evaluates to True or False, and blocks of code to execute depending on the test result. A simple example tests if a number is even or odd. Nested conditionals and compound Boolean expressions are also demonstrated. Branching programs allow programs to make choices and perform different actions, while still maintaining constant time execution based only on the program length.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Branching Programs: - The Simplest Branching Statement Is A Condi&onal

This document discusses branching programs and conditional statements in Python. It explains that conditional statements contain a test expression that evaluates to True or False, and blocks of code to execute depending on the test result. A simple example tests if a number is even or odd. Nested conditionals and compound Boolean expressions are also demonstrated. Branching programs allow programs to make choices and perform different actions, while still maintaining constant time execution based only on the program length.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Branching

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

We can have nested condi<onals


if x%2 == 0:! if x%3 == 0:! print('Divisible by 2 and 3)! else:! print('Divisible by 2 and not by 3)! elif x%3 == 0:! print('Divisible by 3 and not by 2)!

And we can use compound Booleans


if x < y and print('x elif y < z:! print('y else:! print('z x < z:! is least)! is least)! is least) !

What have we added?


Branching programs allow us to make choices and do dierent things But s<ll the case that at most, each statement gets executed once. So maximum <me to run the program depends only on the length of the program These programs run in constant &me

You might also like