today we look at conditional statements and loop statements.
Preview:
1. Using while loop output 1 2 3 4 5 6 8 9 10
2. For all numbers of 1-100
3. All odd numbers in output 1-100
4. All even numbers in output 1-100
5, Beg 1-2+3-4+5 ... 99 of all numbers of the and
6. User Login (three chance retry)
First, conditional
Look at a very interesting topic.The topic is this:Please print the number from 1 to 1000, but you cannot use any of the loop statements or conditional statements.I was also blindfolded when I saw the problem.The first thing to think about is the recursive implementation,But how do you jump out of this loop?I thought about it, someone gave a hint that can be used with to judge:Look at the code first:#includ
First, the common condition statement
If statement -Use this statement to execute code only if the specified condition is true
if...else Statements -Executes code when the condition is true and executes other code when the condition is false
If...else if....else Statements -Use this statement to select one of several code blocks to execute
Switch Statement -Use this statement to select
Boolean variable
The following values are interpreted as false (false):False None 0 "" () {} []Everything else has been interpreted as true.>>> TrueTrue>>> FalseFalse>>> True = = 1True>>> False = = 0True>>> True + False +4243bool Function--used to convert other values, such as>>> bool ([])False>>> bool (' Hello,world ')True
Conditional statements
If Else elifIs and is not--determine if tw
Basic python tutorials-conditional and cyclic statements-python cyclic statements
Boolean variable
The following values are viewed as false by the interpreter ):
False None 0 "" () {} []
Everything else is interpreted as true.
>>> TrueTrue>>> FalseFalse>>> True = 1True>>> False = 0True>>> True + False + 4243
Bool function -- used to convert other values, such
b = false;
if (b) {//true
System.out.println ("The Spanish shark you sent to me");
} else {
System.out.println ("The Spanish Whale you gave me");
}
if (!b) {//false
System.out.println ("The Spanish shark you sent to me");
} else {
System.out.println ("Sister of Hong Kong Island, You sent me to the Spanish Whale ");}}
This is the two If-else statement, in this example the parentheses first judge the b=true, the output else inside the statement, the second If-else stat
Sequential structuresequence structure of the program: If there is no process control in the code, the program executes in the written format from the top and the next line, and then executes the next statement after the execution of a statement, without judging and jumping to the end of the program.If statementIf statements use a Boolean or Boolean value as the selection criteria, there are three structural forms: if translated into Chinese, that mea
EXECUTE statement 1, repeating it until the collection element has been taken out.How to make a ' collection ':Range ([Start,] stop [, step]): Generates an integer sequence with a range of [start,stop] and a step of step,start default to 0,step of 1. Note : The integer series generated by range () can only be changed by the for loop for each elementTwo keywords related to loops:Break statement: Jumps out of a for or while loop. You can only jump out of its nearest loop.Continue statement: Skips
In writing a shell script, there may be a situation when you need to take a path of two paths. So, you need to use conditional statements to make the program make the right decisions and perform the right actions.
The UNIX shell supports conditional statements, which perform different operations under differ
extreme performance optimizations
Traversing an array can use a normal loop instead of for-in and For-each.
Second, conditional statementsCommon conditional statements have If-else and switch-case, then when to use If-else, when to use the Switch-case statement?
Let's take a look at the code for a simple If-else statement:
if (value = = 0) {retu
这个程序 other than 12345 ;; Esacdone Continue jump out of this layer of cycleInstance:While:d o echo-n "Enter a number from 1 to 5:" read Anum case $aNum in 1|2|3|4|5) echo "The number you entered is $aNum!" ; *) echo "The number you entered is not between 1 and 5!" Continue the number between #如果输入的值不是1-5, the Contiuune will be transferred directly to the "echo-n" input number between 1 and 5 " echo" Game Over "; ; Esac
The format of the Stwith statement is generally as follows:1 Switch(expression) {2 CaseValue:statement13 Break;4 CaseValue2:statement25 Break;6 ....7 Casevalue:statement8 Break;9 default: statement;Each case indicates that if the value of expression equals case, the statament is executed.Keyword break causes the code to jump out of switch.If there is no keyword break, the code continues with the next situation. The keyword default is the resu
! Just become ESAC! It won't be hard to recite! In addition, each variable content of the program section will need two semicolon (;;) to represent the end of the program paragraph, which is very important! Why does it need to have * this variable content at the end? This is because if the user is not the input variable content one or two o'clock, we can inform the user of the relevant information Ah!In general, in the syntax of "case $ variable in", the "$ variable" is roughly two ways to get i
). The end of the course is to write the English case in reverse. It becomes ESAC. It won't be very hard to recite!Other than that. Each variable has a two semicolon (;) to represent the end of the program paragraph, which is very important! Why should there be * this variable content at the end? This is due to the assumption that the consumer is not the input variable content one or two o'clock. We can tell the user about the information.Generally speaking. Use the "Case $ variable in" syntax.
naturally written in the English language in turn! Just become ESAC! It won't be very hard to recite! In addition, two semicolons are required at the end of each program section of the variable content (;;) to represent the end of the program paragraph, which is very important! Why should there be * this variable content at the end? This is due to. Assume that the user is not the input variable content one or two o'clock, we can inform the user of the relevant information Ah!In general, use the
In front of you to explain a multi-choice ElseIf statement, although this statement can make a variety of judgments, but when used, need to write a lot of if statements, to our program readability inconvenience, in order to avoid the IF statement too much and improve the readability of our program, you can use the switch multiple judgment statement, In this chapter we will introduce you to the "switch statement". Switch statement syntax: switch (expr)
If statement
The IF statement can be used to form a branch structure that is judged according to the conditions given to determine which branch segment to execute.
There are three basic forms of if statements in C language
The first form of:
if (conditional expression)
{
statement 1
}
if (conditional expression)
{
statement 1;
}
This form
5-python Learning--Conditional statements
5-python Learning--Conditional statements
Conditional statement if Else form
If else conditional statement description
Test it.
This article describes the basics of a python conditional statement and a looping statement, as described in " topic I. Basics of Functions " . The main contents include:1. Conditional statement: Includes single, dual, and multi-branch statements, If-elif-else2. Loop statement: While use and simple network brush spider crawler3. Looping
Python Topic 2: Basic knowledge of conditional and cyclic statements, and basic knowledge of python
Previously, I talked about "Topic 1. Basic knowledge of functions". This article describes the basic knowledge of Python conditional statements and cyclic statements. The main
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.