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: 

If multiple checkbox is selected, value is not being displayed

walkerist
Participant

I have 2 check boxes and if it is both selected at the same time, it doesn't get executed

SELECTION-SCREEN BEGIN OF BLOCK chk1
WITH FRAME TITLE text-008.
PARAMETERS: chk_morning AS CHECKBOX DEFAULT 'X',
chk_evening AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK chk1.
IF chk_morning = abap_true.
WRITE / 'Morning'.
ELSEIF chk_evening = abap_true.
WRITE / 'Evening'.
ELSE.
WRITE / 'Morning and Evening'.
ENDIF.

When both the checkbox morning and evening is selected, the write statement 'Morning and Evening' is not being displayed. Any help is appreciated. Thank you

25 REPLIES 25

Rajasekhar_Dina
Participant
0 Kudos
IF chk_morning = abap_true.
WRITE / 'Morning'.
ENDIF.


IF chk_evening = abap_true.
WRITE / 'Evening'.
ENDIF.


IF chk_morning = abap_true AND chk_evening = abap_true.
WRITE / 'Morning and Evening'.
ENDIF.

Sandra_Rossi
Active Contributor
0 Kudos

Please delete your duplicate answer. Thanks.

anujawani2426
Active Participant
0 Kudos

Hi,

SELECTION-SCREEN BEGIN OF BLOCK chk1
WITH FRAME TITLE text-008.
PARAMETERS: chk_morning AS CHECKBOX DEFAULT 'X',
chk_evening AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK chk1.
IF chk_morning = abap_true.
WRITE / 'Morning'.
ELSEIF chk_evening = abap_true.
WRITE / 'Evening'.
ELSEIF chk_morning = abap_true AND chk_evening = abap_true. WRITE / 'Morning and Evening'.
ENDIF.

0 Kudos

You have added a required logical expression, but you have not rearranged the order of these expressions, so no sorry.

0 Kudos

Hi raymond.giuseppi order of written statement will change the result ?. All three conditions needs to be done. We dont know which condition has the priority so in that case how can you say that last conditions which i have added should be first or should not be the last. Or you want to say something else.

please clarify.

0 Kudos

Hi raymond.giuseppi I am totally disagree with your statement

0 Kudos

Hi raymond.giuseppi

If you have right answer, and as per your opinion my answer is wrong then you can put your answer so will get know it.

0 Kudos

In online or F1 documentation you can find 'The logical expressions, beginning with the IF statement, are checked from top to bottom and the statement block after the first real logical expression is executed. If none of the logical expressions are true, the statement block after the ELSE statement is executed.' so 'order of written statement will change the result' - so start from more restrictive criteria (here both boxes are checked) to less, the ELSE will be the last one

0 Kudos

Hi raymond.giuseppi

I got your point. Thanks

matt
Active Contributor
0 Kudos

As an exercise, run it in the debugger and see how it is executed.

0 Kudos

Matthew Billingham tried and got it. Thanks

former_member9115
Participant
0 Kudos

Hi,

You can try this.

IF chk_mor = ABAP_TRUE and chk_eve is initial.
    WRITE / 'Morning'.
ELSEIF chk_eve = ABAP_TRUE and chk_mor is initial.
    WRITE / 'Evening'.
ELSE.
    WRITE / 'Morning and Evening'.
ENDIF.

0 Kudos

What happen when no box is checked?

0 Kudos

If you're going to use abap_true (which is a good thing), don't use IS INITIAL instead of = abap_false.

0 Kudos

matthew.billingham

Thanks you sir for your suggestion.
Just for my knowledge I want to know why IS INITIAL not preferable? (so in future i can improve my skill)

shital1111

A checkbox is like boolean true/false. Used more by developers worldwide. That's why.Technically speaking, it's the same (like also = ' ' or = space etc.)

shital1111 It's consistency and readability. Why use two different approaches instead of a single approach? With IS INITIAL you are relying on the programmer who reads your code in knowing that IS INITIAL means "false". With abap_false it's right there in the words!

I suggest you work your way through this https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md or getting the book mentioned here https://blogs.sap.com/2022/06/14/simplified-learning-of-clean-abap-and-clean-ui5/

0 Kudos

Thank You sandra.rossi

Thank You matthew.billingham

DominikTylczyn
Active Contributor

Hello walkerist

Look at your IF...ELSEIF...ELSE...ENDIF statement carefully and see how it's evaluated and executed if both checkboxes are set.

  1. The IF statement is true as chk_morning = abap_true so the block is executed
  2. ELSEIF is not evaluated as previous IF is true, the block is not exeucted.
  3. ELSE is not executed as the first IF is true - see SAP Help on IF statement to understand why ELSE is not executed in this case. The SAP Help reads:

If none of the logical expressions are true, the statement block after the ELSE statement is executed.

In your case the first logical expression is true, hence ELSE is not executed.

Best regards

Dominik Tylczynski

Hi,

Try this one.

IF chk_morn = abap_true AND chk_even = abap_true.
WRITE / 'Morning and Evening'.
ELSEIF chk_morn = abap_true.
WRITE / 'Morning'.
ELSEIF chk_even = abap_true.
WRITE / 'Evening'.
ELSE.
WRITE / 'nothing selected'.
ENDIF.

This is a correct answer 🙂

Tq, Raymond

DominikTylczyn
Active Contributor
0 Kudos

That's actually quite a good question on IF ELSEIF ELSE logic.

gabmarian
Active Contributor
0 Kudos

When you evaluate condition with IF-ELSEIF you always start with the more specific case and then proceed.

Do a google search for the FizzBuzz programming problem.

raymond_giuseppi
Active Contributor
0 Kudos

In a IF control structure, only the first valid statement block will be executed. So adapt your checks and their order.