Jul 17 2009

Flow Statements-If & Switch

Category: Javaadmin @ 10:31 pm

  • If
public class flow1
{
	public static void main(String[] args)
	{
	int x=15;
	if (x>10)
		{
			System.out.println("x is grater than 10 ");
			System.out.println(x>10);
		}
	else
		{
			System.out.println("x is not grater than 10");
			System.out.println(x>10);
		}
	}
 
}

Result
x is grater than 10
true

  • Switch
public class flow2
{
		public static void main(String[] args)
		{
			int x=2;
			switch (x)
			{
				case 1:
					System.out.println("You have selected num 1");
					break;
				case 2:
					System.out.println("You have selected num 2");
					break;
				case 6:
					System.out.println("You have selected num 6");
					break;
				default:
					System.out.println("You haven't selected any num");
 
			}
 
		}
}

Result

You have selected num 2

Tags: , , , ,


Jul 04 2009

The Difference between ++x and x++

Category: Javaadmin @ 12:04 am

public class PrePostDemo
{
	public static void main(String[] args)
		{
		int a = 4;
		int b = 4;
 
		System.out.println(a);
		a++;
		System.out.println(a);
		a++;
		System.out.println(a++);
 
		System.out.println(b);
		++b;
		System.out.println(b);
		++b;
		System.out.println(++b);
		}
}

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code a++; and ++b; will both end in result being incremented by one. The only difference is that the prefix version (++b) evaluates to the incremented value, whereas the postfix version (a++) evaluates to the original value.

Tags: , ,


Jul 01 2009

Arithmetic Operators

Category: Javaadmin @ 11:12 pm

public class ArithmeticDemo
{
	public static void main (String[] args)
	{
 
        int result = 1 + 2; // result is now 3
        System.out.println(result);
 
        result = result - 1; // result is now 2
        System.out.println(result);
 
        result = result * 2; // result is now 4
        System.out.println(result);
 
        result = result / 2; // result is now 2
        System.out.println(result);
 
        result = result + 8; // result is now 10
        System.out.println(result);
 
        result = result % 7; // result is now 3
        System.out.println(result);
 
	}
}

Tags: ,


Next Page »