0

what are differences between else if and if twice(or more)?

i always confuse about else if and if twice: here is example: if twice: int mark = 100; if (mark >= 50) {   Console.WriteLine("You passed.");   if (mark == 100) {     Console.WriteLine("Perfect!");   } } else {   Console.WriteLine("You failed."); } /*Outputs You passed. Perfect! */ else if: int x = 33; if (x == 8) {    Console.WriteLine("Value of x is 8"); } else if (x == 18) {    Console.WriteLine("Value of x is 18"); } else if (x == 33) {    Console.WriteLine("Value of x is 33"); } else {    Console.WriteLine("No match"); } //Outputs "Value of x is 33" i will glad that you will can explain me.

19th May 2020, 10:14 PM
Oren Abarbanel
5 ответов
+ 2
Every if *can* have elses, but it doesn't have to. Your first example with the two ifs, they just don't depend on each other like if and else. The inner if starts its own if-else construction (in this case there's no else). The else that follows a bit further down belongs to the first if. That's the actual branch. The inner if is not related. The second example is building a chain. The first has an else, which starts a new if, and that if also has an else, which starts a new if...
19th May 2020, 10:26 PM
HonFu
HonFu - avatar
+ 1
This is little complicate LOL. thank you about explain, HonFu :)
19th May 2020, 10:43 PM
Oren Abarbanel
+ 1
thank you about explain, Avinash Thakur :)
19th May 2020, 11:00 PM
Oren Abarbanel