Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.

Similar presentations


Presentation on theme: "Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied."— Presentation transcript:

1 Control Structures WHILE Statement Looping

2 S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied. …or Iteration

3 Sometimes we want to repeat a bit of code many times. We use loops to achieve this! For example, if you wanted to output the same statement 3 times, we would use a FOR loop. This is because we know the number of iterations. RULE: use a FOR loop if the number of iterations is known. What if we don’t know the number of iterations? For example, iterate until the user enters a specific value…

4 Condition ? Execute code block If condition is true If condition is false WHILE Loop Like if statements, while loops evaluate whether a condition is true or false! The block is evaluated (iterated) until the condition becomes false.

5 Looping mechanisms in python… In Python, there are 2 looping mechanisms… FOR LOOP WHILE LOOP LOOPS for a specified number of times! For example, output HELLO WORLD 3 times LOOPS for a an unknown number of times! For example, output HELLO WORLD ? times until user types quit.

6 >>>password = (‘dalriada‘) guess = input(‘Please enter password: ‘) while guess != password: print(‘Access denied') guess = input(‘Please try again’) print(‘Access granted’) Syntax while : Example, prompt the user to guess the password (dalriada)

7 What will be output here?

8 >>>number =1 while number < 5: print(number*number) number = number + 1 What will be output here?

9 >>>while x < 5: print(‘Hello’) What will be output here?


Download ppt "Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied."

Similar presentations


Ads by Google