Chapter 4: The Case Control Structure

Switch - Case, Page number: 137

In [1]:
#Variable declaration
i = 2

#Switch case statements
if i == 1: # case 1
    print "I am in case 1"
else:
    print "I am in case 2"# case 2
    print "I am in case 3"#case 3
    print "I am in default"# default
        
I am in case 2
I am in case 3
I am in default

Switch - Case, Page number: 138

In [2]:
#Variable declaration
i = 2

#Switch case statements
if i == 1: # case 1
    print "I am in case 1"
elif i == 2: # case 2
    print "I am in case 2"
elif i == 3: #case 3
    print "I am in case 3"
else: # default
        print "I am in default"
I am in case 2

The Tips and Traps a), Page number: 140

In [3]:
#Variable declaration
i = 22

#Switch case statements
if i == 121: # case 121
    print "I am in case 121"
elif i == 7: # case 7
    print "I am in case 7"
elif i == 22: #case 22
    print "I am in case 22"
else: # default
        print "I am in default"
I am in case 22

The Tips and Traps b), Page number: 140

In [4]:
#Variable declaration
c = 'x'

#Switch case statements
if c == 'v': # case 'v'
    print "I am in case v"
elif c == 'a': # case 'a'
    print "I am in case a"
elif c == 'x': #case 'x'
    print "I am in case x"
else: # default
        print "I am in default"
I am in case x

The Tips and Traps c), Page number: 141

In [5]:
ch = 'a'

#Switch case statements
if ch == 'a' or ch == 'A' : # case 'a' and case 'A'
    print "a as in ashar"
elif ch == 'b'or ch == 'B': # case 'b' and case 'B'
    print "b as in brain"
elif ch == 'c'or ch == 'C': # case 'c' and case 'C'
    print "c as in cookie"
else: # default
        print ("wish you knew what are alphabets")
a as in ashar

The Tips and Traps e) , Page number: 143

In [3]:
#Input from user
#i = raw_input("Enter value of i ")
i = 1

#Switch case statements
#print "Hello"
if i == 1 : # case 1
    j = 10
elif i == 2 :# case 2
    j = 20

Goto , Page number: 146

In [6]:
goals = 3

if goals <= 5 : #goto
    print  "To err is human!"  #label sos
else:
    print "About time soccer players learnt C" 
    print  "and said goodbye! adieu! to soccer" 
    
To err is human!

Goto , Page number: 148

In [8]:
#nested for loops
for i in range(1,4):
    for j in range(1,4):
        for k in range(1,4):
            if ( i == 3 and j == 3 and k == 3 ): #goto
                print "Out of the loop at last!"  # label out
                break
            else:
                print i, j, k 
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
Out of the loop at last!
In [ ]: