登录

Selection

IF statements

IF statements may or may not have an ELSE clause.

IF statements without an ELSE clause are written as follows:

IF <condition> THEN
    <statement(s)>
ENDIF

IF statements with an ELSE clause are written as follows:

IF <condition> THEN
    <statement(s)>
ELSE
    <statement(s)>
ENDIF

Note, due to space constraints, the THEN and ELSE clauses may only be indented by two spaces rather than three. (They are, in a sense, a continuation of the IF statement rather than separate statements).

//Example – nested IF statements
IF ChallengerScore > ChampionScore THEN
    IF ChallengerScore > HighestScore THEN
        OUTPUT ChallengerName, " is champion and highestscorer"
    ELSE
        OUTPUT ChallengerName, " is the new champion"
    ENDIF
ELSE
    OUTPUT ChampionName, " is still thechampion"
    IF ChampionScore > HighestScore THEN
        OUTPUT ChampionName, " is also the highest scorer"
    ENDIF
ENDIF

if

if {condition}:
	{statement1}
	{statement2}
	……

colon:

Don't forget colon: after {condition}!

indentation

We can use Tab or Space to input indentation before {statement1}!

if 5 > 3:
	print('I will output')
if 5 > 3 and 10 < 5:
	print('I will not output')
if True:
	print('I will output, too')

else

if {condition}:
	{statement1}
	{statement2}
	……
else:
	{statement3}
	{statement4}
	……
score = int(input())
if score >= 60:
	print(score, 'pass')
else:
	print(score, 'fail')
score = int(input())
if score >= 60:
	if score >= 90:
		print(score, 'excellent')
	else:
		print(score, 'pass')
else:
	print(score, 'fail')

CASE statements

CASE statements allow one out of several branches of code to be executed, depending on the value of a

variable.

CASE statements are written as follows:

CASE OF <identifier>
    <value 1> : <statement1>
                <statement2>
                ...
    <value 2> : <statement1>
                <statement2>
                ...
    ...
ENDCASE

An OTHERWISE clause can be the last case:

CASE OF <identifier>
    <value 1> : <statement1>
                <statement2>
                ...
    <value 2> : <statement1>
                <statement2>
                ...
    OTHERWISE : <statement1>
                <statement2>
                ...
ENDCASE

Each value may be represented by a range, for example:

<value1> TO <value2> :  <statement1>
                        <statement2>
                        ...

Note that the CASE clauses are tested in sequence. When a case that applies is found, its statement is executed and the CASE statement is complete. Control is passed to the statement after the ENDCASE. Any remaining cases are not tested.

If present, an OTHERWISE clause must be the last case. Its statement will be executed if none of the preceding cases apply.

//Example – formatted CASE statement
INPUT Move
CASE OF Move
    ꞌWꞌ : Position ← Position − 10
    ꞌSꞌ : Position ← Position + 10
    ꞌAꞌ : Position ← Position − 1
    ꞌDꞌ : Position ← Position + 1
    OTHERWISE : CALL Beep
ENDCASE

The case statement in Python was only released in Python3.10

grade = input()
match grade:
	case "A":
		print(score, 'excellent')
	case "B":
		print(score, 'pass')
	case "C":
		print(score, 'good')
	case _:
		print(score, 'fail')

登录