Monday, August 2, 2010

Sample Program of while Loop

this program is using while statement to print 5 hello world in your screen .

here is a sample program;

import java.io.*;

public class samplewhile
{
public static void main(String args[])
{
//declaration
int start=1;
while(start<=5)
{
System.out.print("Hello World");
start++;
}
}
}

While Loop

While Loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.

syntax of while loop

initialization;
while(condition)
{
Statements..
inc/dec;
}

Looping

Looping is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable.

there are three different type of looping, used in programming.
1. while Loop
2. do while Loop
3. for Loop

sample program of switch statement

This program will show to you how switch statement execute by the java compiler.

import java.util.Scanner;

public class SampleSwitch
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
//declaration
int num;

System.out.print("Enter a number:");
num=input.nextInt();

Switch(num)
{
  case 1:
  case 3:
  case 5:
  case 7:
  case 9:
    System.out.print("You enter odd number:");
    break;
case 2:
  case 4:
  case 6:
  case 8:
  case 10:
    System.out.print("You enter even number:");

    break;
default:
  System.out.print("Your number you enter is not found in the case");
}


}
}

understand this program and practice to create your own program using switch statement,,

Switch Statement syntax

the syntax of Switch Statement:

switch(value)
{
case value1:
    statement 1;
  break;
case value 2:
   statement 2;
   break;
 case value n:
   statement ...
   break;
default:
   statement;
}

i hope you understand this syntax in switch statement to create running program.

}

Switch Case Statement

Switch Case Statement is a type of selection control statement that exists in most modern imperative programming languages (e.g., Pascal, C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "goto", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit), offering the potential of faster execution through compiler optimization. In some programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement, select statement or inspect instruction.