google Ads

Friday, July 17, 2009

Control Structures

Control Structures


In a program all the instructions are executed sequentially by default, when no repetition of some calculations are necessary. When in some situation we may have to change the execution order of statements based on condition or to repeat a set of statements until certain condition are met. In such situation conditional and control statements are very useful

A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, Java language provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

With the introduction of control structures we are going to have to introduce a new concept: the compound-statement or block. A block is a group of statements which are separated by semicolons (;) like all Java statements, but grouped together in a block enclosed in braces: { }:

{ statement1; statement2; statement3; }

Most of the control structures that we will see in this section require a generic statement as part of its syntax. A statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block), like the one just described. In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block.

Control Statement

if Statements
switch Statements

Looping Statements

while loop Statements
for loop Statements
do-while loop Statemnts

No comments:

Post a Comment