You are on page 1of 4

Alistair C.

Rooney 2002 All rights reserved

Java for ABAP programmers Lesson 8


TM

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. Imagine trying 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 introduce decision paths into our code. In Java its very similar except that we make use of braces to mark the beginning and end of 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 reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

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 a Boolean 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 ((a<PI)&&(a>78)||(d==17)) { .code. } Nesting is also available in Java. Remember to indent your code to make it more readable. Like this: if (a<PI) { if (a==73) { code here } } There can be multiple levels of nesting, but remember this can make your coding very obfuscated, so beware. If you find yourself going down more than 5 levels have a long hard look at your program design. One way to get around too many nested if statements is to use the switch statement (more on this later in the lesson).

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

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; else c = 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, short or char. Each case within the switch statement must be terminated with a break statement. Lets have a look at an example of the switch statement in action.

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

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 reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

You might also like