Friday, April 30, 2010

for loop sample program

sample program using for loop in java.
this program will ask the user to input how many numbers they want to print:

import java.util.Scanner;

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

System.out.print("How many numbers you want to print:");
num=input.nextInt();

for(x=0; x<=num; x++)
{
System.out.println(x");
}
}
}

Thursday, April 29, 2010

for Loop

for loop is a part of programming to execute program continuesly until it reaches the final looping of final number of loop.

example program.

int x;
for(x=0; x<=10; x++)
{
System.out.println(x);
}

sample output:

0
1
2
3
4
5
6
7
8
9
10

Tuesday, April 27, 2010

syntax in programming

syntax is a rules in programing.

for loop sysntax:

for(init; condition; inc/dec)
{
statements;
}

Monday, April 26, 2010

Swap number using java program

1. /*
2. Swap Numbers Java Example
3. This Swap Numbers Java Example shows how to
4. swap value of two numbers using java.
5. */
6.
7. public class SwapElementsExample {
8.
9. public static void main(String[] args) {
10.
11. int num1 = 10;
12. int num2 = 20;
13.
14. System.out.println("Before Swapping");
15. System.out.println("Value of num1 is :" + num1);
16. System.out.println("Value of num2 is :" +num2);
17.
18. //swap the value
19. swap(num1, num2);
20. }
21.
22. private static void swap(int num1, int num2) {
23.
24. int temp = num1;
25. num1 = num2;
26. num2 = temp;
27.
28. System.out.println("After Swapping");
29. System.out.println("Value of num1 is :" + num1);
30. System.out.println("Value of num2 is :" +num2);
31.
32. }
33. }
34.
35. /*
36. Output of Swap Numbers example would be
37. Before Swapping
38. Value of num1 is :10
39. Value of num2 is :20
40. After Swapping
41. Value of num1 is :20
42. Value of num2 is :10
43. */

this program will swap number numbers.

Sunday, April 25, 2010

Data Types

Data types
1. Boolean
2. byte
3. char
4. double
5.float
6.int
7.short

next Lesson i will explain to you all data types. .

Saturday, April 24, 2010

Break statement in java

Break statement is one of the several control statements Java provide to control the flow of the program. As the name says, Break Statement is generally used to break the loop of switch statement.

Please note that Java does not provide Go To statement like other programming languages e.g. C, C++.

This form of break statement is used to jump out of the loop when specific condition occurs. This form of break statement is also used in switch statement.

For example,


for(int var =0; var < 5 ; var++){
System.out.println("Var is : " + var);
if(var == 3)
break;
}


Labeled Break Statement
The unlabeled version of the break statement is used when we want to jump out of a single loop or single case in switch statement. Labeled version of the break statement is used when we want to jump out of nested or multiple loops.
For example,
Outer:
for(int var1=0; var1 < 5 ; var1++)
{
for(int var2 = 1; var2 < 5; var2++)
{
System.out.println("var1:" + var1 + ", var2:" + var2);

if(var1 == 3)
break Outer;

}
}


next lesson about continue statement

Friday, April 23, 2010

Factoring in java program

this program will show how to get the factorial using java program.

public class NumberFactorial {

public static void main(String[] args) {

int number = 5;

/*
* Factorial of any number is !n.
* For example, factorial of 4 is 4*3*2*1.
*/

int factorial = number;

for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}

System.out.println("Factorial of a number is " + factorial);
}
}