*>COBOL has no exact equivalent syntax to x ? y : z if age < 20 move "What's up?" to greeting else move "Hello" to greeting end-if 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 evaluate color *> can be any type when "torquoise" 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
greeting = age < 20 ? "What's up?" : "Hello"; // Good practice is that all consequents are enclosed in {} // or are on the same line as if. if (age < 20) greeting = "What's up?"; else { greeting = "Hello"; } // Multiple statements must be enclosed in {} if (x != 100 && y < 5) { x *= 5; y *= 2; } if (x > 5) { x *= y; } else if (x == 5) { x += y; } else if (x < 10) { x -= y; } else { x /= y; } // Every case must end with break or goto case switch (color) // Must be integer or string { case "turquoise": case "red": r++; break; case "blue": b++; break; case "green": g++; break; default: other++; break; // break necessary on default }
Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.