• No results found

03_Braching and Iteration.pdf

N/A
N/A
Protected

Academic year: 2020

Share "03_Braching and Iteration.pdf"

Copied!
16
0
0

Loading.... (view fulltext now)

Full text

(1)

Control Statements (Branch and Loops)

Nihar Ranjan Roy

[email protected], [email protected] Department of Computer Science and Engineering

GD Goenka University, Gurugram

(2)

Outline

1 If Statement

2 For Statement

3 break and continue statements

4 Loops with else clause

5 while loop

(3)

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")

(4)

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

(5)

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:

(6)

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:

(7)

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:

(8)

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):

(9)

For Statement

For statement II

#This program demonstrates usage of for loop #Example-2

words=["NIhar","Ranjan","Roy"] for w in words:

(10)

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:

(11)

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

(12)

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

(13)

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:

(14)

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

(15)

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()

(16)

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

References

Related documents

This raises the question of, ‘what might make somebody feel like an outsider?’ I remember a fellow in the army telling me he and his buddies would go to church on Sundays just

This is a full-time, salaried position that reports to the Marketing Manager and supports the Sales, Branding and Marketing activities for Catering, Dining Services and 19 East.. The

This thesis argues that an assessment of Turkey’s impact on the role of the EU in the world stage must take into account the three existing normative approaches for

We implemented this using a Symmetrix VMAX 10K storage array together with an existing VNX5300 storage array to form a bi-directional disaster recovery solution.. We achieved this

Aquatic Heteroptera Of the Lake Manguao Catchment, Palawan Aquatic Heteroptera Of the Lake Manguao Catchment, Palawan and New Rank Of Rhagovelia kawakamii hoberlandti Hungerford

His research interests include design and performance analysis of data centers, design of free space optical data center architectures utilizing the high capacity, high speed of

Correlació area de l'iBa / nombre d'especies citades de Mallorca, Menorca, Eivissa, Formentera, Cabrera, Dragonera, Corsega i Sardenya.. Correlation between the island

Condition: each time through the loop, condition is tested; if it’s true, the statement block, and the increment is executed, then the condition is tested again. When the