Conditional and Looping Constructs in Python


INTRODUCTION 

In the previous chapters , we have learnt the basics of Python programming where we were dealing with simple short programs containing a sequence of instructions given for carrying out a particular task . It was all in sequence , i.e. , one after the other . In real life also , this sequence of operations is observed when a school bus carries school children from school to home and vice versa  There is only one way - the driver has no choice but to follow the road , one stoppage after another , to reach home / school and has to follow a sequence . Conditional and Looping Constructs .

Similarly , Python also executes one statement after another in a program . All these statements are executed by the CPU . We already know that a CPU has three parts - CU ( Control Unit ) , ALU ( Arithmetic Logic Unit ) and MU ( Memory Unit ) .


 The Arithmetic Logic Unit is involved in all mathematical operations and conversions . Now , we will focus on Logic Unit which makes a computer think ; so we need to write the code . In real - life scenarios also , we often go with the flow and tend to take Output decisions , for example , after completing your 10th standard , you decide which stream to go for and after completing graduation , one has to decide which company to join . Similarly , if you have ever used Google Maps direction services to travel from one place to another , you might have noticed that sometimes it shows more than one path- the least crowded path, the shortest distance path, etc., and you can decide which path to take as per your priorities. A decision choosing one of the two or more possible options. If it is raining, I will use an umbrella so that I don't get wet as shown in picture . This of branching. If one condition is true, that it is raining, I take my umbrella. If the condition is not true but false, then I will not take my umbrella. 


PROGRAM CONTROL FLOW


The control and flow of a program in any programming language can be broadly classified into three categories:
1. Sequential
2. Selection Conditional/Decision
3. Iteration/Looping
Now we shall discuss these program control flow constructs in brief.

1. Sequential: Sequential statements in the program are executed
in a sequential order or in a linear fashion, one after another,
without any jump in the program. In picture statement 1
will be executed first, followed by statement 2 and then by Statement 2 Statement 3 
SEQUENTIAL STATEMENT

2. Selection Conditional/Decision: When the program uses a condition to decide which set of statements is to be executed
between two alternative sets of statements. In conditional statements 'if statement is used. In picture . "body of it statement" will be executed if test condition evaluates to true.
CONDITIONAL STATEMENT

3. Iteration /Looping: When a certain set of statements needs to be executed multiple times in a loop. In picture, body of loop will be executed until test expression evaluates to true.

                    DECISION-MAKING

The order of execution of the statements in a program is known as flow of control. It is

dependent on the programming constructs used while writing the program. Decision-making

statements are used to control the flow of execution of a program depending upon the

condition.

There are four types of decision-making statements in Python:

1. if statement

2. if-else statement

3. if-elif ladder

4. Nested if-else statement


1) if Statement

if statement is the easiest method for making a decision in Python. It simply states that if something is true, Python should perform the steps that follow. In programming, decision- making or selection can be achieved through the conditional statement. The simplest form is the if statement.

Syntax:

if condition                                                                      

statement (s)

Header

IF STATEMENT FLOW CHART


2. if-else Statement

if' statement alone tells us that if a condition is true, it will execute a block of statements and if the condition is false, it won't. But what if we want the program to display some appropriate message or to execute certain set of instructions in case the condition becomes false? Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false as shown in picture .

Syntax:

if condition:

statement (S)

else:

statement (S

IF ELSE EXAMPLE


3. if-elif-else Statement

if-elif-else construct is used in the situations where a user can decide among multiple options.As soon as one of the Conditions controlling the 'if' is true, then statement/statements associated with that 'if are executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed, and control flows from top to down.

Syntax:

Test

if condition:

statement (s)

elif condition:

statement (s)

elif condition:

statement (s)

else:

statement (s)


IF ELIF ELSE EXAMPLE


3. Nested if-else Statement

The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions. We can have many levels of nesting inside if.else statements. We can have an if...elif..else statement inside another if...elif..else statement. This is called nesting in computer programming

NESTED IF ELSE EXAMPLE




th, td { padding-top: 10px; padding-bottom: 20px; padding-left: 30px; padding-right: 40px; }
Previous
Next Post »

Click And Feel Like Free