Decision Making & Loops - Python Programming Language

Decision Making

Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.

Following is the general form of a typical decision making structure found in most of the programming languages−

Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.
Python programming language provides following types of decision making statements.

  1. if statements
  2. if....else statements
  3. nested if statements
  4. ifel statements

Example:

1.if and if....else statements

Program:

a = int ( input ( "Enter a number: " ) )
b = int ( input ( "Enter another number: " ) )
if a > b :
    print ( "First number is greater than second number " )
else :
    print ( "Second number is greater than first number " )

Output:

Enter a number: 12
Enter another number: 13
Second number is greater than first number

2.nested if statements

Program:

a = input("Enter a number: ")
b = input("Enter another number: ")
if a>b:
    print("First number is greater then Second number")
elif a==b:
    print("Both number is equal")
else:
    print("Second number is greater than First number")


Loops

A loop statement allows us to execute a statement or group of statements multiple times.The following diagram illustrates a loop statement −

Python programming language provides the following types of loops to handle looping requirements.

  1. while loop
  2. for loop
  3. nested loop

1. The while loop:

Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.

Syntex:

while Condition:
    Statements
    exit condition

For example:

print("Print hello 10 time from the help of while loop")
a=int(1)
while a<=10
    print("Hello",+a)
    a=a+1

Output:

Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

2. The for loop:

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
Syntex:

for Condition:
    Statements

For example:

for i in range(1,5):
    print(i)
    print("\n")

Output:

1

2

3

4

3.The nested loop:

You can use one or more loop inside any another while, or for loop.

For example:

for i in range(1,5):
    for j in range(i,5):
        print(i);
    print("\n")

Output:

1
1
1
1

2
2
2

3
3

4

Loop Control Statements

The Loop control statements change the execution from its normal sequence. When the execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Python supports the following control statements.

  1. break statement
  2. continue statement
  3. pass statement

1. break statement:

Terminates the loop statement and transfers execution to the statement immediately following the loop.

2. continue statement:

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

3. pass statement:

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute..


Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.