ScripticXLearn programming visually
Platform
Problems
Community
Leaderboard
Learn
Docs
MiniScript+ basicsVariablesOperators and expressionsInput and outputConditions and decisionsLoopsStringsSums, counts, and extremaDigits of a numberDivisibility and prime numbersNumeric sequences and recurrencesNumber basesFunctions and parametersRecursion
Examples
Basic programsConditionsInput and outputLoopsAlgorithmsWorking with textDigit problemsBase conversionsFunctions and recursionEncode text with a key
What's newHelpContact
Loading…

Documentation

MiniScript+

DocumentationMiniScript+
Getting started
MiniScript+ basics
Language basics
VariablesOperators and expressionsInput and outputConditions and decisionsLoopsStrings
Elementary algorithms
Sums, counts, and extremaDigits of a numberDivisibility and prime numbersNumeric sequences and recurrencesNumber bases
Functions
Functions and parametersRecursion
Documentation/Conditions and decisions

Conditions and decisions

Choose which instructions run with IF, THEN, and ELSE.

On this page
IF and ELSEMore than two casesValidate before calculatingCommon mistakesPractice

A condition answers a question with TRUE or FALSE. It lets a program choose a calculation or message. Read operators and input/output first.

IF and ELSE#

Write a condition after IF, followed by THEN. Close the block with END. An optional ELSE branch runs only when the condition is false.

MS+MiniScript+
1INPUT n2IF n MOD 2 == 0 THEN3  PRINT "even"4ELSE5  PRINT "odd"6END

For 8, the remainder on division by two is zero, so the output is even. For 9, the output is odd. Exactly one branch runs. Zero is even too.

More than two cases#

To distinguish negative, zero, and positive values, check zero inside the branch that has already excluded negative numbers.

MS+MiniScript+
1INPUT n2IF n < 0 THEN3  PRINT "negative"4ELSE5  IF n == 0 THEN6    PRINT "zero"7  ELSE8    PRINT "positive"9  END10END

Each IF needs its own END. Indentation shows structure but does not replace these delimiters. Separate independent IF statements may execute several blocks; an IF with ELSE chooses one branch.

Validate before calculating#

Check an operation's restrictions before executing it. Do not calculate an average when there are zero values, or divide by an input without checking the divisor.

MS+MiniScript+
1INPUT total2INPUT count3IF count > 0 THEN4  PRINT total / count5ELSE6  PRINT "There are no values."7END

With total = 18 and count = 4, the output is 4.5. With count = 0, the message is printed and the division never runs.

Common mistakes#

  • = assigns; == compares.
  • An interval needs two comparisons: n >= 10 AND n <= 20.
  • Included boundaries use <= or >=; excluded boundaries use < or >.
  • Compare numbers with numbers; preserve "8" as text only when that is intentional.

Practice#

  1. Print the larger of two inputs. Include equal inputs in your tests.
  2. Print passed for a grade in [5, 10]; handle invalid grades separately.
  3. Test parity with 0, 1, 2, and -3. Continue with loops.
PreviousInput and outputNextLoops

On this page

IF and ELSEMore than two casesValidate before calculatingCommon mistakesPractice