Lecture 5 Assigning Values, Calculations, Conversions, Control Statements, Debugging BCO5647 Applications Programming Tech...
Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Kruger   Chapter 4 Section 4.3 Chapter 2 Section 2.3.8 <...
Input Parameters <ul><li>A Parameter is a special type of variable. It requires the value of the variable to be input from...
Input Parameters <ul><li>report lec401. </li></ul><ul><li>tables: spfli. </li></ul><ul><li>parameters: carr like spfli-car...
Input Parameters
Changing variables: copy & initialisation <ul><li>You can set a start value for an elementary field yourself using the  VA...
Changing variables :  Initialisation <ul><li>The  CLEAR  statement sets the value of a variable or a structure to zeros. I...
Calculations
Calculations
Conversion Rules:  E lementary Types
Calculations with dates N N N
Control Statements <ul><li>ABAP has 2 conditional logic statements: If/Endif  and Case/Endcase 2 loops:   Do/Enddo  While/...
Logical Expressions
Conditions: If / ElseIf <ul><li>Syntax:  if  logical expression #1. command lines. [elseif  logical expression #2. command...
Logical Expressions & Operators IF statements may be joined using the  AND, OR or NOT operators. Logical expressions may u...
Evaluating Field Contents: C ase/EndCase  Vs If/Endif
Loops :  Do/EndDo <ul><li>An unconditional loop. </li></ul><ul><li>Syntax : do [N times]. Commands. enddo. </li></ul><ul><...
Loops :  While/EndWhile <ul><li>Loop with terminating condition. </li></ul><ul><li>Syntax : while  logical expression comm...
Other control commands <ul><li>The CONTINUE statement inside a loop bypasses all subsequent statements within the loop and...
Debugging a program <ul><li>To investigate runtime errors, ABAP has a simple and efficient debugging facility which allows...
Debugging Mode
Breakpoints in Debugging Mode
Upcoming SlideShare
Loading in...5
×

Lecture05 abap on line

1,271

Published on

Published in: Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,271
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
154
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Input Parameters A Parameter is a special type of variable. It requires the value of the variable to be input from a selection screen. Parameters are defined using the parameters statement. Parameter names can be no more than 8 characters. Why is ABC on the selection screen? (It has been set as the default of PAR2) What will be printed as a result of the write statement? (whatever values the user has input) How can the appearance of the screen be improved? ==&gt; use meaningful labels. HOW? ==&gt; assign the attributes of a table field to the parameter. HOW WILL THIS IMPROVE THE SCREEN? ==&gt; The data element of the table field will give the parameter a label and F1 help, also if a check table is involved it will ensure data integrity of input.
  • Input Parameters parameters: carr like spfli-carrid default ‘LH’. What is the effect of the “like” clause? &gt;&gt; carr is assigned the same field attributes as spfli-carrid - field type and length (CHAR &amp; 3 from domain) - label (from data element - note extra steps required to assign DD field label to parameter - steps outlined in workshop ex. ) - F1 help (from data element) - if check table used in spfli-carrid then a n icon appears at the end of the parameter’s input field - if you click, a list of valid values for the field is displayed
  • What is a CONSTANT? Note: Type s_carr_id reference has been made to the Data Element so that the variable is assigned the DD object attribute (type &amp; length). Each elementary field comes as standard with an initial value appropriate to its type. You can set an start value for an elementary field yourself using the VALUE addition. After VALUES you may only specify a fixed data object. If you want to copy the field contents of variable var1 to a second variable var2 , you can choose one of two syntax variants: MOVE var1 TO var2. var2 = var1. If the two data objects have different types, the type is automatically converted if there is a conversion rule. You can find detailed information about copying and about the conversion rules in the keyword documentation for MOVE The CLEAR statement resets the field contents of a variable to the initial value for the particular type. You can find detailed information about the initial values for a particular type in the keyword documentation about CLEAR .
  • Assignment statements: clear What is the result of the write statement? a1 bb where b is a blank a2 0 a3 0 a4 000 a5 00000000 a6 000000 g1-a1 XXX g1-a2 0
  • Note: TYPE statement in slide example references a STRUCTURE called SBC400FOCC and defined in the Data Dictionary. (Emerald does not have SBC400FOCC but has structure YCHSTRUK_SFLIGHT ) You can precede calculations with the COMPUTE statement. This statement is optional.
  • Calculations COMPUTE is optional See online documentation for COMPUTE for further explanation of functions and DIV &amp; MOD. Note the brackets have spaces except the first when used with a function. What is unusual about the compute lines for SUM, RESULT, ROOT? ==&gt; use of a data field type c What has happened? ==&gt; ABAP/4 automatically carries out a conversion based on a set of conversion rules.
  • Conversion Rules: Elementary Types Conversion rules exits for all combinations of elementary types. A comprehensive overview of conversion rules is contained on the online documentation for the DATA statement. Type c fields are saved left justified. When you convert to type c fields, the field is filled up with blanks if it is too short, and truncated if it is too long. See example 1 &amp; 2 above When you convert a type c field to a type p field, only digits, a plus or minus sign and a decimal point are permitted. (see example 3 above) When you convert a type p field to a type c field, the leading zeros are converted to spaces. (see example 4 above)
  • Calculations with dates What is the result of the program above? today’s day = no of days e.g. 19990113 as sy-datum then DIFF = 13 Why not just display DATEREC-DAY as answer? Program illustrates how dates can be used in calculations. For example, How would you work out the number of days in the current month? Or the number of days from a particular date until today?
  • Other control commands : LOOP ….. ENDLOOP for reading internal tables. AT ….. ENDAT for control level processing within a LOOP statement. Covered later?
  • Logical expressions can be linked with NOT, AND, and OR. You can nest parenthetical expressions as deeply as you want. The parentheses which denote sub-expressions always count as one word. They must therefore be separated by spaces. If you compare two type C fields with unequal length, the shorter field is lengthened to match the length of the longer one when the comparison is made. It is filled from the right-hand end with spaces.
  • The ELSE and ELSEIF statements are optional. Examples : i) if value1 = 22. add 25 to value1. endif. ii) if value1 = 22. add 25 to value1. else. write value1. endif. iii) if w_year1 &gt; w_year2. write ‘Year1’. elseif w_year1 &lt; w_year2. write ‘Year2’. else. write ‘Equal’. endif.
  • co - contains only &lt;f1&gt; co &lt;f2&gt; (case sensitive) is true if &lt;f1&gt; contains only chars from &lt;f2&gt;. ‘ BD___’ co ‘ABCD_’ True ‘ BD___’ co ‘ABCDE’ False ca - contains any &lt;f1&gt; ca &lt;f2&gt; (case sensitive) is true if &lt;f1&gt; contains at least one char from &lt;f2&gt;. ‘ ABcde’ ca ‘Bd___’ True ‘ ABcde’ ca ‘bD___’ False cs - contains string &lt;f1&gt; cs &lt;f2&gt; (not-case sensitive) is true if &lt;f1&gt; contains the char string in (trailing spaces ignored) &lt;f2&gt;. ‘ ABcde’ cs ‘bC___’ True ‘ ABcde’ cs ‘ce____’ False cp - contains pattern &lt;f1&gt; cp &lt;f2&gt; (not-case sensitive) is true if &lt;f1&gt; contains the pattern in &lt;f2&gt;. (* any char, + single char) ‘ABcde’ cp ‘*b*’ True
  • IF and CASE statements allow you to make case distinctions: CASE ... ENDCASE: Only one of the sequences of statements is executed. The WHEN OTHERS statement is optional. IF ... ENDIF: The logical expressions that are supported are described in the documentation about IF . The ELSE and ELSEIF statements are optional. If the logical expression is fulfilled, the following sequence of statements is executed. If the logical expression is not fulfilled, the ELSE or ELSEIF section is processed. If there is no ELSE or no further ELSEIF statement, the program continues after the ENDIF statement. You can include any number of ELSEIF statements between IF and ENDIF . A maximum of one of the sequences of statements will be executed. data: w_year type i. w_year = 1997. case w_year. when 1996. write ‘The year is 1996’. when 1997. write ‘The year is 1997’. when 1998. write ‘The year is 1998.’ when others. write ‘The year is undefined.’ endcase.
  • Examples : i) data n type i, square type i, count type i. n = 100. count = 0. do n times. count = count + 1. square = count ** 2. write square. enddo. ii) n = 100. do n times. square = sy-index ** 2. write square. enddo.
  • Example : A binary search. min = 0. max = 1000. while key &lt;&gt; search_key. key = ( min + max ) div 2. (integer division) if key &gt; search_key. max = key - 1. else. min = key + 1. endif. endwhile.
  • Examples : do 4 times. If sy-index = 2. Continue. Endif. Write sy-index. Enddo. This produces the following output: 1 3 4 The system terminates the second loop pass without processing the write statement. do 4 times. Check sy-index between 2 and 3. Write sy-index. Enddo. This produces the following output: 2 3 The system terminates the first and fourth loop pass without processing the write statement because sy-index does not fall between 2 and 3. do 4 times. If sy-index = 3. exit. Endif. Write sy-index. Enddo. This produces the following output: 1 2 The system terminates the entire loop processing in the thirdloop pass without processing the write statement or the fourth loop pass.
  • Debugging can be invoked by : Start a program by calling the Debugging function from the ABAP Editor initial screen. Enter /h in the command field, press ENTER and choose Execute. From the Object Browser access debugging mode by right-clicking program name and selecting: Execute &gt;&gt; Debugging. When in debugging mode, the program lines appear in the upper half of the screen with the contents of the fields in the lower half. Breakpoints can be line-oriented, field-oriented (watchpoint) or command oriented.
  • Starting the program in the debugging mode allows you to execute the program line by line using the &apos;Single Step&apos; icon. You can display up to eight variables. To trace the variable values, enter the field names in the left input field. You can also see this entry by double-clicking on the field name in the code displayed.
  • You can set a breakpoint by double-clicking in front of a line of source code in the debugging mode. If you then click on the &apos;Continue&apos; icon, the program will be executed up to the point where the next breakpoint is defined. You can find information on content-related breakpoints in the ABAP Statements and Data Declarations unit.
  • Lecture05 abap on line

    1. 1. Lecture 5 Assigning Values, Calculations, Conversions, Control Statements, Debugging BCO5647 Applications Programming Techniques (ABAP)
    2. 2. Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Kruger Chapter 4 Section 4.3 Chapter 2 Section 2.3.8 </li></ul><ul><li>Objectives This lecture will </li></ul><ul><li>Introduce the Input Parameter </li></ul><ul><li>Explore changing variables by copying and initialisation </li></ul><ul><li>Examine how calculations can be made in ABAP </li></ul><ul><li>Examine Control Statements and Logical Expressions in ABAP </li></ul><ul><li>Explore Debugging techniques in ABAP via the Debug Function </li></ul>
    3. 3. Input Parameters <ul><li>A Parameter is a special type of variable. It requires the value of the variable to be input from a selection screen. </li></ul>
    4. 4. Input Parameters <ul><li>report lec401. </li></ul><ul><li>tables: spfli. </li></ul><ul><li>parameters: carr like spfli-carrid </li></ul><ul><li>default ‘LH’. </li></ul><ul><li>select * from spfli </li></ul><ul><li>where carrid = carr. </li></ul><ul><li>write: / spfli-carrid, spfli-connid, </li></ul><ul><ul><ul><li>spfli-cityfrom, spfli-cityto. </li></ul></ul></ul><ul><li>endselect. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write / 'no records found'. </li></ul><ul><li>endif. </li></ul>
    5. 5. Input Parameters
    6. 6. Changing variables: copy & initialisation <ul><li>You can set a start value for an elementary field yourself using the VALUE addition. </li></ul><ul><li>You can copy the field contents of a data object to another data object with the MOVE statement. If the two data objects have different types, the type is automatically converted if there is a conversion rule. </li></ul><ul><li>The CLEAR statement resets the field contents of a variable to the initial value for the particular type. </li></ul>
    7. 7. Changing variables : Initialisation <ul><li>The CLEAR statement sets the value of a variable or a structure to zeros. If the data type is c, the value is instead set to blanks. data: a1(2) type c value ‘AB’, a2 type i value 123, a3 type p value 12345, a4(3) type n value ‘789’, a5 type d value ‘19990112’, a6 type t value ‘1330’, begin of g1, a1(3) type c value ‘ASD’, a2 type i value 123, end of g1. </li></ul><ul><li>clear: a1, a2, a3, a4, a5, a6, g1-a1 with ‘X’, g1-a2. </li></ul><ul><li>write: a1, a2, a3, a4, a5, a6, g1-a1, g1-a2. </li></ul>
    8. 8. Calculations
    9. 9. Calculations
    10. 10. Conversion Rules: E lementary Types
    11. 11. Calculations with dates N N N
    12. 12. Control Statements <ul><li>ABAP has 2 conditional logic statements: If/Endif and Case/Endcase 2 loops: Do/Enddo While/Endwhile and others such as Continue, Check & Exit. </li></ul><ul><li>ABAP does not have a GOTO statement. </li></ul><ul><li>Control commands can be nested and/or joined with logical operators. </li></ul>
    13. 13. Logical Expressions
    14. 14. Conditions: If / ElseIf <ul><li>Syntax: if logical expression #1. command lines. [elseif logical expression #2. command lines. ] [else. command lines. ] endif. </li></ul><ul><li>Examples. </li></ul>
    15. 15. Logical Expressions & Operators IF statements may be joined using the AND, OR or NOT operators. Logical expressions may use the following comparative operators : eq = ne <> >< gt > ge >= lt < le <= between co, ca, cs, cp (string comparisons) Examples.
    16. 16. Evaluating Field Contents: C ase/EndCase Vs If/Endif
    17. 17. Loops : Do/EndDo <ul><li>An unconditional loop. </li></ul><ul><li>Syntax : do [N times]. Commands. enddo. </li></ul><ul><li>The [N times] parameter is optional. How do you end the loop ? </li></ul><ul><li>A system field, sy-index, automatically stores the number of the current loop pass. </li></ul><ul><li>Examples. </li></ul>
    18. 18. Loops : While/EndWhile <ul><li>Loop with terminating condition. </li></ul><ul><li>Syntax : while logical expression commands endwhile </li></ul><ul><li>While the logical expression is true, the sequence of commands is executed. </li></ul><ul><li>Examples. </li></ul>
    19. 19. Other control commands <ul><li>The CONTINUE statement inside a loop bypasses all subsequent statements within the loop and forces the next loop to begin. </li></ul><ul><li>The CHECK statement has the same effect as CONTINUE if the specified logical expression is not true. </li></ul><ul><li>The EXIT statement within a loop structure stops the processing of the loop and the program will continue after ENDDO/ENDWHILE. </li></ul><ul><li>Examples. </li></ul>
    20. 20. Debugging a program <ul><li>To investigate runtime errors, ABAP has a simple and efficient debugging facility which allows you to : </li></ul><ul><ul><ul><ul><li>set breakpoints and stop the program during runtime. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>continue statement by statement. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>skip over subroutines. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>view and change the contents of variables. </li></ul></ul></ul></ul>
    21. 21. Debugging Mode
    22. 22. Breakpoints in Debugging Mode
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×