Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Diff b/w IF/ENDIF and CASE statement?

Former Member
0 Likes
4,861

Hi all,

Plz let me know the difference between IF and CASE statements.Which one is better to be used in general...and are there any specific situations where we have to go for CASE insted of IF.

Rgds,

Reddy.

8 REPLIES 8
Read only

Former Member
0 Likes
2,197

Hi,

Case statement we can use for different type of situation occurence instead of using or in if case but if statement we are using for one situatation check max.

Regards,

Nandha

reward if it helps

Read only

Former Member
0 Likes
2,197

Hi

CASE is also a kind of IF...ELSE stmnt. (may be for 3 or 4)

For few conditions to check and act accordingly, we prefer IF...ELSE....

If the conditions are more ..then CASE stmnt is preferred..

CASE <field>

WHEN <value>

<code>

WHEN <value>

<code>

WHEN <value>

<code>

ENDCASE.

Regards

Raj

Read only

Former Member
2,197

Hi

If it is 1 or 2 conditions use IF..ELSE..ENDIF.

if there are more conditions better to use CASE..WHEN..ENDCASE.

Reward points for useful Answers

Regards

Anji

Read only

Former Member
0 Likes
2,197

Hi,

Case statement is good for performance wise because here we declare cases

and according to these cases program will be executed.

When we use if statement then the cursur goes to in the loop, when ever

the loop is not complete till then the programme will not execute.

Thats by in performance basis the case statement is good.

Let me try and explain using an example...

1) For IF statement...

Suppost you have a variable color..and you are checking it against color names.

then your code will be

IF COLOR == 'BLUE'.

statements

ELSEIF COLOR == 'BLACK'.

statements.

...and so on....

ENDIF.

But if you use CASE Statement

CASE COLOR.

when 'BLUE'.

statements.

when 'BLACK'.

statements.

so follows that CASE is easy to use and performance wise is also better.

Regards,

Priyanka.

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
2,197

Hi,

The behaviour is same, that is the effect that you want to have is same.

But If you have lot of alternatives cases it looks ODD when you put then in IF ELSEIF and ENDIF's. In such a case CASE ENDCASE is more useful interms of readability.

CASE is supposed to be faster compared to IF.

Regards,

Sesh

Read only

Former Member
0 Likes
2,197

hi

u can use any of these

but if there are large number of conditons to check

then performance wise and also according to the scenario

we prefer to use CASE statement

regards

ravish

<b>plz dont forget to reward points if helpful</b>

Read only

Former Member
0 Likes
2,197

Hello,

Choice is your requirement:

Using IF-ENDIF condition you can have any logical expression line EQ, NE, CP etc. Also NOT, OR , AND relational logic.

Howevere in CASE- ENDCASE you check for equality only.

Regards,

A.Singh

Read only

Former Member
0 Likes
2,197

HI,

it is better to use case instead of using elseif(condition) block.

if u use elseif means to execute the code in elseif it should take decision twice(once at if(condition) and other at elseif(condition)).like this if u use 10 elseif (condition) statements in ur if...endif block to execute the 10 th block it should take the decision for 11 times.it will degarde the performance.in case of CASE it always will take decision once.so in these types of situations we have to use case.

<b>reward if helpful</b>

rgds,

bharat.