Basic knowledge review--Process Control

Source: Internet
Author: User

The process of changing the order of program runs through conditional or circular statements is called Process Control.

Conditional statements

Conditional statement: Used to change the execution flow of the program, where the else code block is optional.
1.if/else

1PWD = Raw_input ("What ' s the password?")2 ifPWD = ='Apple':3     Print "loging on ..."4 Else:5     Print "Password error!"6 Print " All done"

2. Upgrade Version If/elif

1PWD = Raw_input ("How is old is you ?")2 ifAge <= 12:3     Print ' Free'4 elif< Age < 16:5     Print 'Child Fare'6 Else :7     Print 'Adult Fare'

Looping statements

Looping statements: Used for repeating code blocks, mainly for loops and while loops, where the For loop is easier to use than while and while is more flexible.

The For loop is more suitable for situations where the condition is known and the number of loops is fixed, while loops are more suitable for situations where conditions are uncertain, while loops are more than one variable declaration in the For Loop memory.


1.for Loop, execute n times

1  for  in range:2     print i

2.while Loop, executes n+1 times until the last one is False

1 i = 02 while I <:3     print  i  4     i = i + 1

3. Interrupt loop, continue interrupt this cycle, break interrupts the entire cycle

1 #when the loop executes to I = 2, the If condition is set, the continue is triggered, the execution is skipped (no print is executed), and the next execution is resumed (i = 3)2  forIinchRange (10):3     ifi = = 2:4         Continue5     PrintI6 7 #when the loop executes to I = 2, the If condition is set, the break is triggered, the entire loop ends8  forIinchRange (10):9     ifi = = 2:Ten          Break One     PrintI

Basic knowledge review--Process Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.