java for abap programmers 8

Upload: aaron-villar

Post on 03-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Java for ABAP Programmers 8

    1/4

    Alistair C. Rooney 2002 All rights reserved

    JavaTM for ABAP programmers Lesson 8

    Lesson 8 Control Flow (Part 1) The if, ? and switch Statement.

    A program that hurtles through code in a linear fashion is not terribly exciting. Imaginetrying to play a game of chess where you knew all the moves in advance.

    Ill say one thing, its very nice to debug!

    As you know from ABAP, there are plenty of times where you want to condition your

    code. In ABAP we use the IF, ELSE, ELSEIF and ENDIF. keywords to introducedecision paths into our code.

    In Java its very similar except that we make use of braces to mark the beginning and endof our code.

    Having said that, it is perfectly legal(see style note) to leave the braces off when you

    have only one line in your if or else statement.

    For Example:

    if (b==5)c = 73;

    else

    c = 0;

    It is not, however, professional to do this as it can easily lead to misinterpretations.

    Style Note: Always use braces with if and else statements.

    For Example:

    if (b==5){

    c = 73;}else{

    c = 0;}

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

  • 7/28/2019 Java for ABAP Programmers 8

    2/4

    Alistair C. Rooney 2002 All rights reserved

    If Statements in Java need to be followed by brackets. The expression between the

    brackets must resolve back to aBoolean value. If not the compiler will tell you when you

    try to compile the Java program.

    You should be used to doing this in ABAP, but lets take a quick look at this now!

    In ABAP the expression could be IF myNewVariable eq wa_newvariable.

    In Java we would write this as if (myNewVariable = = wa_newvariable).

    Notice the double equals in Java! A single equals in Java is always an assignment

    whereas the double equal is the comparison operator.

    You are free to do complex comparisons in Java provided you use the && (and) and the ||

    (or) operators correctly and encase the whole expression in brackets.

    For example:

    if((a78)||(d==17)){

    .code.}

    Nesting is also available in Java. Remember to indent your code to make it more

    readable.

    Like this:

    if(a

  • 7/28/2019 Java for ABAP Programmers 8

    3/4

    Alistair C. Rooney 2002 All rights reserved

    There is a shortcut operator in Java for doing an if statement in one line. This is called the

    ? operator.

    Using the ? and : operators.

    This replaces the traditional if operator by using the ? operator for true values and the :operator for false values.

    For example:

    if (b==5)c = 73;

    elsec = 0;

    can be written like this:

    c = (b==5)?73:0;

    In other words if b equals 5 the c is equal to the value after the ? (73) else the value

    would be whatever follows the : (0).

    Pretty nifty hey! This is nice for embedding within other code e.g.

    System.out.println(The answer is +(b==5)?73:0);

    OK so far so good. All of this should be second nature to anyone who has programmed

    before. Now onto the switch statement.

    The switch statement.

    The switch statement is a little disappointing in Java. In ABAP and in other languages we

    have powerful case statements that can check each level individually.

    The Switch statement can only test an int, byte, shortorchar.

    Each case within the switch statement must be terminated with a breakstatement.

    Lets have a look at an example of the switch statement in action.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

  • 7/28/2019 Java for ABAP Programmers 8

    4/4

    Alistair C. Rooney 2002 All rights reserved

    int option = x;switch(option){ case 1:

    System.out.println(You selected 1);

    break; case 2:System.out.println(You selected 2);

    break; case 3:

    System.out.println(You selected 3); break; default:

    System.out.println(You selected something else); break;}

    Not very difficult is it? But it is a little limited.

    Next time well look at looping with Java. The more advanced students can have a look

    at the Iterator and Enumerator classes.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.