3

Following code compiles on my gcc 5.4.0, doesn't generate any warnings and works fine:

if (a == 0) {
    puts("0");
} else switch (a) {
    case 1: puts("1"); break;
    case 2: puts("2"); break;
    default: puts("default"); break;
}

Is if ... else switch ... correct statement?

Piotr
  • 41
  • 4
  • 12
    Correct, yes. Formatted horribly, yes. – Carey Gregory Jul 05 '17 at 13:34
  • Similar question in C++: https://stackoverflow.com/questions/35985592/is-if-condition-try-legal-in-c/35985655#35985655 – Mohit Jain Jul 05 '17 at 13:39
  • 5
    Related fun fact: The C language actually does not have an `else if` statement. It is an `else` statement without braces followed by another `if` statement. `else if(a==b){ }` is a brace-less, strangely indented version of `else /* new line */ { if(a==b){} }`. – Lundin Jul 05 '17 at 15:13
  • @Lundin That's right. IMO many books and tutorials suggest that `else if` is a statement but actually it is not – Piotr Jul 05 '17 at 15:27

5 Answers5

9

There's nothing wrong with your code. The grammar for if ... else is as follows:

attr(optional) if ( condition ) statement-true else statement-false

switch is a statement, so it is allowed to go after else.


Note that else if is not a special construct either, it's just an if statement after an else.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
4

What you have is syntactically correct... but it does not make easy reading.

You will likely be pulled up on it if your code is being reviewed - because you're making use of an "un-braced, multi-line statement as part of a conditional".

Prefer to be explicit and write it like so:

if (a == 0) {
    puts("0");
} else {
    switch (a) {
        case 1: puts("1"); break;
        case 2: puts("2"); break;
        default: puts("default"); break;
    }
}

It is legal in the same way that the following is legal:

if (a == 0)
    puts("0");
else
    puts("not0");

Such constructs can lead to mistakes when re-visiting the code... I seem to remember that one of the recent "popular" vulnerabilities was implemented (by mistake... hopefully) in part due to this "un-braced" use of if.

Attie
  • 6,690
  • 2
  • 24
  • 34
3

The switch is a statement, so this is just putting a statement in the else.

It's no different from

else
  a = 0;

or

else
{
  switch(a)
  {
  case 1:
  ...
  }
}

It's not a very common way of writing it, but it's fine.

unwind
  • 391,730
  • 64
  • 469
  • 606
3

Nothing wrong with your code, that is the same of writing this:

if (a == 0) {
    puts("0");
} else {
    switch (a) {
        case 1: puts("1"); break;
        case 2: puts("2"); break;
        default: puts("default"); break;
    }
}

That is perfectly legit. else switch is not a real statement.

Your statement is

if(condition){code block} else {code block}

code block can contain any statement, such as your switch statement.

magicleon94
  • 4,887
  • 2
  • 24
  • 53
2

Syntax of if is:

if ( condition ) statement-true  else statement-false
condition is an expression convertible to a boolean (true/false)
statement-true is a statement which is executed if condition is true
statement-false is a statement which is executed if condition is false

And switch block is a statement.

So yes, your code exhibit correct statement.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100