Thursday, 9 January 2014

JUMP STATEMENTS OR BRANCHING STATEMENTS:break

 Jump statements:break,continue,and return:These statements transfer control to another part of your program.

Using break statement


break:


Break statement is one of the several control statements Java provide to control the flow of the program.Break Statement is generally used to break the loop of switch statement.In java,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.

Let’s look at syntax of break statement:

break;



The break statement is written as break; without any expression.

Example for simple break  statement

class BreakDemo
{
  public static void main(String[] args)
  {
  for (int i = 0; i < 5; i++)
  {
  System.out.println(i);
  if (i==3)
  {
  break ;
  }
  }
  }
}

This would produce the following results

0
1
2
3




Saturday, 4 January 2014

Iteration statement:while loop


How to use Java while:

Using while loop

While loop:

The while loop is java’s most fundamental looping statement.It repeats a statement or block while its controlling expression is true.In while statement condition can be any boolean expression.

Iteration statements:for loop in java

The most common iteration Statements in Java

Iteration Statement: for loop


The 'for' LOOP:


The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. If only one statement being repeated, there is no need for the curly braces. When using this version of the for statement, keep in mind that,The initialization expression initializes the loop; it's executed once, as the loop begins.

Switch Statement in Java

How to use Java Switch: Case: Default: 

Using switch statement

Swich:

The switch statement is Java’s multiway branch statement.It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.It is an alternatives of else-if ladder. Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression. The expression used in the switch statement must return an int, a String, or an enumerated value.

Friday, 3 January 2014

Nested if Statement in Java

The most common Control Statements in Java

Control Statement Nested if


The nested if Statement:

Nested if are very common in programming. Nesting an if  Statement just means putting one if Statement inside of another. when you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is with  in the same block as the else and that is not already associated with an else.

Saturday, 2 February 2013

if and if-else control statements in java

The most common Control Statements in Java

Control Statement if and if.. else

The if Statement:


In Java if statement is most widely used. The if statement is control statement which helps programmers in decision making. The if statement uses boolean expression in making decision whether a particular statement would execute or not. This is a control statement to execute a single statement or a block of code, when the given condition is true and if it is false then it skips if block and rest code of program is executed. 

Introduction to Control Statements in Java


Control statements decide flow of a program

JAVA CONTROL STATEMENTS

if, if-else, switch, nested if, switch, for, while, do-while, break, continue and return control statements


Control statements are used in programming languages to cause the flow of control to advance and branch based on changes to the state of a program. The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
In Java, control statements can be divided under the following three categories: