+ 2
What is the point of nesting if statements?
5 Respostas
+ 2
@Muhthukamar in cases such as that it is easier to write switch statements, as "Case _" is very easy to read and write since you give them a title without commenting which is what you would need to do in nested if statememts
+ 1
A nested if statement is different then an else statement
a nested if would be a check if something else is true
example
int x = 25;
if (x > 20)
{
if (x == 25)
{
//do something special because it's 25
}
}
end example
A else is used to caught if a statement is false and do something
example
int x = 10
if (x > 20)
{
}
else
{
//do something because it's not greater then 20
}
end example
else would only be executed if the if is false
you can also use
else if to add other checks before just plain else and once one is true it executes that and ignores the rest even if others are true
example
int x = 15;
if (x > 20)
{
//would be false and any code here is ignored
}
else if (x < 20)
{
//this would be true so this would execute
}
else if (x > 10)
{
//this would do nothing cause a previous statement was true already and the rest is ignored even if true
}
end example
hope this helps
0
wht
0
So we can go on with multiple conditions,, for cases like you have to write separate wish sentence for student having marks 70,80,90 and just pass,,
0
@dinoswarleafs yes i agree,but what i intend to say was,
Greater than 40
Congrats
Equal to 100
And wonderful
Greater than 80
And Superb
Less than 40
Better luck next time
Pretend that above example is written with nested if.
To wish like that,,we can go with nested if..