Wednesday, April 9, 2014

What are different Control Statements available in Apex?

What are different Control Statements available in Apex?


The control statement are used to control the flow of execution of the program . This execution order depends on the supplied data values and the conditional logic. Apex contains the following types of control statements:

  1. Selection Statements
  2. Repetition Statements
  3. Branching Statements 
Selection statements:
If

The if statement is apex conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the if statement:

if (condition) statement1;
else statement2;

Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is any expression that returns a boolean value. The else clause is optional.

The if works like this: If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is executed. In no case will both statements be executed. For example, consider the following:
i
nt a, b;
// ...
if(a < b) a = 0;

else b = 0;


Nested ifs

A nested if is an if statement that is the target of another if or else. Nested ifs are very common
in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. Here is an example:

if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)

The if-else-if Ladder

A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder. It looks like this:

if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;

The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. The final else acts as a default condition; that is, if all other conditional tests fail, then the last else statement is performed. If there is no final else and all other conditions are false, then no action will take place.

Repetition Statements:

While Loops

The Apex while loop repeatedly executes a block of code as long as a particular Boolean condition remains true. Its syntax is:

while (condition) {
code_block
}

the while loop checks the Boolean condition statement before the first loop is executed. Consequently, it is possible for the code block to never execute. As an example, the following code outputs the numbers 1 - 10 into the debug log:

Integer count = 1;
while (count < 11) {
System.debug(count);
count++;
}

Do-While Loops

if the conditional expression controlling a while loop is initially false, then the body of the loop will not be executed at all. However, sometimes it is desirable to execute the body of a loop at least once, even if the conditional expression is false to begin with. The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop.

do {
code_block
} while (condition);
Apex do-while loop does not check the Boolean condition statement until after the first loop is executed.Consequently, the code block always runs at least once.

As an example, the following code outputs the numbers 1 - 10 into the debug log:

Integer count = 1;
do {
System.debug(count);
count++;
} while (count < 11);

For

Here is the general form of the traditional for statement:
for(initialization; condition; iteration) {
// body
}

The for loop operates as follows. When the loop first starts, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop control variable, which acts as a counter that controls the loop. It is important to understand that the initialization expression is only executed once. Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value. If this expression is true, then the body of the loop is executed. If it is false, the loop terminates.Next, the iteration portion of the loop is executed. This is usually an expression that increments or decrements the loop control variable. The loop then iterates, first evaluating the conditional expression, then executing the body of the loop, and then executing the iteration expression with each pass. This process repeats until the controlling expression is false.

// Demonstrate the for loop.

class ForTick {
integer n;
for(int i=0; i<10; i++)
System.debug(‘I value is : ‘ + i);
}


The For-Each Version of the for Loop

The advantage of this approach is that no new keyword is required, and no preexisting code is broken. The for-each style of for is also referred to as the enhanced for loop. The general form of the for-each version of the for is shown here:

for(type itr-var : collection) statement-block

Here, type specifies the type and itr-var specifies the name of an iteration variable that will receive the elements from a collection, one at a time, from beginning to end. The collection being cycled through is specified by collection.

Integer[] myInts = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (Integer i : myInts) {
System.debug(i);
}

Branching Statements:

Return statements:

It is a special branching statement that  transfers the control to the caller of the method. This statement is used to return a value to the caller method and terminates execution of method. This has two forms: one that returns a value and the other that can not return. the returned value type must match the return type of  method. 

Syntax:  return;
 
return values;
return;   //This returns nothing. So this can be used when method is declared with void return type.
return expression;   //It returns the value evaluated from the expression.
Example: Here Welcome() function which returns a String value "Welcome to sfdcsrini.blogspot.com". This is printed to the screen.

static String Welcome(){
  return 'Welcome to sfdcsrini.blogspot.com';
}





0 comments:

Post a Comment

 
| ,