Ads

ABAP Programming Basics for Beginners

ABAP Programming Basics for Beginners 


To become a  technical ABAP consultant ,we will learn ABAP Programming Basics which required for  all ABAP Programming Beginners along with basics we should have good communication skills In this post, i defined the ABAP basic rules syntax format, keyword definition, statements ,variables ,data types, operators and variable declaration which we must be learn ,if we learn these primary basics ,it would be simple when we learning the course.



PROGRAMMING: SYNTAX

FUNDAMENTALS
ABAP Workbench
Object Navigator (SE80)
Packages
Transports
Creating database tables
Field names, data elements, and data types
Data element: “object that describes the data type and semantic meaning of a table field” (SAP online glossary)
Domain: defines valid value ranges for fields. Similar fields can be grouped into a domain. Changing that domain changes all the fields.
ABAP Variables and Data Types
Data type: description of the kind of data a variable may hold and the range of acceptable values based on storage allocated.

Data object: actual variable or constant (of a stated type) that has been defined.
Complete Data Types
Fixed size, specified format data storage.
Incomplete Data Types
Storage size can vary, so must be set upon variable declaration.

ABAP Standard Data Types


i integer 4 byte whole number +/- 2.1 billion
F float 8 bytes, 15-16 significant digits
C string up to 65k characters
N numeric string up to 65k characters (non-math number)
String string dynamic length up to 2 GB long!
xstring hex string dynamic length byte sequence
X byte sequence up to 65k bytes
D date 8 characters in form YYYYMMDD
T time 6 characters in from HHMMSS
P packed number precise whole or floating number up to 16 bytes
Bold italics indicate incomplete data types (size set on declaration)

Variable Declarations


DATA varname TYPE [type specification]
Use of chained statements (more typical):
Other variations (considered obsolete) exist.
LIKE can be used to declare a variable based on a prior declaration
DATA  bad TYPE i. "complete data type
DATA  super TYPE i VALUE 7. "w/ initialization
DATA  please TYPE c LENGTH 2 VALUE 'TN'. "Incomplete
DATA:  bad TYPE i,
 super TYPE i VALUE 7.
When specifying default values for numeric data with decimals, the
value must be placed within single quotes.
DATA state2  LIKE state. "Only data type 'copied'
Declaring your own data types—program local
TYPES allows declaring local data types that are more specific than 

Standard types.


TYPES typename TYPE [type specification]
This can be used to give a standard type a more descriptive name for your application or to more specifically define variable composition.
TYPES cannot have a user-specified default value.
TYPES declarations are local to program. Global type declarations are possible through use of the ABAP Dictionary, allowing type management across entire system.
TYPES: userval TYPE i,
 usercode TYPE n LENGTH 10,
 rate TYPE p LENGTH 3 DECIMALS 2.

Declaring your own data types—global


ABAP Dictionary (SE11)
Enter name of type to be created in “Data type” field.
To avoid name collision for class, start your type names with Znn_
Select Create and choose the class of data type.
Describe the data type and provide its technical specification.
Specify how the field will be labelled in reports.
Activate.
Use in programs.
Can generate a list of all repository elements that use the data type.
Choose Where-Used List icon from SE11 initial screen.
What is the value of defining and using global data types?

Constants


CONSTANTS allows specification of fixed data objects.
CONSTANTS name TYPE type VALUE value.
A number of system-maintained constants exist. These are all within structure SYST. Can see list through dictionary, data type SYST.
Reference values by using the identifier SY-COMPONENT. 
Example,
SY-MANDT will display the current client number.
CONSTANTS a_const TYPE d VALUE '19681214'.
CONSTANTS b_con TYPE p LENGTH 3 DECIMALS 2 VALUE
'37.46'.

Arithmetic and assignment


Valid arithmetic operators: +, -, *, /, ** (exponentiation), DIV (integerdivision), MOD.
Parenthesis can be used to set order of precedence.
Operators and parentheses are keywords and must be separatedfrom other statement elements by at least one space.
Assignment syntax:
MOVE var2 TO var1.
var1 = var2.
varx = 5 + 11 * 2.
If variables of different type, automatic type conversion attempted.
CLEAR varx. Resets varx to default type-related value.
Writing to display output 
WRITE is used for basic result(output).
WRITE 10 'result'.
Output is written beginning in column 10.
WRITE (3) 'result'.
Output is written in field of size 3. * used to show number truncation
(if any). String truncation note noted.
WRITE (*) 'Output'
Output is written in field of sufficient size, without extra spacing.
WRITE 10(3) 'Output'.
Combination of above techniques.
WRITE / 'Output'.
Output written on the next line
WRITE /10(3) 'Output'.
Combination of above techniques.

Parameters

PARAMETERS prompts the user for runtime value at program start.
(Called a selection screen.) Syntax is the same as DATA.
Parameter name must be 8 characters or less.
User is prompted with the parameter name.
DEFAULT can be used to specify a default value in the field.
PARAMETERS age TYPE i.
PARAMETERS: var1 TYPE c LENGTH 8,
 var2 TYPE i.
PARAMETERS var1 TYPE c LENGTH 8 DEFAULT 'NONE'.

About  Literal text

ABAP is multilingual. Coding in string literals (prompting, etc.) defeats language independence and should only be done in testing.
The method for this is text symbols.
Text symbols belong to each program in the text pool for that program.
The  different  texts are placed in the text pool and assigned a 3-character
alphanumeric code (yyy).
This code is then used by specifying either
TEXT-yyy instead of the literal.
‘literal’(yyy) where literal is the message in the native language.
Access the text pool to define text symbols by either:
From editor select Goto  Text Elements  Text Symbols
Using the syntax above to reference a text symbol in coding. Then double
click on the text symbol entered.
Goto  Translation will translate the text symbols.
About  logical Expressions
Logical expressions evaluate to true or false.
In ABAP, logical expression cannot be assigned to variables (no
boolean data type).
Logical expressions are used in conditional code sequences.

Relational Operators


Operator Operation
=, EQ Is equal to
<>, NE Is not equal to
<, LT Is less than
>, GT Is greater than
<=, LE Is less than or equal to
>=, GE Is greater than or equal to

Boolean Operators


Operator Operation
AND Logical and
OR Logical or
NOT Logical not
NOT is placed before an expression to negate its result.
NOT var_a LT var_b.
Conditional code execution: IF, ELSEIF, ELSE
IF logical_expression.
One  or more statements.
ENDIF.
IF logical_expression.
One  or more statements.
ELSE.
1 or more statements.
ENDIF.
IF  logical_expression.
one or more statements.
ELSEIF  logical_expression.
One  or more statements.
ENDIF.
IF var_a LT var_b.
WRITE / var_a.
ENDIF.
IF var_a LT var_b.
WRITE / var_a.
ELSE.
WRITE / var_b.
ENDIF.
IF var_a LT var_b.
WRITE / var_a.
ELSEIF var_b LT var_c.
WRITE / var_b.
ENDIF.
Conditional code execution: CASE
CASE data_object.
WHEN value.
 One or more statements.
WHEN value.
 One  or more statements.
WHEN OTHERS.
 One  or more statements.
ENDCASE.
CASE var_b.
WHEN 1.
 WRITE \ 'star'.
WHEN 2.
 WRITE \ 'pot'.
WHEN OTHERS.
 WRITE \ 'later'.
ENDCASE.

How to use  the Debugger to test programs


Program must be saved and activated before debugging.
Set a breakpoint indicating where you want the program to stop in
the execution and display the debugger.
Use Direct Processing to begin execution. Program runs to
breakpoint.
In the source code display, double click any variable you wish to
watch.
Add/remove additional breakpoints by single clicking to left of line.
Step through code using controls in upper left—single step, execute,
return, continue (runs until next breakpoint).
Watched variable values can be changed.
A watchpoint can be set of a variable and the program will run until that variable's value changes 

Looping in ABAP


DO value TIMES.
One  or more statements.
ENDDO.
WHILE condition.
 One  or more statements.
ENDWHILE.
DO.
1 or more statements.
IF abort_condition.
 EXIT.
ENDIF.
ENDDO.
DO var_a TIMES.
WRITE / 'Hi'.
ENDDO.
WHILE var_a < 11.
var_a = var_a * 3.
ENDWHILE.
DO.
var_a = var_a * 3.
IF var_a > 201.
 EXIT.
ENDIF.
ENDDO.
ABAP supports definite iteration, pre-test iteration, and post-test iteration.
Within loops sy-index is a system managed loop counter.
Exiting Loops
EXIT: Loop exits immediately. Jump to statement that follows loop block.
CONTINUE: Restart next loop iteration.
CHECK condition: Restart next loop iteration (continue) if false.
WHILE sy-index < 10.
 WRITE / sy-index.
 IF sy-index < 3.
 CONTINUE.
 ENDIF.
 WRITE 'After ENDIF'.
ENDWHILE.
WHILE sy-index < 11.
 WRITE / sy-index.
 CHECK sy-index < 4.
 WRITE 'After CHECK'.
ENDWHILE.

Other  than the above ABAP programming  basics ,we should learn additional skills that ABAP objects which are very important for developing advanced sap application so that object oriented ABAP also part of advanced skills to learn webdynpro.

SAP ABAP interactive ALV program  example


Comments

Popular posts from this blog

BADI Interview Questions in SAP ABAP

Sample SAP ABAP Programming Examples for Practice

Module Pool Programming Interview Questions and Answers in SAP ABAP

Step by Step tutorial on BDC Session Method Program in SAP ABAP

SAP ABAP Interview Questions and Answers for 10 Years Experienced