Five conditional statements

<1.if conditional statements

Syntax: i, syntax: if conditional expressions: a block of code if the if control multiple statements, all statements in strict accordance to indent.

Code block: a block of code is stored in this set of codes, the same block of code is executed or not executed either. Code block is a mechanism for grouping the code.

Process execution: if statements, when executed, will first be evaluated for the determination of the conditional expression, statements (block) after If True, If is performed if False, not executed.

if :Ture
print(0)
if 2<3 :
print(0)
print(1)
<2.input函数

input function is a function of the input, the use of the program will pause, waiting for user input, enter after carriage returns continue with the following procedure, you can then add a prompt statement input function, enclosed by single quotes, input ( 'Enter'), input function str type of return value.

<3.if-else statements

grammar:

  if conditional expression:

      Block

  else :

      Block

Execution flow: if-else statements, when executed, first a condition expression is evaluated if the judgment, after If True, if block is executed, if the False, else block is executed.

<5.if-elif-else statements

grammar:

  if conditional expression:

      Block

  elif conditional expression:

      Block

  elif conditional expression:

      Block

 …………………………

  else :

      Block

Implementation process:

  if-elif-else statements, when executed, will sequentially from the top down condition expression is evaluated determination, if the expression evaluates to True, the execution of the current block of code, and then ends the statement, if the expression evaluates to False then determine down until you find True date, if all expressions are False, then the code after the else block execution.

  The if-elif-else there will only be a block of code is executed.

<4while statement

loop statement

You can specify the number of times specified code block repeatedly performed

while loop

grammar:

  while conditional expression:

      Block

 else:

      Block

 

Execution flow: while statement is executed, the conditional expressions will first be evaluated while the determination, if the determination result is True, the loop body is executed (code blocks), the loop is finished, the condition expression is evaluated to continue value determination, and so on until the determination result is False, the loop terminates.

Example: demand within the sum of all even 100

0 = I
Result = 0
the while I <100:
I + 2 =
Result + = I
Print
 
(Result) ----------------
Copyright: This is CSDN bloggers "error floor eventually youth, freedom sooner or later the rest of chaos original article ", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/gfhfbbvbvb/article/details/102073769

Guess you like

Origin www.cnblogs.com/xiaoqinxinPython/p/11672202.html