यदि / नत्र Language
yadi and natra
Run code only when a condition holds.
yadi means “if” and natra means “otherwise”.
rakha umer = 20;
yadi (umer > 17) {
bhana("Voting garna milchha.");
} natra {
bhana("Ajhai sano.");
}
Voting garna milchha.
The condition goes in parentheses, and each block goes in braces. natra is optional.
More than two choices
Put another yadi inside the natra block:
rakha ank = 72;
yadi (ank > 79) {
bhana("Distinction");
} natra {
yadi (ank > 59) {
bhana("First division");
} natra {
bhana("Pass");
}
}
First division
yadi gives back a value
A yadi is an expression: it produces the value of the last line in the block that ran. You can store that value.
rakha a = 8;
rakha b = 13;
rakha thulo = yadi (a > b) { a; } natra { b; };
bhana("Thulo:", thulo);
Thulo: 13
If the condition is false and there is no natra, the result is khali.
No semicolon after the brace
A yadi statement ends with }, so it does not need a ; after it. Adding one is allowed.
Found a mistake on this page? Edit it on GitHub.