difference between else if and if sequences

Asked 2 years ago, Updated 2 years ago, 35 views

In the case of if statements, I often use else if when dividing, but I happened to see them dividing like if bullet points.

if(){
} else if() {
} else if() {
}

and

if(){}
if(){}
if(){}

What are the differences between ?

c

2022-09-30 19:43

9 Answers

I think you need to use it differently depending on the purpose.

else if only handles one of the three parentheses.
Bullets may be processed in all three brackets.

Take a simple example

The string "abc" must be set to

  • (1) Starting with "a"
  • (2) Ending with "c"
  • (3) The string "abc" is

To evaluate in three patterns:

else if only takes action in parentheses of (1) evaluation if:

if(1) Assessment {
    // String beginning with "a"
} else if((2) evaluation) {
    // String ending with "c"
}else if((3) Assessment)
    // string "abc"
}

In the case of bullet points, however, all actions in parentheses are performed.

if(1) Assessment {
    // String beginning with "a"
}
if((2) Assessment){
    // String ending with "c"
}
if((3) Assessment){
    // string "abc"
}


2022-09-30 19:43

Example 1

inta=10;
if(a<10){
    // not go in here
} else if (a<20) {
    // get in here
} else if (a<30) {
    // not go in here
}

Example 2

inta=10;
if(a<10){
    // not go in here
}
if(a<20){
    // get in here
}
if(a<30){
    // get in here
}

...it's a completely different thing.d ^^


2022-09-30 19:43

if(){}
else if(){} 

where

if(){
} else {
  if(){
  }
}

That's right.

if(){}
if(){}

It's a little different from


2022-09-30 19:43

I think it would be okay if someone else answered...

Pattern A

if(Condition 1){Process 1}
else if (Condition 2) {Process 2}
else if (Condition 3) {Process 3}

Pattern B

if(Condition 1){Process 1}
if(Condition 2){Process 2}
if(Condition 3){Process 3}

I think it's the difference between

For pattern A , if "condition 1" is met, "action 1" is performed, but "action 2" is not performed even if "condition 2" is met.
For pattern B , perform " action 1" if " condition 1" is met, and " condition 2" if " condition 2" is met.
Therefore, if "Condition 1" and "Condition 2" are not met at the same time, the result will be the same. The same goes for "Condition 3".

However, for pattern B , an evaluation of Condition 2 is performed even if "Condition 1" is met.
Therefore, if "Condition 1" and "Condition 2" are not met at the same time, the evaluation of "Condition 2" will be performed even if "Condition 1" is met, which will result in extra processing.
Specifically, if Condition 1:(A==1), Condition 2:(A==2), pattern B is a little bit smaller, but slow.
Conversely, if Condition 1:(A==1), Condition 2:(B==1), the pattern A and pattern B (often) have different results.


2022-09-30 19:43

if(A){
} else if(B) {
} else if(C) {
}

is similar to the switch case statement.Conditions B, C are not processed if there is a priority (A>B>C) and the condition A is true.

if(A){}
if(B){}
if(C){}

The proceeds with one of the matching A, B, or C conditions.


2022-09-30 19:43

I thought of some code examples that might tell the difference.

inta=0;
if(a==0){
   a++;// I'm going to go in here.
}
else if(a==1){// For a series of if()~else if()~else, a is already "evaluated"
   a++;// Does not enter
}
else if(a==2){
   a++;// Does not enter
}
// where a is "1".

and

inta=0;
if(a==0){//Independent if().
   a++;// Enter here because the evaluation result is true
}
if(a==1){// This is also an independent if().
   It's a++;//, so it goes in here too.
}
if(a==2){// This is also an independent if().
   It's a++;//, so it goes in here too.
}
// where a is 3


2022-09-30 19:43

If you have any doubts, please write down the sample code and try to execute it.
If you switch between true and false of a, b, and c, you will see the difference in behavior.

#include<stdio.h>
# include <stdlib.h>
# include<stdbool.h>

int main(intargc, char*argv[])
{
    boola = false;
    bool b = true;
    boolec = true;

    if(a){
        printf("if-else:a is true.\n");
    } else if(b) {
        printf("if-else:a is noture, and is true.\n");
    } else if(c) {
        printf("if-else:a is not true, and is not true, and is true.\n";
    }

    if(a){
        printf("if:a is true.\n");
    }
    if(b){
        printf("if:bit true.\n");
    }
    if(c){
        printf("if:cis true.\n");
    }
}


2022-09-30 19:43

Bulleted if statements are simply arranged side by side for the reader to understand the source code, and when a different if(...) appears, they are logically another statement.

For example, if(...)else should be used because "even or odd" only applies to one of the conditions.

int num=2;

if(num%2==0){
    printf "%d is even\n", num;
}
else{
    printf "%d is odd\n", num;
]

"If ""I want to see if a value is a multiple of x and y"", I think it will be a bullet form of the if statement."

int num=10;

if(num%2==0){
    printf "%d is a multiple of 2\n", num;
}
if(num%5==0){
    printf "%d is a multiple of 5\n", num;
}

Personally, I think you should use switchcase honestly if you want to write one or more if statements to be easy to distinguish, or if you want to write conditions like switchcase statements.


2022-09-30 19:43

if(A){}
if(B){}
if(C){}

is three independent if statements.

if(A){
} else if(B) {
} else if(C) {
}

is one if statement.→ if(A)
The else clause in if(A) is another if statement: if(B).
Similarly, the else clause of if(B) is another if statement: if(C).
If you rewrite it like this, it will be easy to understand the difference.

 if(A)
{
    ...
}
else
{
    if(B){
        ...
    }
    else
    {
        if(C)
        {
            ...
        }
    }
}


2022-09-30 19:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.