Embarking on ABAP Adventures: Unveiling the Sum of Two Numbers
Welcome back everyone, So basically we were discussing the basic points of ABAP programming in our previous blogs, So we will continue with writing our first program { sum of two numbers }.
- As you remember, we have selected the type of above program as executable why?
- It is because, we have to execute the program
Note :- In any programming language, a program generally have 3 parts :-
- Input
- Processing Logic
- Output
so, for our first program we will first go for input, then processing logic and then for output.
- We have discussed in our previous blog, that DATA is used to define a variable.
- So, basically we have to create three data object, one for output and 2 for input.
- Then we will provide the input, in this program we will start with default input and in the upcoming one we will take inputs from the user.
- Then we will perform the operation.
- In SAP we use write statement to display the output.
- After writing the code, for best practices always use the pretty printer
Write Statement :-
-
The basic ABAP statement for displaying data on the screen is written.
-
Example :-
DATA : number type I value 1,
name(25) type c value ‘Leena’.
Write : ‘The number is’, number.
Write : / ‘The Name is ‘ , name.
-
We can use ‘/’ in write statement to denote a new line.
- Write the below program in your system.
REPORT zar_sum_of_two_numbers.
****************************
*Defining the input variables and output variable
DATA lv_input1(2) TYPE n. "First Input
DATA lv_input2(2) TYPE n. "Second Input
DATA lv_output(2) TYPE n. "Third Input
**Here lv is used for local variable
********************************************
*******************************************
**Taking defaults inputs
lv_input1 = 10.
lv_input2 = 20.
*******************************************
****************************************
*Write the logic to perform operation
lv_output = lv_input1 + lv_input2.
****************************************
****************************************
*Display the result
Write : 'The sum of two numbers is',
/ lv_output.
*****************************************
-
Use Ctrl + f2 to check for syntax and Ctrl + f3 to activate the program.
-
Press f8 to execute the program.
-
IF, you remember, when we were creating the above program, we had given a description ‘Sum of two numbers’ , this will always present when you will execute it, you can change it whenever you want just click on edit → attributes.
-
Below, of the title we can see the same thing which we have written within the write statement.
Note :- We have taken default input for now, we will take user inputs in future.
-
Chain Operator :-
-
The chain operator is ‘:’.
-
It is used to combine the statements.
-
Statement sequence -
Write var1,
Write var2,
Write var3.
-
Chain statement : Write var1, var2, var3.
Conditional Statements :-
- Conditional statements allows us to execute a block of code if a certain condition is met.
- The various conditional statements are as follows :-
- IF statement
- CASE statement
1. IF statement :-
-
We have three versions of IF statements, as you can see in the below image.
-
IF is a conditional statement.
-
Multiple statements blocks are there, depends upon the condition one block executes.
-
We provide multiple conditions using elseif.
-
If none of the If and elseif conditions are satisfied, it goes to else part.
Implementation of conditional statements :-
-
Go to SE38.
-
Create a executable program.
REPORT zar_conditional_statements.
DATA : lv_input(2) TYPE n VALUE 2.
**********************************
*Checking of IF condition
IF lv_input = 1.
WRITE : 'The output is', lv_input.
ELSE.
WRITE : 'Wrong Input'.
ENDIF.
***********************************
-
Execute the program.
-
You will see the output as ‘Wrong input’.
So, that’s enough for today,
If will continue in the next blog.
Thanx for being a part of this wonderful journey.
Comments
Post a Comment