Python
Python Control flow
| Control flow | Example | Answer | Description |
|---|---|---|---|
| while loop | x=0 while x<=3: x=x+1 print(x) | 1 2 3 4 | |
| while loop | x=0 while x<=3: x=x+1 if x==3: continue print(x) | 1 2 4 | Continue keyword continues with the next iteration. In this case 3 is not printed |
| while loop | x=0 while x<=3: x=x+1 if x==3: break print(x) | 1 2 | Break statement breaks the iteration and exits the loop. In this case after 1 and 2 are printed, the iteration stopped |
| for loop | for x in range(4): x=x+1 print(x) | 1 2 3 4 | |
| for loop | for x in range(4): x=x+1 if x==3: continue print(x) | 1 2 4 | Continue keyword continues with the next iteration. In this case 3 is not printed |
| for loop | for x in range(4): x=x+1 if x==3: break print(x) | 1 2 | Break statement breaks the iteration and exits the loop. In this case after 1 and 2 are printed, the iteration stopped |
| for loop | for x in range(5, 10, 2): print(x) | 5 7 9 | Starting from 5 and until 10 increment by 2 and print |
| If statement | x=8 if x>0 and x<=3: print(“less than 4”) elif x>3 and x<=7: print(“less than 8”) else: print(“greater than 7”) | greater than 7 |