Loop Python
Loop Python
Statement
Statement are the instructions given to the computer to perform any kind of action.
Types of Statement
1. Empty Statement
The statement which does nothing. In python an empty statement is pass statement. It takes the following form :
pass
wherever Python encounters a pass statement, Python does nothing and moves to next statement.
2. Simple Statement
print(“Programming”)
3. Compound Statement
A compound statement represents a group of statements executed as a unit. It takes following form :
SEQUENCE
The sequence construct means the statements are being executed sequentially.
Java execution starts with first statement, and then each statement is executed in turn.
STATEMENT 1
STATEMENT 2
STATEMENT 3
P a g e 1 | 11
SELECTION
The selection construct means the execution of statement(s) depending upon a condition-test.
If a condition evaluates to true, a course of action (a set of statements) is followed otherwise another course of action (a
different set of statements) is followed.
One Course-of-action
TRUE
Condition? STATEMENT 1 STATEMENT 2
FALSE
Another STATEMENT 3
Course-
of-
action
STATEMENT 4
ITERATION
The iteration construct means repetition of a set of statements depending upon a condition-test. Till the time a condition
is true, a set of statements are repeated again and again. As soon as the condition becomes false, the repetition stops.
The iteration construct is also called looping construct.
FALSE
Condition? EXIT
TRUE
Condition is
evaluated again, STATEMENT 1
And if true,
given set of
statements are STATEMENT 2
repeated.
P a g e 2 | 11
The set of statements that are repeated again and again is called the body of loop.
- Helps in reading and analysing the problem which needs to be solved by programming.
ALGORITHM
- It is a step by step procedure (well defined instructions) to solve a given problem. For instance, the
algorithm for addition of two numbers is :
1. Input first number.
2. Input second number.
3. Add first number with second number and store the result in third variable.
4. Display the result.
FLOWCHARTS
- It is a graphical representation of an algorithm. Following symbols are used in flowcharts:
- Use Data symbol for Input/Output Operation (taking input and showing output)
- Use Process symbol for any type of computation and internal operations like initialization,
calculation etc.
- e.g. flowchart for addition of two numbers will be :
P a g e 3 | 11
PSEUDOCODE
- It is an informal language that helps programmers describe steps of a program’s solution without using any
programming language syntax.
DECISION TREES
- These are a way of presenting rules in a hierarchical and sequential structure, where based on the hierarchy
of rules, certain outcomes are predicted.
P a g e 4 | 11
The if statement
if statement tests a condition and if the condition evaluates to true, it carries out some instructions and does
nothing in case condition evaluates to false.
if <conditional/realtional expression> :
statement
[statement]
where a statement may consist of a single statement, a compound statement or just the pass statement.
e.g.1.
if ch == ‘ ‘ :
print(“character is space”)
The above code will check whether the character variable ch stores a space or not ; if it does, then
condition ch == ‘ ‘ evaluates to true and print statement given in second line will be executed.
If however, variable ch does not store a space i.e., the condition ch == ‘ ‘ evaluates to false, then nothing will
happen, no statement will be executed.
e.g.2.
ch = input(“Enter a single character:”)
if ch >= ‘0’ and ch < ‘9’ :
print(“You entered a digit”)
The above code after getting input in ch, compares its value; if the value falls between characters ‘0’ to
‘9’ i.e. the condition evaluates to true , and thus execute the statement in the if – body that is , it will print a
message saying ‘You entered a digit’
e.g.3.
if grade == ‘A’ :
print(“You did well”)
e.g.4. Program to find the eligibility of a person for voting. Display message “Eligible” or “Not Eligible for
voting” based on age entered by the user. (using if statement)
P a g e 5 | 11
e.g.5. Program that takes a number and checks whether the given number is odd or even.
e.g.1. if a > 0 :
print(a, “is a positive number”)
else :
print(b,”is a negative number”)
e.g.2. Program to accept two numbers and print the largest of two numbers.
x = float(input(“Enter first number:”))
y = float(input(“Enter second number:”))
if x > y :
P a g e 6 | 11
print(“largest number is”,x)
else :
print(“largest number is”,y)
e.g.3. Program to find the eligibility of a person for voting. Display message “Eligible” or “Not Eligible for
voting” based on age entered by the user.
e.g.4. Program that takes a number and checks whether the given number is odd or even.
e.g.6. Program to display a menu for calculating area of circle or perimeter of a circle.
radius = float(input(“Enter radius of the circle:”))
print(“1. Calculate Area”)
print(“2. Calculate Perimeter”)
choice = int(input(“Enter your choice(1 or 2):”))
if choice == 1:
area = 3.14 * radius * radius
print(“Area of circle with radius”, radius, “is”, area)
else
perm = 2 * 3.14159 * radius
print(“Perimeter of circle with radius”, radius, “is”, perm)
P a g e 7 | 11
The if – elif Statement
e.g. 1.
if runs >= 100 : Python will test this condition in case
print(“Batsman scored a century”) previous condition (runs >=100) is false
elif runs >= 50 :
print(“Batsman scored a fifty”)
else : This block will be executed when both
print(“Batsman has neither scored a century nor a fifty”) the if condition (i.e. runs >=100 and
runs >=50 ) are false.
e.g. 2. Program to accept a number from user and check whether the number is positive,negative or zero.
P a g e 8 | 11
result = num1 * num2
elif op == ‘/’ :
result = num1 / num2
elif op == ‘%’ :
result = num1 % num2
else :
print(“Invalid operator!!”)
print(num1,op,num2, ‘=” , result)
Nested if Statement
- A Nested if is an if that has another if in its if’s body or in elif’s body or in its else’s body.
P a g e 9 | 11
STORING CONDITIONS
To make program readable, conditions in the if statement can be stored in name and then can be used in the if
statements.
e.g.
pos = a > 0
neg = a < 0
num = float(input(“Enter a number:”))
if neg :
print(num , “is a negative number”)
elif pos :
print(num , “is a positive number”)
else :
print(num , “is equal to zero”)
Practice Questions:
P a g e 10 | 11
print(“C” , end = ‘ ‘)
print(“D”)
Q5. What is the error in following code ? Correct the code:
weather = ‘raining’
if weather = ‘sunny’ :
print(“wear sunblock”)
elif weather = “snow” :
print(“going skiing”)
else :
print(weather)
Q6. What is the output of the following lines of code ?
if str(0) == ‘zero’ :
print(0);
elif str(0) == ‘0’ :
print(str(0))
else :
print(“none of the above”)
P a g e 11 | 11