⬅ Previous Topic
Bitwise Operators - AND, OR, XOR, NOTNext Topic ⮕
Nested Conditions - Multi-Level Decision Making⬅ Previous Topic
Bitwise Operators - AND, OR, XOR, NOTNext Topic ⮕
Nested Conditions - Multi-Level Decision MakingConditional statements are used to make decisions in programming. They allow the program to choose different paths of execution based on whether certain conditions are true or false.
Imagine writing a program to check if someone is eligible to vote. You need to check their age. This is a decision-making scenario — and that's exactly what conditional statements help with.
if
is falseif
inside another if
Let’s check if a number is positive.
set number to 10
if number > 0 then
print "Number is positive"
Number is positive
If the condition is not met, nothing happens unless you provide an else
block.
Let’s extend the previous logic to also say when the number is not positive.
set number to -5
if number > 0 then
print "Number is positive"
else
print "Number is not positive"
Number is not positive
What if you want to check for more than two conditions?
You can use else if
to test additional conditions.
Check if a number is positive, negative, or zero.
set number to 0
if number > 0 then
print "Positive"
else if number < 0 then
print "Negative"
else
print "Zero"
Zero
Sometimes, one decision depends on another. This is where nested conditions are useful.
Check if a person is eligible to vote and if they have a voter ID.
set age to 22
set hasVoterID to true
if age >= 18 then
if hasVoterID is true then
print "Eligible to vote"
else
print "Get a voter ID first"
else
print "Not eligible to vote"
end
Eligible to vote
true
condition runs.else
.⬅ Previous Topic
Bitwise Operators - AND, OR, XOR, NOTNext Topic ⮕
Nested Conditions - Multi-Level Decision MakingYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.