User Tools

Site Tools


products:ict:erp-crm-scm:sap:abap:abap_basics

# ABAP Basics

In this section, we'll delve into the fundamental building blocks of ABAP (Advanced Business Application Programming), covering ABAP data types, variables and constants, operators, and control structures.

## ABAP Data Types

ABAP provides a rich set of data types to work with different kinds of data. These data types can be categorized into the following main groups:

1. Elementary Data Types: These are the basic data types used to store single values. Some common elementary data types include:

  1. `C`: Character (fixed-length)
  2. `N`: Numeric characters
  3. `I`: Integer
  4. `F`: Floating-point
  5. `D`: Date
  6. `T`: Time
  7. `X`: Hexadecimal
  8. `STRING`: Variable-length character string (Unicode-enabled)

2. Structured Data Types: These data types allow you to work with complex structures. Examples include:

  1. `STRUCTURE`: User-defined structure
  2. `TABLE`: Internal table
  3. `INTERNAL TABLE`: Dynamic internal table
  4. `HASHED TABLE`: Hashed internal table
  5. `INDEX TABLE`: Index table

3. Reference Types: Reference types are used for creating references to data objects. The primary reference type is `DATA`.

4. Object-Oriented Types: If you're working with object-oriented programming in ABAP, you'll use types like `CLASS` and `INTERFACE` to define classes and interfaces.

## Variables and Constants

In ABAP, you can declare variables and constants to store and manipulate data. Here's how you declare and use them:

### Variables

```abap DATA: lv_integer TYPE i, “ Declaration with a type

    lv_string  TYPE string, " Declaration with a predefined data type
    lv_custom  TYPE zcustom. " Declaration with a user-defined type (e.g., a structure)

```

### Constants

```abap CONSTANTS: c_max_attempts TYPE i VALUE 3, ” Constant with a value

         c_message       TYPE string VALUE 'Hello'. " Constant with a string value

```

## Operators and Expressions

ABAP supports various operators for performing operations on data. These include:

1. Arithmetic Operators:

  1. `+` (addition)
  2. `-` (subtraction)
  3. `*` (multiplication)
  4. `/` (division)
  5. `` (exponentiation) - `DIV` (integer division) - `MOD` (modulo) 2. Comparison Operators: - `=`, `EQ` (equal) - `<>`, `NE` (not equal) - `<`, `LT` (less than) - `>`, `GT` (greater than) - `⇐`, `LE` (less than or equal to) - `>=`, `GE` (greater than or equal to) 3. Logical Operators: - `AND`, `&&` (logical AND) - `OR`, `||` (logical OR) - `NOT`, `¬` (logical NOT) - `XOR` (logical XOR) 4. String Concatenation: - `&` (concatenate strings) 5. Assignment Operator: - `=` (assign value) 6. Bitwise Operators: - `BIT-AND` - `BIT-OR` - `BIT-XOR` - `BIT-NOT` 7. Other Operators: - `CO` (contains only) - `CP` (contains pattern) - `CS` (contains substring) - `CN` (contains none) ### Expressions You can use operators to create expressions, which are combinations of variables, constants, and operators that produce a result. For example: ```abap DATA: lv_total TYPE i, lv_price TYPE p DECIMALS 2 VALUE '12.34', lv_quantity TYPE i VALUE 5. lv_total = lv_price * lv_quantity. ``` ## Control Structures (if, loops, etc.) ABAP supports various control structures for flow control and decision making: 1. Conditional Statements (`IF`, `ELSEIF`, `ELSE`, `ENDIF`): ```abap IF condition. “ Code to execute if condition is true ELSEIF another_condition. ” Code to execute if another_condition is true ELSE. “ Code to execute if none of the conditions are true ENDIF. ``` 2. Case Statements (`CASE`, `WHEN`, `ENDCASE`): ```abap CASE variable. WHEN value1. ” Code to execute for value1 WHEN value2. “ Code to execute for value2 … WHEN OTHERS. ” Code to execute if none of the above cases match ENDCASE. ``` 3. Loops (`DO`, `WHILE`, `ENDDO`, `LOOP`, `ENDLOOP`):**

```abap DO n TIMES.

" Code to execute n times

ENDDO.

WHILE condition.

" Code to execute while condition is true

ENDWHILE.

LOOP AT internal_table INTO work_area.

" Code to iterate through an internal table

ENDLOOP. ```

These control structures help you control the flow of your ABAP programs, make decisions, and iterate over data structures like internal tables.

In this section, we've covered the foundational aspects of ABAP, including data types, variables, constants, operators, and control structures. These are essential building blocks for writing ABAP programs to interact with SAP systems and perform business logic.

products/ict/erp-crm-scm/sap/abap/abap_basics.txt · Last modified: 2023/10/05 15:40 by wikiadmin