0
Please can anyone help me differentiate between else if and if else in C. Or are they the same thing
C Programming
11 Antworten
+ 1
Vector
There is no syntax like (if else) there is (else if)
+ 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
}
}
+ 2
Understood. Many thanks
+ 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!
+ 2
U have to understand the principe of logical operators in mathematics , it 's the same AND OR NOT
+ 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
//
+ 1
Thanks🙂
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
//
0
Would it work the same way?
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
}
0
I see. Thanks