Showing posts with label control flow. Show all posts
Showing posts with label control flow. Show all posts

Monday, 23 May 2011

Conditional operators in java

The Conditional operator is the only ternary (operator takes three arguments) operator in Java. The operator evaluates the first argument and, if true, evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left: (a?b?c?d:e:f:g) evaluates as (a?(b?(c?d:e):f):g)

int x = 10, y = 12, z = 0;
z = x > y ? x : y;
//As x>y is false, z=y

Thursday, 21 April 2011

Continue, Break and Return

Continue statements are used in looping statements to force another iteration of the loop before reaching the end of the current one. Most often continue is used as part of a conditional statement that only happens in certain cases. The following is a trivial example.
int x=0;
while (x<10)
{
x++;
System.out.println(x);
continue;
// you will never get to this point!!
};
Break statements are used in looping and 'switch' statements to force an abrupt termination or exit from the loop or switch. In the following example the loop is never completed. Once again the normal use of break is as part of a conditional statement.
int x=0;
while (x<10)
{
x++;
System.out.println(x);
break;
// you will never get to this point!
};
Return statements are used to force a quick exit from a method. They are also used to pass values back from methods.

Switches in java

Switch (aka case) statements are used to select which statements are to be executed depending on a variable's value matching a label. default is used for the else situation. The selection variable can only be int or char datatype.
switch (selection)
{
case '1':System.out.println("You typed a 1");break;
case '2':System.out.println("You typed a 2");break;
default:System.out.println("Oops!, that was an invalid entry.");
};

Loops in java

For statements allow a set of statements to be repeated or looped through a fixed number of times. The round bracket contains initializer(s) ; conditional test ; incrementer(s). If more than one initializer or incrementer is used they are separated with commas. The test condition is checked prior to executing the block. Incrementing is done after executing the block. To output #1 #2 etc.(or #5 #4 etc.) on separate rows you could write:
for (int i=1;i<=5;i++) {document.writeln("#"+i);};
for (int i=5;i>=1;i--) {document.writeln("#"+i);};
Note: A special case of the for loop is the infinite loop for (;;){...}. This could be used with appropriate 'breakout' logic where a continuous operation was needed.
Caution: For loops can be written in ways that violate structured programming principles by allowing counter variables to be used outside of the scope of the loop block if they are defined outside the loop. Avoid the looseness of the language by always localizing the loop variable.
The enhanced for statement allows iteration over a full set of items or objects. For example:
int[] squares={0,1,4,9,16,25};
for (int square : squares) {...;}
// is equivalent to
for (int 1;i<squares.length;i++) {...;}
While statements allow a set of statements to be repeated or looped until a certain condition is met. The test condition is checked prior to executing the block. To output #1 #2 etc. on separate rows you could write:
int i=0;
while (i<=5) {
document.writeln("#"+i); i++;
};
Do While statements allow a set of statements to be repeated or looped until a certain condition is met. The test condition is checked after executing the block. To output #1 #2 etc. on separate rows you could write:
int i=1;
do {
document.writeln("#"+i); i++;
} while (i<=5);
Nested loops are possible. Be very careful about modifying any indices!.
for (int i=0;i<10;i++) {
for (int j=i;j<10;j++) {
System.out.print("*");}
System.out.println();}

Conditional Statements - if-else statement and ? operator

Conditional statements execute a block or set of other statements only if certain conditions are met. The condition is always enclosed in round brackets. The statements to be performed if the condition is true are enclosed in curly brackets. For example:
if (value>5) {x=7;}
Note: If the block (ie. curly brackets) contains only one statement, you may leave the brackets off. But this is not good programming practice. The curly brackets make the code more consistent and easier to read.
Occasionally you may want to perform some actions for the false outcome of a condition as well. The else keyword is used to separate branches.
if (name=="Fred") {x=4;}
else {x=20;};
Note: When the conditional if statement is used only to make an assignment to one variable, you can use the terse C conditional operator. A simple example is:
x=(name=="Fred") ? 4 : 20;

See here for conditional operator.