Control Statements (Branch and Loops)
Nihar Ranjan Roy
[email protected], [email protected] Department of Computer Science and Engineering
GD Goenka University, Gurugram
Outline
1 If Statement
2 For Statement
3 break and continue statements
4 Loops with else clause
5 while loop
If Statement
if statement
An ‘if’ statement is a selection control statement based on the value of a given boolean expression.
#program to add two numbers no=7
if no>5:
print("Number is greater than 5") else:
print("No is less than 6")
If Statement
Problem I
Write a program that accepts % of marks from a user and displays grades as per the following rule
1 marks>=90 grade is A
2 marks >=80 grade is B
3 marks>=70 grade is C
4 marks>=60 grade is D
If Statement
Solution
#nested if
marks=70
if marks>=90:
print("Grade is A") else:
if marks>=80:
print("Grade is B") else:
if marks>=70:
print("Grade is C") else:
if marks>=60:
print("Grade is D") else:
If Statement
Multiway selection-elif
The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation.
There can be zero or more elif parts to an if statement, and the else part is optional.
#This program demonstrates usage of if...elif x=int(input("Please enter an integer"))
if x>0:
print("Positive") elif x==0:
print("Number is Zero") else:
If Statement
Solution to last problem
#nested elif
marks=70
if marks>=90:
print("Grade is A") elif marks>=80:
print("Grade is B") elif marks>=70:
print("Grade is C") elif marks>=60:
print("Grade is D") else:
For Statement
For statement I
The for statement in Python differs a bit from what you may be used to in C.
Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.
#This program demonstrates usage of for loop #for x in range(5):
For Statement
For statement II
#This program demonstrates usage of for loop #Example-2
words=["NIhar","Ranjan","Roy"] for w in words:
break and continue statements
break and continue Statements I
break and continue statements work similar to C language
#This program demonstrates usage of break statement # immediate loop breaks when i==5
for i in range(10): if i==5:
break and continue statements
break and continue Statements II
Write a program to find the sum of all the odd numbers from a given list
break and continue statements
break and continue Statements III
continue statement skips rest of the statements in the loop if the condition is true.
#This program demonstrates usage of continue statement
num=[1,2,4,5,6,8] sum=0
for i in num: if i%2==0:
continue print(i)
sum=sum+i
Loops with else clause
Loops with else clause
Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
#Demonstration of loop with else statement. for n in range(2, 10):
for x in range(2, n): if n % x == 0:
print(n, 'equals', x, '*', n//x) break
else:
while loop
While loop
while loop in python is similar to C
#Demonstrates usage of while statement # Generates Fibonacci series
# the sum of two elements defines the next a, b = 0, 1
while loop
Defining functions in python
The keyword def introduces a function definition.
It must be followed by the function name and the parenthesized list of formal parameters.
The statements that form the body of the function start at the next line, and must be indented.
def fib(n): # argument to the function
"""function to generate Fibonacci series upto n"""
a, b = 0, 1
while a <n:
print(a, end=' ') a,b=b,a+b
print()
Exercises
Exercises
1. Write a program to find sum of the digits of an integer number.
2. Write a program to find reverse of an integer number.
3. Write a program using for loop that displays the numbers of a series
1,4,9,16,25,...n
4. Write a program for finding sum of the following series using while
loop
i. x+x2!2 +x3!3 +...n
ii. 1 +x+x2+x3+...n
5. Write a program to accept three numbers from user and check if first