You are on page 1of 12

1/23/2011

INTRODUCTION TO ABAP PROGRAMMING: SYNTAX FUNDAMENTALS

Spring 2011

Enterprise Programming

From the textbookChapter Two


ABAP Workbench Object Navigator (SE80) Forward navigation 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. Keyword Documentation

1/23/2011

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. Technical and (potentially) semantic meaning 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 f c n string xstring x d t p 4 byte whole number +/- 2.1 billion 8 bytes, 15-16 significant digits up to 65k characters up to 65k characters (non-math number) dynamic length up to 2 GB long! dynamic length byte sequence up to 65k bytes 8 characters in form YYYYMMDD 6 characters in from HHMMSS precise whole or floating number up to 16 bytes Bold italics indicate incomplete data types (size set on declaration) integer float string numeric string string hex string byte sequence date time packed number

1/23/2011

Variable Declarations
DATA varname TYPE [type specification]
DATA age TYPE i. "complete data type DATA siblings TYPE i VALUE 7. "w/ initialization DATA state TYPE c LENGTH 2 VALUE 'TN'. "incomplete

Use of chained statements (more typical):


DATA: age TYPE i, siblings TYPE i VALUE 7.

Other variations (considered obsolete) exist. LIKE can be used to declare a variable based on a prior declaration
DATA state2 LIKE state. "only data type 'copied'

When specifying default values for numeric data with decimals, the value must be placed within single quotes.

Declaring your own data typesprogram 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: userval TYPE i, usercode TYPE n LENGTH 10, rate TYPE p LENGTH 3 DECIMALS 2.

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.

1/23/2011

Declaring your own data typesglobal


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 labeled 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?

Quick Practice

1/23/2011

Constants
CONSTANTS allows specification of fixed data objects. CONSTANTS name TYPE type VALUE value.
CONSTANTS a_const TYPE d VALUE '19681214'. CONSTANTS b_con TYPE p LENGTH 3 DECIMALS 2 VALUE '37.46'.

A number of system-maintained constants exists. These are all within structure SYST. Can see list through dictionary, data type SYST.
Reference values by using the identifier SY-COMPONENT. For example, SY-MANDT will display the current client number.

Arithmetic and assignment


Valid arithmetic operators: +, -, *, /, ** (exponentiation), DIV (integer division), MOD. Parenthesis can be used to set order of precedence. Operators and parentheses are keywords and must be separated from other statement elements by at least one space. Assignment syntax: MOVE var2 TO var1. var1 = var2. varx = 3 + 7 * 2. If variables of different type, automatic type conversion attempted. CLEAR varx. Resets varx to default type-related value.

1/23/2011

Writing output
WRITE is used for basic output. WRITE 10 'Output'. Output is written beginning in column 10. WRITE (3) 'Output'. 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.
PARAMETERS age TYPE i. PARAMETERS: var1 TYPE c LENGTH 8, var2 TYPE i.

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 var1 TYPE c LENGTH 8 DEFAULT 'NONE'.

1/23/2011

Quick Practice

Literal text
ABAP is multilingual. Coding in string literals (prompting, etc.) defeats language independence and should only be done in testing. The solution for this is text symbols. Text symbols belong to each program in the text pool for that program. The various texts are placed in the text pool and assigned a 3-character alphanumeric code (xxx). This code is then used by specifying either TEXT-xxx instead of the literal. literal(xxx) 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.

1/23/2011

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 =, EQ <>, NE <, LT Operation Is equal to Is not equal to Is less than Operator AND OR NOT Boolean Operators Operation Logical and Logical or Logical not

>, GT
<=, LE >=, GE

Is greater than
Is less than or equal to Is greater than or equal to

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. 1 or more statements. ENDIF. IF logical_expression. 1 or more statements. ELSE. 1 or more statements. ENDIF. IF logical_expression. 1 or more statements. ELSEIF logical_expression. 1 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.

1/23/2011

Conditional code execution: CASE


CASE data_object. WHEN value. 1 or more statements. WHEN value. 1 or more statements. WHEN OTHERS. 1 or more statements. CASE var_a. WHEN 1. WRITE \ 'uno'. WHEN 2. WRITE \ 'dos'. WHEN OTHERS. WRITE \ 'other'.

ENDCASE.

ENDCASE.

Quick Practice

1/23/2011

Using 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 leftsingle 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
ABAP supports definite iteration, pre-test iteration, and post-test iteration.
DO value TIMES. 1 or more statements. ENDDO. WHILE condition. 1 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 < 10. var_a = var_a * 2. ENDWHILE. DO. var_a = var_a * 2. IF var_a > 200. EXIT. ENDIF. ENDDO.

Within loops sy-index is a system managed loop counter.

10

1/23/2011

Exiting Loops
EXIT: Loop exits immediately. Jump to statement that follows loop block. CONTINUE: Restart next loop iteration.
WHILE sy-index < 10. WRITE / sy-index. IF sy-index < 3. CONTINUE. ENDIF. WRITE 'After ENDIF'. ENDWHILE.

CHECK condition: Restart next loop iteration (continue) if false.


WHILE sy-index < 10. WRITE / sy-index. CHECK sy-index < 3. WRITE 'After CHECK'. ENDWHILE.

Quick Practice

11

1/23/2011

Copyrights
Presentation prepared by and copyright of Dr. Tony Pittarese, East Tennessee State University, Computer and Information Sciences Dept. (pittares@etsu.edu) Podcast lecture related to this presentation available via ETSU iTunesU.
Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Oracle is a registered trademark of Oracle Corporation. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company. Other products mentioned in this presentation are trademarks of their respective owners.

12