Control Flow in Python and control flow in Python

Source: Internet
Author: User

Control Flow in Python and control flow in Python

1. if statement:

The if statement is used to test a condition. if the condition is true, we run a statement (called the if-block). Otherwise, we will process another statement (called the else-block ). The else clause is optional.

You can also use the elif statement, which combines two associated if else-if else statements into an if elif else statement to simplify the program and reduce the number of indentation required.

Note: whether it is an if statement, an elif statement, or an else statement, it is followed by a colon.

Elif and else statements are always optional.

The if statement can be written again, which becomes the nested if statement.

Note: There is no switch statement in Python.

2. while statement:

If one condition is true, the while statement allows you to execute one statement repeatedly. A while statement is an example of a loop statement. The while statement has an optional else clause.

Note: The while statement is followed by a colon and the else statement is optional.

3. for statement:

For... in is another loop statement that iterates over a sequence of objects, that is, each item in the sequence is used one by one.

Similarly, the for and else statements must be followed by colons. else statements are optional.

The for... in loop applies to any sequence. We often use a list of numbers generated by the built-in range function, but in a broad sense, we can use a sequence of any types of objects!

4. break statement:

The only statement of a break statement is to terminate the loop, that is, it jumps out of the loop when a break is encountered.

An important note is that if you terminate a for or while LOOP, no corresponding loop else block will be executed. That is, they think that their else statements are part of the loop and also jump out.

5. continue statement:

The continue statement is used to tell Python to skip the remaining statements in the current loop block and continue the next loop.

Related Article

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.