What is the point of nesting if statements? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the point of nesting if statements?

26th Sep 2016, 10:03 PM
HarveyDent
HarveyDent  - avatar
5 Answers
+ 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
27th Sep 2016, 7:09 PM
Dinoswarleafs
Dinoswarleafs - avatar
+ 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
27th Sep 2016, 5:35 PM
Zachary Blubaugh
Zachary Blubaugh - avatar
0
wht
27th Sep 2016, 5:37 PM
rahul negi
rahul negi - avatar
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,,
27th Sep 2016, 6:43 PM
Muthukumar Arumugam
Muthukumar Arumugam - avatar
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..
28th Sep 2016, 4:19 AM
Muthukumar Arumugam
Muthukumar Arumugam - avatar