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 Equality and Relational Operators

Category: Javaadmin @ 1:26 pm
==	equal to
!=	not equal to
>	greater than
>=	greater than or equal to
<	less than
<=	less than or equal to
public class ComparisonDemo
{
	public static void main (String[] args)
	{
		int x = 3;
		int y = 5;
		if (x==y)
			System.out.println("equal to");
		if (x!=y)
			System.out.println("not equal to");
		if (x&gt;y)
			System.out.println("greater than");
		if (x&gt;=y)
			System.out.println("greater than or equal to");
		if (x&lt;=y)
			System.out.println("less than or equal to");
	}
 
}

Out put
not equal to
less than
less than or equal to

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: , ,


Next Page »