0

Please can anyone help me differentiate between else if and if else in C. Or are they the same thing

C Programming

19th Mar 2022, 10:38 PM
Vector
Vector - avatar
11 odpowiedzi
+ 1
Vector There is no syntax like (if else) there is (else if)
19th Mar 2022, 10:58 PM
A͢J
A͢J - avatar
+ 2
Vector Sometimes we can have nested if else like: if (a > b) { if ( a > c) { //Print a } else { //Print c } } else { if (b > c) { //Print b } else if (a > c) { //Print a } else { //Print c } }
19th Mar 2022, 11:06 PM
A͢J
A͢J - avatar
+ 2
Understood. Many thanks
19th Mar 2022, 11:13 PM
Vector
Vector - avatar
+ 2
Some languages support three different keywords that are similar in form to if/elseif/else. But C only supports if/else. It does not formally recognize else if. Rather, when you use else if, the C compiler interprets it as an else clause that contains a nested if. So when you write C code in the following style - which is perfectly accepable - it is interpreted as the syntax below it: if () else if () else if () else In reality is seen to be nested as: if () else { if () else { if () else } } In the final analysis they are the same!
20th Mar 2022, 1:20 AM
Brian
Brian - avatar
+ 2
U have to understand the principe of logical operators in mathematics , it 's the same AND OR NOT
21st Mar 2022, 5:26 PM
Nassera
Nassera - avatar
+ 1
If there are only yes and no situation Then only if else if(true) // else // If there are yes no maybe situation then if () // else if() // else //
19th Mar 2022, 10:53 PM
A͢J
A͢J - avatar
+ 1
Thanks🙂
21st Mar 2022, 5:27 PM
Vector
Vector - avatar
0
Thank you. But I can see if else being an actual thing. If there are say 3 possible situations. Can I say If() // if else () // else //
19th Mar 2022, 10:56 PM
Vector
Vector - avatar
0
Would it work the same way?
19th Mar 2022, 10:56 PM
Vector
Vector - avatar
0
Vector You can apply many else if based on situation like in calculator: if (ch == '+') { } else if (ch == '-') { } else if (ch == '*') { } else if (ch == '/') { } else { // Invalid input }
19th Mar 2022, 11:01 PM
A͢J
A͢J - avatar
0
I see. Thanks
20th Mar 2022, 6:06 AM
Vector
Vector - avatar