登录

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
       <statements>
ENDIF

IF statements with an ELSE clause are written as follows:

IF <condition>
    THEN
        <statements>
    ELSE
        <statements>
ENDIF

Note that the THEN and ELSE clauses are only indented by two spaces. (They are, in a sense, a continuation of the IF statement rather than separate statements.) When IF statements are nested, the nesting should continue the indentation of two spaces.

Example – nested IF statements

IF ChallengerScore > ChampionScore
  THEN
    IF ChallengerScore > HighestScore
      THEN
        OUTPUT ChallengerName, " is champion and highest scorer"
      ELSE
        OUTPUT Player1Name, " is the new champion"
    ENDIF
  ELSE
    OUTPUT ChampionName, " is still the champion"
    IF ChampionScore > HighestScore
      THEN
        OUTPUT ChampionName, " is also the highest scorer"
ENDIF ENDIF

if

python

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

python

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> : <statement>
    <value 2> : <statement>
    ...
ENDCASE

An OTHERWISE clause can be the last case:

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

It is best practice to keep the branches to single statements as this makes the pseudocode more readable. Similarly, single values should be used for each case. If the cases are more complex, the use of an IF statement, rather than a CASE statement, should be considered.

Each case clause is indented by two spaces. They can be considered as continuations of the CASE statement rather than new statements.

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
    ꞌEꞌ : Position ← Position + 10
    ꞌAꞌ : Position ← Position – 1
    ꞌDꞌ : Position ← Position + 1
    OTHERWISE OUTPUT "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')

登录