Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Is there any difference between the if and case statements?

Status
Not open for further replies.

ragabs

Full Member level 1
Joined
Jun 28, 2005
Messages
97
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
2,111
if and case statements

in c
is there any difference between the if and the case statements?
 

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Re: if and case statements

Hi,

The difference:
"if" statement: the program will read though all the statements, let say got 5 if statements the program will check it one by one whether is true or not by sequence.

"case" statement: the program will wait the decision, if the program know the decision it will switch in the block.

~ the time delay for if statement will take long compare with case statement.
 

    ragabs

    Points: 2
    Helpful Answer Positive Rating

checkmate

Advanced Member level 3
Joined
Feb 25, 2004
Messages
833
Helped
178
Reputation
356
Reaction score
125
Trophy points
1,323
Location
Toilet Seat
Activity points
7,815
Re: if and case statements

The "IF-ELSE" structure has a notion of priority, but the "CASE" structure does not.
 

SkyHigh

Advanced Member level 1
Joined
Jan 13, 2005
Messages
432
Helped
79
Reputation
158
Reaction score
27
Trophy points
1,308
Activity points
7,319
Re: if and case statements

ragabs,

IF-ELSE statement differs from SWITCH-CASE statement.

1. SWITCH works only with discrete or single expresssion.
Unlike IF, no equalities are allowed.
SWITCH sets the program counter or jumps the program directly to the program segment when expression in SWITCH matches the condition in CASE.

For example,
Code:
switch( Grade )
     {
        case 'A' : printf( "Excellent" );
        case 'B' : printf( "Good" );
        case 'C' : printf( "OK" );
        case 'D' : printf( "Mmmmm...." );
        case 'F' : printf( "You must do better than this" );    
        default  : printf( "What is your grade anyway?" );
     }

2. IF works can work with many expressions, from Boolean TRUE and FALSE, range of integers to bitwise comparison. This is because IF works with TRUE or FALSE when expressions are compared with a reference or conditions already defined by you.
For example,
Code:
if ( var_a >= var b)                  // notice that equality is used.
if ( function1(var_a) == var_b ) // notice IF statement allows implicit function.
if ( var_a )                               // notice IF statement tests the TRUE-FALSE of var_a.
IF statement allows qualities. This is very useful when you are working with a range of values. SWITCH will become helpless to work with equalities.

Regarding speed, IF and SWITCH works the same because they need to compare expressions with conditions.
For example:
Code:
switch( Grade )
     {
        case 'A' : printf( "Excellent" );
        case 'B' : printf( "Good" );
        case 'C' : printf( "OK" );
        case 'D' : printf( "Mmmmm...." );
        case 'F' : printf( "You must do better than this" );    
        default  : printf( "What is your grade anyway?" );
     }

Code:
if ( Grade == 'A' ) printf( "Excellent" );
elseif ( Grade == 'B' ) printf( "Good" );
elseif ( Grade == 'C' ) printf( "OK" );
elseif ( Grade == 'D' ) printf( "Mmmmm...." );
elseif ( Grade == 'F' )  printf( "You must do better than this" );    
else printf( "What is your grade anyway?" );
In the above example, both SWITCH and IF works the same way and the same speed. We must remember the set of conditions from A to F are already defined. Both and SWITCH and IF have to compare the expression with the condition one by one until they match.

Ultimately, C Program will eventually be compiled into Assembly Progam specific to the RISC processor.
SWITCH is like BEQ (Branch Equal) and IF is like BHI (Branch Higher), although BEQ can also be used for IF.


If you know the conditions you are working with, use the right statemenet, IF or SWITCH and define the appropriate expressions to work with your conditions.
 

    ragabs

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top