# An Introduction to ABAP based on a Prime Factors Example

TL; DR: Our blog series for complete ABAP beginners. This blog article is for you if you are completely new to ABAP and need some insights into the general logic behind it.

The following exercises will help you understand some basic concepts in ABAP.

“Tell me and I forget, teach me and I may remember, involve me and I learn.”

Benjamin Franklin

On exercism.org you will find 40 ABAP exercises. These are common programming exercises such as Word Count, Tic-Tac-Toe, Scrabble Score and so on that you will probably come across in one way or another when learning a programming language. We recommend that you register there and start doing all these exercises to build up your knowledge of programming and ABAP.

What our blog series Introduction to ABAP will do for you:

• Discuss the task and the considerations you need to make to solve the exercise without having to look up a published solution.
• Randomly show you some ABAP peculiarities that come up in each exercise, so that you understand ABAP better and better.

## Learn by example: Prime Factors

https://exercism.org/tracks/abap/exercises/prime-factors

### 1. The job.

Work out the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number. Some more useful school knowledge: Factors are the numbers you multiply together to get another number. For example: Factor 2 * Factor 3 = 6. “Prime factorisation” is working out which prime numbers can be multiplied together to make the original number.

What are the prime factors of 147?

It’s best to start with the smallest prime, which is 2, so let’s see:

Can we divide 147 exactly by 2? 147 ÷ 2 = 73½
No, you can’t. The answer should be an integer, and 73½ is not an integer.

Let’s try the next prime number, 3: 147 ÷ 3 = 49
That worked, now we try to factor 49.

The next prime number, 5, does not work. But 7 does, so we get 49 ÷ 7 = 7.

This is as far as we need to go, because all the factors are prime numbers.
147 = 3 × 7 × 7

Now you have a better understanding of this problem. Have you?

### 3. What are the constraints of the task?

There is no need to scroll up to see the task again. Here it is:

Calculate the prime factors of a given natural number.
A prime number is only evenly divisible by itself and 1.
Note that 1 is not a prime.

Once you understand this, it is actually quite simple. 8 lines of code.

Okay, you’re right, we’re not done yet. Actually, we need 12 lines of code to translate our reflections into ABAP. As you can see here:

### 4. Our solution translated in ABAP.

Note: On exercism.org you will find solutions using classes and methods to calculate prime factors. As our blog series is aimed at complete programming beginners, we skipped the object-oriented programming (for now) and wrote a simple ABAP report.

## ABAP Syntax

Typically, an ABAP statement starts with a keyword and ends with a period. For example:

Unlike other languages such as Java or C#, ABAP is not case-sensitive. This means that you can use the ABAP keywords and identifiers in uppercase or lowercase.

## Data declaration

A typical procedural ABAP program consists of type definitions and data declarations. The data declarations describe the data structure that the program uses when it runs.

Parameters are components of a selection screen that are assigned a global elementary data object in the ABAP program and an input field on the selection screen.

So, after running our example. In a SAP GUI system or in eclipse you would have to press F8 to run a program, before you should have saved (Ctrl + s) and activated (Ctrl + F3) your program. After these three steps, you will see that parameters is a data declaration that opens an additional screen (dynpro) in the SAP GUI that receives your input variable.

ABAP has three main types of data: elementary types, complex types and reference types. In our example, only elementary types are used. Therefore, we will only give a brief overview of them in the following table:

## DO/ENDDO and WHILE/ENDWHILE

In ABAP there are two loops that could have helped us solve our prime factor exercise: while/endwhile or do/enddo.

DO/ENDDO: Unconditional and indexed loops:

The block of statements between DO and ENDDO is executed continuously until the loop is exited using exit statements such as EXIT. Specify the maximum number of loop passes, otherwise you may get an infinite loop.