Skip to main content
Skip to content

Lesson 5 of 12

Decisions with agar and warna (if else)

Make your program choose: run one block when a condition holds, another when it does not, and chain as many checks as you need.

9 min

agar: run code only if

agar means "if". Put a condition in brackets, then a block in curly braces. The block runs only when the condition is truthy. The braces are required, even for a single line.

Expected output

Press Run to try it

Pass ho gaye!

warna: otherwise

warna means "otherwise". It follows the closing brace of an agar and runs when the condition was false. Exactly one of the two blocks runs, never both.

Expected output

Press Run to try it

7 odd hai

warna agar: more than two choices

Chain checks with warna agar. AltFLang tests them top to bottom and runs the first block whose condition is true, then skips the rest. A final warna catches everything that matched nothing. Order matters: put the strictest check first, otherwise a broad check like marks >= 33 would swallow a 95.

Expected output

Press Run to try it

Grade A

Conditions inside conditions

A block can contain another agar. That is called nesting. Often && reads better than two nested levels, but nesting is useful when the inner check only makes sense after the outer one passed.

Expected output

Press Run to try it

Vote de sakte ho

Practice exercise

Given temp = 38, print Garmi if temp is above 35, Mausam badhiya if it is from 20 to 35, and Thand otherwise. Then change nothing else — the expected output is Garmi.

Expected output

Garmi
Open in playground
Show solution
solution.altf
rakho temp = 38;
agar (temp > 35) {
  bol("Garmi");
} warna agar (temp >= 20) {
  bol("Mausam badhiya");
} warna {
  bol("Thand");
}