Conditionals - in COBOL and Java

COBOL Java
declare age as binary-long = 10
declare greeting as string
*>greeting = age < 20 ? has no directly equivalent syntax in COBOL

if age < 20
   move "What's up?" to greeting
else
   move "Hello" to greeting
end-if

declare x as binary-long = 200
declare y as binary-long = 3
if x not = 100 and y < 5
   multiply 5 by x
   multiply 2 by y
end-if



*> evaluate is preferred in COBOL rather than if/else if/else
evaluate x
when > 5
   multiply y by x
when 5
   add y to x
when < 10
   subtract y from x
when other
   divide y into x
end-evaluate





declare color as string = "blue"
declare r b g other-color as binary-long
evaluate color   *> can be any type
when "pink"
when "red"
   add 1 to r
when "blue"
   add 1 to b
when "green"
   add 1 to g
when other
   add 1 to other-color
end-evaluate
public class choices
{

    public static void main(String[] args)
    {
        int age = 10;
        String greeting = age < 20 ? "What's up?" : "Hello";

        if (age < 20)
        {
            greeting = "What's up?";
        } else
        {
            greeting = "Hello";
        }

        int x = 200;
        int y = 3;

        if (x != 100 && y < 5)
        {
            x = 5 * x;
            y = 2 * y;
        }

        if (x > 5)
        {
            x = x * y;
        } 
        else if (x == 5)
        {
            x = x + y;
        } 
        else if (x < 10)
        {
            x = x - y;
        } 
        else
        {
            x = x / y;
        }

        String color = "blue";
        int r = 0, b = 0, g = 0, other_color = 0;

        if (color.equals("pink") || color.equals("red"))
        {
            r++;
        } 
        else if (color.equals("blue"))
        {
            b++;
        } 
        else if (color.equals("green"))
        {
            other_color++;
        }

    }

}

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.