Monday 27 May 2019

Unconditional statements


Assignment:
1.     Wap to display simple calculatr:  print(“1: addition”), print(“2: sub”), print(“3: Multiplication”),print(“4. Division), print(“. Enter between 1 to 4”

Unconditional Statements:
Example:1
a=[10,20,30,40]
for i in a:
    if i>20:
        print("to apply this post below 20 years only")
        break;
    print(i)
"""output:
10
20
to apply this post below 20 years only
>>> """

Unconditional statements allow you to direct the program's flow to another part of your program without evaluating conditions.

Loops perform set of operations repeatedly until the control variable fails to satisfy the test condition
Some times, while executing loop it becomes desirable to skip a part of loop.

(1.)           Break  (2.) continue (3.) pass (4.)  return

break statement
Break statement is used to come out of the loop or out of the suite. It can be used in for,while, if statements. Break is a keyword

# program to display all the elements before number 88
a=[11, 9, 88, 10, 90, 3, 19]
for i in a:
   print(i)
   if(i==88):
               print("The number 88 is found")
               print("Terminating the loop")
               break
"""output:
11
9
88
The number 88 is found
Terminating the loop
>>> """

(2.)           continue:
We can use continue statement to skip current iteration and continue next iteration. Continue is a keyword. When continue is executed, the next iteration (repetition) is executed.

#To print odd numbers in the range 0 to 9
for n in range(10):
    if n%2==0:
        continue
    print(n)
"""output:
1
3
5
7
9
>>>
"""

# program to display all the elements before number 88
a=[11, 9, 88, 10, 90, 3, 19]
for i in a:
   print(i)
   if(i==88):
               print("we can not process this roll number:",i)
               continue
"""output:
11
9
88
we can not process this roll number: 88
10
90
3
19
>>>
>>> """

(3.)           Pass statement:
The pass statement does not do anything. It is used with ‘if’ statement or inside a loop to represent no operation. We use pass statement when we need a statement syntactically but we do not want to do any operation.

a=[20, 11, 9, 66, 4, 89, 44]
for n in a:
    if n%2 == 0:
        pass
    else:
        print(n)

"""output:
11
9
89
>>> """


It is just like null statement in Java.

Example: print('{:4}'.format(2*3),end="")
   6(after 4 spaces)

#displaying nos from 1 to 25 in 5 rows and 5 colums
for a in range(1,6):
    for b in range(1,6):
        print('{:3}'.format(a*b),end='')
    print()

"""
  output:
  1  2  3  4  5
  2  4  6  8 10
  3  6  9 12 15
  4  8 12 16 20
  5 10 15 20 25

Related video: https://www.youtube.com/watch?v=OdnfITud_6I&feature=youtu.be


Next Topic: Assessment 8
Previous Topic: Nested loops

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...