Python Process Control instance code

Source: Internet
Author: User

First, we will introduce the IF-else Condition Statement. If statements are used to selectively execute specific expressions based on their authenticity. Program Block to control the process of the program. Use the same language as Java. For else if, there is an abbreviated Elif method.
For example: CopyCode The Code is as follows: if x> 3:
Print ("Greater ")
Elif x = 3:
Print ("EQ ")
Else:
Print ("small ")

Next we will introduce the while statement. The while statement repeats a specific program block when the condition expression is true.
First, let's take a look at the sample program and then explain it:Copy codeThe Code is as follows: x = int (input ("enter a integer :"))
While X! =-1:
Print (X)
X = int (input ("next number :"))
Else:
Print ("end ")
Print ('over ')

In this Code, as long as the value of X is not equal to-1, it will be executed repeatedly. Compared with C/C ++/Java, the else statement is special. Here, else is an optional statement. When the conditional expression is false and jumps out of the while loop, the program block under the else statement is executed.
Finally, let's take a look at the for loop. For... In is another type of loop statement in Python. The main purpose is to iterate the Access Object Sequence. The usage is as follows:Copy codeThe Code is as follows: for X in range (1, 5 ):
Print (X)
Print ('over ')

For Loop statements, you must use the methods to jump out of the current loop and exit the loop. in Python, use the continue and break statements. The use of these two syntaxes is the same as that of C/C ++/Java.
With the above three methods, you can complete all the process control work in python!

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.