Java Tutorial Part-2

Java : Part 2

Yup..
Today im gonna start off with the part 2 of my tutorials.
Dad's OK and i have some free time so decided to make this tutorial
So in the last tutorial,we stopped at For Loop which executes a loop of code a particular amount of time . So today , lets go on to some examples of For loop.

Example


Code:
class loop
{
public static void main(String args[])
{
int a
for(a=1;a<=3;a++)
{
System.out.println("Showing you a loop")
}
}
}

So in the above example ,you can see that i have made a for loop which runs 3 times.Because a is 1 in the beginning and it executes the loop until a<=3 since that's the limit given.So "Showing you a loop" will execute 3 times before exiting the loop

NESTED LOOPS

Nested Loops are loops within loops.That means,one loop inside another loop.This can be used for more complex situations..Hm.. Let me think.
Suppose you want to create a pattern like this :



Code:
1
12
123
1234
12345
So this look beautiful and this types of pattern can be made using Nested for loop.I will show the syntax needed to make that pattern
Code:
for(a=1;a<=5;a++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println("");
}

In the above program snippet ,first the value of a=1 .
Then it enters the nested loop where j's value is 1.
It checks whether its <=i .Since its true,j (j=1) will be printed once.
Then j's value is incremented to 2 .Now it checks the condition again and since i =1 ,the condition is false ,so it terminates out of the loop
Since a System.out.println("") is given,it will bring the cursor to the next line.
Now i's value becomes 2 .So this time j will continue till its value becomes 2 hence 1 and 2 will be printed
This continues till that pattern is printed


While Loop

So now let's move on to another type of loop,known as While loop.
In for loop,we have seen that the initial value,test condition and increment is placed all at one place.But in a while loop,the things are little different.I will demonstrate it with an example

Code:
while(condition)
{
Statements
increment
}
So the while loop begins with a while word followed by the condition for the loop to execute ,inside the braces.If the condition is true,the statements inside the loop are executed.
Remember that the increment is placed inside the while loop.
Now another thing is ,the initial value of the variable that you are using for the loop is declared outside the loop,somewhere above it.I will illustrate it with an example


Code:
int a=1;
while(a<5)
{
System.out.println("while loop example")
a=a++
}
So its pretty easy.Just follow through it

Application of while loop

The while loop can be used in a variety of situations and its one of the most used loops So before beginning,let me say one thing,its not necessary to increment the value of the variable.Anything can be done to the variable

A PROGRAM TO CHECK WHETHER THE ENTERED NUMBER IS A Palindrome number or not
Quote:class odd_even
{
public static void main(int n)
{
int a,b,s;
a=n;
s=0;
while(n!=0)
{
b=n%10;
s=s*10+b;
n=n/10;
}
if(a==s)
System.out.println("The entered number is a palindrome number");
else
System.out.println("The entered number is not a palindrome number");
}
}

So this program may look absurdly difficult to you.But this is one of the easiest programs that you will ever run into,in your life.

If you look at the program again,you can see that a number is entered by the user into the variable n.
Now the console assigns the value of a as n .You will understand why its done in the end.Now the console enters into the while loop checking sure that the entered number is not 0.
Now we use the modulus (%) symbol.As we have learned earlier,a modulus symbol is used to get the remainder of two variables when they are divided.So 5%2 will return 1. 6%2 will return 0.

So lets assume the entered number is 121.
Its value is assigned to a
We enter the loop
b's value becomes 121 because 121%10 = 1
Now s=s*10+b which means s=0*10+1
n=n/10 n=121/10 = 12

So next time, b's value will be 12%10 = 2
s=1*10+2 (s became 1 on the last step and b is 2.s becomes 12)

Third time,s will be equal to 121 .
Now the console checks whether a (121) is = s (121) .Since the condition is true, it will print the entered number is a palindrome number.
Just work around with these loops,try to create more programs..
Good luck