Powered by Blogger.

Saturday 27 February 2016

CONTROL STATEMENTS

No comments :
A Programming Language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program control statements can be put into the following categories:
  • Selection.
  • Iteration.
  • Jump.
Selection

Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or state of a variable.
Java Supports two selection statements:  
  • if
  • switch 
IF:   
The if statements is conditional branch statements.


Here, Condition is a Boolean expression. if condition is true, then the statement is executed. if condition is false, then the statement is bypassed.
Example:



Switch:
The switch statement is multi-way branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.
Here is the general form of a switch statement:

 
Iteration
Iteration statements enable program execution to repeat one or more statements(That is, iteration statements form loops).
Java Supports Iteration statements:
  • for Loop.
  • while Loop.
  • do-while Loop.
For Loop:
The for loop operates as follows.



The initialization portion of the loop sets a loop control variable to an initial value. The condition is a Boolean expression that tests the loop control variable. if the outcome of that test is true, the for loop continues to iterate. if it is false, the loop terminates. The iteration expression determines how the loop control variable is changed each time the loop iterates.
Example:



Output:
                                          This is N:0
                                          This is N:1
                                          This is N:2
                                          This is N:3
While Loop:
The while loop is most fundamental loop statement. It repeats a statements or block while its controlling expression is true.
Here is its general form: 



The condition can be any Boolean expression. The body of the loop will be execution as long as the conditional expression is true. when condition becomes false, control passes to the next line of code immediately following the loop. The curly braces are unnecessary if only a single statement is being repeated.
Example:



Out Put:
                                             This is N:4
                                             This is N:3
                                             This is N:2
                                             This is N:1
Do-while Loop:
A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given Boolean condition at the end of the block.
Its general form is:



Each iteration of the do-while loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates.
Example:



Out Put:
                                             This is N:4
                                             This is N:3
                                             This is N:2
                                             This is N:1

Jump:
Jump statements allow your program to execute in a nonlinear fashion.
Java Supports three Jump statements:
  • Break.
  • Continue.
  • Return.
Break Statement:
The Break statement has three uses. First, as you have seen, it terminates a statement sequence in a switch statement. second, it can be used to exit a loop. Third, it can be used as a "civilized" form of goto.
Continue Statement:
The Continue statement performs such an action. In while and do-while loop, a that controls the loop. In a for loop, control goes first to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression. for  all three loops, any intermediate code is bypassed.
Return Statement:
The last control statement is return. The return statement is used to explicitly return from a method.