Application Development 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: 

assigning value inside a case statement

Former Member
0 Kudos

Hi friends

iam using the below code and can any one tell me why even after assigning the value in the case statement it coming out of it.

code

data :

p1(1) type n.

case p1.

when '0'.

write 'sunday'.

p1 = 1.

when '1'.

write 'monday'.

p1 = 2.

when others.

write 'invalid day'.

endcase.

output.

sunday

can any one tell me why it is coming out of the case statement even iam assigning p1 = 1

1 ACCEPTED SOLUTION

JozsefSzikszai
Active Contributor
0 Kudos

hi,

once one WHEN condition was found true, it will jump to ENDCASE, no other conditions will be checked.

you can do something like this:

WHILE condition.

CASE ... => your code from above

...

ENDCASE.

ENDWHILE.

hope this helps

ec

10 REPLIES 10

Former Member
0 Kudos

Hi,

It is working as it should.

each of the when block acts exactly similar to if....elseif....else...endif. block.

when one of the block is satisfies by your value , the cursor will jump to the ENDCASE statement only.

So even you update the value of p1, it will not go to the next when block.

There is no room for break or similar statement in ABAP as we used in C for case.

Regards,

Anirban

Former Member
0 Kudos

Hi,

Try like this:

parameters: p1(1) type n.

case p1.

when '0'.

write 'sunday'.

when '1'.

write 'monday'.

when others.

write 'invalid day'.

endcase.

Regards,

Bhaskar

Former Member
0 Kudos

This is happening b'cause u have already in the when clause where u r setting the value of the variable that is controlling ur case..endcase statement.To meet ur requirement u need to trigger the case..endcase statement again as shown below:

data :

p1(1) type n.

do 3 times.

case p1.

when '0'.

write 'sunday'.

p1 = 1.

when '1'.

write 'monday'.

p1 = 2.

when others.

write 'invalid day'.

endcase.

enddo.

Former Member
0 Kudos

after executing the first case, it comes out of the case and does not check other cases.

0 Kudos

ya its coming out of the case statement after displaying sunday.

is do n times

enddo

is the only solution to print all the when conditions are do we have any other alternative for that.

Regards

PRIYANKA.

0 Kudos

Priyanka

Use IF ...Endif for every conditions

Regards

Sridhar

0 Kudos

even for if endif also iam facing the same problem.

Regards

priyanka

0 Kudos

Hi priyanka,

that's what I would like you to say in my previous post above.

Both IF....ENDIF.

and CASE....ENDCASE will behabe same way.

and if you use do....enddo. to get all the cases executed, what is the requirment of case...endcase.

you can get them directly.

Isn't it?

Regards,

Anirban

Former Member
0 Kudos

hi,

do like this

parameters: p1(1) type n.

do n times.

case p1.

when '0'.

write 'sunday'.

when '1'.

write 'monday'.

when others.

write 'invalid day'.

endcase.

p1 = p1 + 1.

enddo.

JozsefSzikszai
Active Contributor
0 Kudos

hi,

once one WHEN condition was found true, it will jump to ENDCASE, no other conditions will be checked.

you can do something like this:

WHILE condition.

CASE ... => your code from above

...

ENDCASE.

ENDWHILE.

hope this helps

ec