#How to set an if function:
if Condition:
suite (if the condition is True)
statement (no matter the condition is True or False)

#Introduction of elif and else:
if condition1:
suite1 (if condition1 is True)
elif condition2: (if condition1 is False)
suite2 (if condition2 is True)
else: (if condition1 & 2 are both False)
suite3 (if condition1 & 2 are both False)
statement (no matter condition is True or False)

#For loop:


#While loop:

#run the conditional suite only when the condition is True
| Loop Type | Description |
|---|---|
| while loop | Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. |
| for loop | Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
| nested loops | You can use one or more loop inside any another while, for or do..while loop. |
| Control Statement | Description |
|---|---|
| break statement | Terminates the loop statement and transfers execution to the statement immediately following the loop. |
| continue statement | Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |
| pass statement | The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. |
# How to run a if / loop function that may raise Errors?
Handling an exception
If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.
Syntax
Here is simple syntax of try….except…else blocks −
try: You do your operations here; ...................... except ExceptionI: If there is ExceptionI, then execute this block. except ExceptionII: If there is ExceptionII, then execute this block. ...................... else: If there is no exception then execute this block.
finally: This would always be executed. ......................
Raising an Exceptions
You can raise exceptions in several ways by using the raise statement. The general syntax for the raise statement is as follows.
Syntax
raise [Exception [, args [, traceback]]]
User-Defined Exceptions
Python also allows you to create your own exceptions by deriving classes from the standard built-in exceptions.
Here is an example related to RuntimeError. Here, a class is created that is subclassed from RuntimeError. This is useful when you need to display more specific information when an exception is caught.
In the try block, the user-defined exception is raised and caught in the except block. The variable e is used to create an instance of the class Networkerror.
class Networkerror(RuntimeError): def __init__(self, arg): self.args = arg
So once you defined above class, you can raise the exception as follows −
try: raise Networkerror("Bad hostname") except Networkerror,e: print e.args
Exercise 3:
分別用for,while迴圈各寫⼀個nxn的乘法表 程式 可以讀取使用者輸入的值 n, n>1
輸出樣式: (n=1~9)

Reference:
Coursera
PPT from NTU SPECS course
Tutorialspoint