Chapter 5
Chapter 5
Introduction:-
Generally a program executes its statements from beginning to end . But
not many programs execute all their statements in strict order from
beginning to end.
Programs depending upon the need can choose to execute one of the
available alternatives or even repeat a set of statements .
Statement
Statements are the instructions given to the computer to perform any kind of
action( include data movements , making decisions or repeating actions)
Type of Statement
Empty Statement
Simple Statement
Compound Statement
Empty Statement
current letter h
print'use of pass'
current letter o
print'current letter',
current letter n
letter
Simple Statement
Sequence- The sequence construct means the statements are being executed sequentially.
Selection-
Iteration Statements
Algorithms Tools are :---(i) pseudocode , flow charts , or decision trees and
tables.
Flowcharts
Use Data symbol for Input/Output (I/O) operation(taking input and showing output).
Use Process symbol for any type of computation and internal operations like
initialization, calculation etc.
Use Subprocess symbol to invoke a procedure written already.
else:
sum1= sum=2=0
sum1=num1+num2+num3
sum2+=num1
sum2+=num3
print(“Numbers are”,num1,num2,num3)
then it is a century
then it is a fifty
else
num1= float(input(“Enter first number :”))
num2=float(input(“Enter second
batsman hasnumber:”)) Enter
neither scored century nor first number:5
fifty.
op=input(“Enter operator[+-*/%]:”)
result=0 Enter second number:2
if op==’+’
result=num1+num2 Enter operator[+ - * / %]:*
elif op==’_’:
5.0 * 2.0=10.0
result=num1-num2
elif op==’*’: ===========RESTART===============
result=num1* num2
elif op==’/’: Enter first number : 5
result=num1/num2
Enter second number : 2
elif op==’%’:
Enter operator [+ - * / %] : /
result=num1% num2
5.0/2.0 = 2.5
else:
print(“Invalid operator!!”)
else:
if x< y:
else:
The in operator tests if a given value is contained in a sequence or not and returns
True or False accordingly.
In operator used with range( ) in for loops.
Eg
The for loop of Python is designed to process the items of any sequence , such
as a list or a string one by one.
The general form of for loop
This is the body of the for loop. All
statements in the body of the loop
will be executed for each value of
Eg for a in [1,4,7]: loop variable a , i.e firstly for a=1;
then for a =4 and then for a=7
print(a)
The loop variable a
. Variable a will be print(a*a)
assigned each
value of list one by
one, i.e for the
first time a will be
1, then 4 and then
7
Iteration :--- Each time , when the loop –body is executed is called an iteration.
Out put
16
7
5 x1=5
49
Program to print table of a number (5)
5x2=10
Num=5
5 x3=5
for a in range(1,11):
print(num,’x’,a,’=’,num*a) 5x4=10
The above code will print the output as shown here: 5 x5=5
5x6=10
5 x7=5
A while loop is a conditional loop that will repeat the instructions within itself
as long as a conditional remains true(Boolean True or truth value true)
While<logical expression>:
loop-body
where the loop –body may contain a single statement or multiple
statement or an empty statement (i.e pass statement).
The loop iterates while the logical expression evaluates to true. When
the expression becomes false, the program control to the line after the
loop –body.
a=5 Output:
print(“hello”,a) Hello 2
Break statement
The break statement enables a program to skip over a part of the code .
A break statement terminates the very loop it lies within . Execution resumes at
the statement immediately following the body of the terminated statement.
Continue Statement
The continue statement is another jump statement like the break statement as
both the statements skip over a part of the code. But the continue statement is
somewhat different from break .
Instead of forcing termination , the continue statement forces the next iteration
of the loop to take place, skipping any code in between.