English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

c programming

2007-09-28 02:33:43 · 5 answers · asked by stranger 2 in Computers & Internet Programming & Design

5 answers

Typically, nested if-else statements are used in the program's logic where there isn't only one condition to evaluate. A switch statement, on the other hand, evaluates only one variable and starts to work there. You can think of a switch statement as a simplified (and stripped-down) version of an if-else block.

switch(x) {
case 1: // do this
case 2: // do this
case 3: // do this
// etc
}

The nested if-else statement's power comes from the fact that it is able to evaluate more than one condition at once, and/or use lots of different conditions in one logical structure.

if(x == 0) {
// do this
} else if(y == 1) {
// do this
} else if(x == 1 && y == 0) {
// do this
}

If you're going to evaluate only one variable in the condition statement, you're better off going with a switch statement, since it looks a lot neater than nested ifs. However, like in the case above, there are times where the only structure you could use is an if-else block, so you don't have much of a choice.

2007-09-28 03:27:22 · answer #1 · answered by RJ Arce 1 · 1 0

I am not familiar with C itself, but I did a lot of programming in other languages back in the day.
The switch is usually a statement on its own that specifies what to do when an exception is encountered.
For instance:
"If x=0, y=12"
Using this type of statement means that the program already is looking for a value of x and is performing an action based on that value. Only when it finds a specific exception, x=0, will it "switch" what it is already doing. (This is just a simple command line, it can be applied to any condition that must be met.)
A nested "if, then, else" statement contains all the constraints inside the nested procedure.
For instance:
If x=0
then y=12
else y=0
endif
Here it doesn't begin looking for specific values of x until it goes into this nested procedure statement. Once it enters though, it will look at all values of x, if they meet the specified value (0), then the task is performed (y=12); if they don't meet that value (non-zero), then another task is performed (y=0).

The entire procedure and constraints are contained inside the nest. Only the exception is specified in a switch, the constraints are on the outside of it.

Hope this helps!!
Good Luck!!!

2007-09-28 02:54:39 · answer #2 · answered by Goyo 6 · 0 3

At the code phase, the "switch" statement is used when you need to "select" between one or more decisions according to a "completely defined control signal". The "If, else" statement is much more appropriate for logic and boolean comparisons rather than equality.

At the compilation phase, the "if" statement is converted to some sort of "load, compare, and jump" instructions, while the "switch" statement is converted to a hash table, thus it should be faster than the "if, else" statement for long list of comparisons, since it will point and jump directly to the part of code to get executed.

For example:

switch (x) {
    case 1:
    case 2:
    case 3:
    case 4:
}

Will be converted to:

    ///this hash function will return one of the cases' addresses
    0x0013122F jmp (hashFunction(x))
    ///hash table starts
    0x00131234 address of case 1
    0x00243453 address of case 2
    0x00331237 address of case 3
    0x00545648 address of case 4
    ///hash table ends

You can see how both of them looks like by making a simple application that have some if statements, compile it, and open it using any disassembler or system debugger. Then create a switch version of it, and see the difference between each.


Thanks

2007-09-28 06:38:04 · answer #3 · answered by Fox 3 · 1 1

A switch statement looks a lot more tidy

2007-09-28 02:39:26 · answer #4 · answered by AnalProgrammer 7 · 1 1

One key difference as I see it is that a switch statement can be recursive, wheres an if statement can not.

Sorry, but syntax is C#.. hopefully not too different.

string output = "";

switch( integerN )
{
case 6:
output += "6 ...";
goto case 5;
case 5:
output += "5 ...";
goto case 4;
case 4:
output += "4 ...";
goto case 3;

case 3:
output += "3 ...";
goto case 2;

case 2:
output += "2 ...";
goto case 1;

case 1:
output += "1";
break;
}

console.Write(output);

2007-09-28 03:58:05 · answer #5 · answered by Christian T 2 · 1 1

fedest.com, questions and answers