Mastering SAP ABAP: Exploring Conditional Statements, CASE Statements, and Looping Techniques in Day 17

Welcome back everyone to the Day 17 of my complete SAP ABAP master series. So, basically we were discussing about conditional statements where we have discussed IF statements.


How to provide multiple IF conditions :-

  • To provide multiple if conditions we use else if statements.
********************************************************
DATA : lv_input(2) TYPE n VALUE 2.
*Checking of multiple IF conditions
IF lv_input = 1.
  WRITE : 'The output is', lv_input.
ELSEIF lv_input = 2.
  WRITE : 'The output is', lv_input.
ELSEIF lv_input = 3.
  WRITE : 'The outpput is', lv_input.
ELSE.
  WRITE : 'Wrong Input'.
ENDIF.
*******************************************************
  • So, this is all about if statements. let’s discuss CASE statements.

CASE Statements :-

  • It is a conditional statement.
  • Multiple statements blocks are there, depends upon the condition one block executes.
  • If none of the conditions are satisfied, it goes to others part.

Syntax :-



*********************************************************
*Example :- Showing the execution of case statements
DATA : lv_input(2) TYPE n VALUE 1.

CASE lv_input.
  WHEN 1.
    WRITE : 'The output is ', lv_input.
  WHEN 2.
    WRITE : 'The output is', lv_input.
  WHEN 3.
    WRITE : 'The output is', lv_input.
  WHEN OTHERS.
    WRITE : 'Others'.
ENDCASE.
**********************************************************
  • Your output will be like below.




Difference between CASE and IF statements :-

  • IF we have multiple IF conditions.,
    • If checks all the conditions, until we get a true conditions whereas
    • Case directly goes to the true condition.
  • Case is performance effective as compared to IF.

Loop :-

  • Loop allows us to execute a group of statements multiple times.
  • The various loops are as follows :-
    • Do loop
    • While loop
    • Loop at <INTERNAL_TABLE>

Do Loop :-

  • Do loop is called as a unconditional loop.
  • Every do loop ends with ENDDO, Similar to if statements and case which ends with ENDIF and ENDCASE.
***********************************************************
*Syntax of Do loop
Do <n> times. "Here we will write how many times I want my loop to execute
	<statement block>. "This statement will execute n times.
enddo.
************************************************************

Practical Implementation of Do Loop :-

  • Create a executable program in SE38.
******************************************************
*Implementing Do loop.
DATA : lv_input(2) type n value 10.

DO 10 TIMES.
  WRITE :/ 'The output is ', lv_input.
  lv_input = lv_input + 1.
ENDDO.

********************************************************

Output :-



Note :- You must debugger in your each and every program in ABAP Editor to understand the flow logic of program.

How to use debugger ?

  • Activate the program first.

  • Select the line on which you want to place the debugger.



  • Click on the below icon.



  • A debugger will be set to the left of that selected line.



  • Now, As you will press F8 to execute the program, a debugger screen will appear where you can press f5 to debug your code line by line.

  • Use Desktop 3 to see the details more clearly below.




What if we do not specify the number of times, our do loop will execute ?

  • It will become an endless loop.
  • To overcome that problem, we must specify a condition where loop gets terminated.
  • We will achieve this via exit statement.
*****************************************************
*Do loop second syntax
DATA : lv_input(2) type n value 10.
DO.
    WRITE :/ 'The output is ', lv_input.
    lv_input = lv_input - 1.
    if lv_input = 1.
      EXIT. "Use to end the current loop
    endif.
ENDDO.
*******************************************************

Output :-




While Loop :-

  • While loop is called as a conditional loop.
  • Every while loop ends with ENDWHILE.
***************************************
*Syntax for While loop
While <condition>.
		<statement block>.
endwhile.
***************************************

Practical implementation of While Loop :-

  • Create a executable program in ABAP Editor.
******************************
*Implementing While loop
DATA : lv_input(2) TYPE n VALUE 10.

WHILE lv_input < 15. "You can use LT in place of <
  WRITE :/ 'The output is' , lv_input.
  lv_input = lv_input + 1.
ENDWHILE.

*********************************

Output :-




So, that enough for today.

We will continue with third type of loop in the next blog.

Thanx alot for being a part of this wonderful journey.

Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming