1 / 9

Control Structures

Control Structures. WHILE Statement. Looping. S. E. Q. U. REPITITION. …or Iteration. E. N. …a step or sequence of steps that are repeated until some condition is satisfied. C. E. Sometimes we want to repeat a bit of code many times. We use loops to achieve this!

janiej
Download Presentation

Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structures WHILE Statement Looping

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

  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. WHILE Loop Condition ? If condition is true Execute code block Like if statements, while loops evaluate whether a condition is true or false! The block is evaluated (iterated) until the condition becomes false. If condition is false

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

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

  7. What will be output here?

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

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

More Related