0% found this document useful (0 votes)
20 views

A Conditional Statement Having Multiple Alternative Branches Is Called A Chained Conditional

The document discusses chained conditionals and nested conditionals. A chained conditional is a conditional statement with multiple alternative branches, while a nested conditional appears within one of the branches of another conditional statement. Examples are provided of chained and nested conditionals involving the outcome of a football match and purchasing a video game using credits and money.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

A Conditional Statement Having Multiple Alternative Branches Is Called A Chained Conditional

The document discusses chained conditionals and nested conditionals. A chained conditional is a conditional statement with multiple alternative branches, while a nested conditional appears within one of the branches of another conditional statement. Examples are provided of chained and nested conditionals involving the outcome of a football match and purchasing a video game using credits and money.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1

A conditional statement having multiple alternative branches is called a chained conditional.

A conditional expression that has been nested can be found in one of its branches. As an
illustration, consider

in the manner described below;

A chained conditional example

<<<&gt;< # After entering the following variables, the software that follows shows the outcome
of a football match:

H=3 # regular-time home team time Regular time of A=3 #away team In the event of a draw in
regular time, H2=0 #home team extra time A2=3 #away team extra time (in the event that
regular time ends in a tie) #home team penalty shootout P1=0 P=2.

A chained conditional is a conditional statement with a series of alternative branches while a

nested conditional appears in one of the branches of another conditional statement. Examples
are

as follows;

Example of a chained conditional

&gt;&gt;&gt; # The following program displays the winner of a football match after the
following variables have been input:

H=3 # home team regular time A=3 # away team regular time H2=0 # home team extra time(in
case of a draw in regular time) A2=3 # away team extra time(in case of a draw in regular time)
P1=0 # home team penalty shootout P2=2 # away team penalty shootout

if H &gt; A: print('Home team Wins!') elif H2 &gt; A2: print('Home team wins after extra time!')
elif P1 &gt; P2: print('Home team wins on penalties') elif H &lt; A: print('Away team Wins!') elif
H2 &lt; A2: print('Away team wins after extra time') elif P1 &lt; P2: print('Away team wins on
penalties') elif H==A: print('Extra time!')
else: print('Match Cancelled!')

The output of the above program is as follows:

Away team wins after extra time

&gt;&gt;&gt;

Example of a nested conditional.

This program displays an online procedure required to purchase a video game

using credits and money.

points=3000 # credits acquired in the gaming platform cash=300 # money in the user's gaming
account.

if points&gt;1000: print('Your points plus $50 will buy the Game') if cash&gt;=50:
print('Purchase approved!') else: print('You need to deposit money into your account') else:
print('Purchase impossible')

by changing the variables (points and money) one gets different output

statements.

the output of the program above is;

Your points plus $50 will buy the Game Purchase approved! &gt;&gt;&gt;
Question 2
Deeply nested conditionals can become difficult to read. Describe a strategy for avoiding nested

conditionals. Give your own example of a nested conditional that can be modified to become a

single conditional, and show the equivalent single conditional. Do not copy the example from

the textbook.

Ans.

Logical operators can be used to simplify a nested conditional into a single conditional. In the

example below I have used the ‘and’ operator to simplify the nested conditional.

Example.

This program displays an online procedure required to purchase a video game

using credits and money.

points=3000 # credits acquired in the gaming platform

You might also like