26 Jan 2017

What is for-each loop in java..??

You know about the loop concept in java if you don't know then click me ...😎😎

Now come to the point about the for-each loop.This loop is specially designed to handle the elements of a collection . Collection is the group of  elements. You know the array part of java .we can take an array as a collection of similar elements like Integer or String etc., or a class is a collection of similar methods or a package like java.util, is a collection of classes and interfaces .


What is a collection..?

A collection represents a group of elements like integer values or objects or methods classes .Examples for collections are arrays and java.util classes (Stack, LinkedList etc. ).


How can you explain it ..!!

The for-each loop repeatedly execute a group of statements for each element of the collection. It execute as many times as there are number of elements in the collection.

Lets assume an example but first know the syntax of that loop :

Syntax:

for(var: collection) //here the var is a variable attached to a collection .
{
statements;
}


here var represents each element of the collection one by one.Suppose If a collection has 5 element then the loop will execute 5 times and the var represents these elements one by one .

Lets take an example of for-each loop:

class mobile
{public static void main(String ar[])

{
int a[]={5,35,69,96,75}; // declare an integer type array with five elements.

for(int i : a)  //use for-each loop for retrieve the elements in array
{System.out.println(i);}
}
}


Output :
5
35
69
96
75

for more examples of  for-each loop click me....😊😊


What is for loop..??                   What is while loop..??

What is do-while loop..??              What is control statement..??

What is if-else statement..??           What is switch statement..??













,

What is control statements ..??

First of all the main question arises about the statements  that is  "What is a statements in a program ".

The answer is "It is smallest  unit of a program that is a complete instruction in itself. " Statements of java generally contain  expressions a end with a semi-colon (;). The two most commonly used statements in any programming language are as follows:


Sequential statements : These are the statements which are executed one by one .

Control statements : These are the statements that are executed randomly and repeatedly .
These are followed by java also.

Now lets assume an example :

System.out.println("Hello Jarvis");
a=b+c;
System.out.println("a");

So what you think , what type of statement it is

I tell  you that  is the example of Sequential statements as you can see that the execution of statements are in sequence .

What is control statements?

Control statements are the statements which alter the flow of execution and provide better control to the programmer on the flow of execution . They are useful to write better and complex programs.


These are the following control statements are available in java:


  1. if-else statement
  2. do-while loop
  3. while loop
  4. for loop 
  5. for-each loop 
  6. switch statement 
  7. break statement 
  8. continue statement
  9. return statement 





















What is do-while loop..??

This loop is used when there is a need to repeatedly execute a group of statements as long as condition is true.If the condition is false the repetition will be stopped and the execution  comes out of the loop.

So the syntax of do-while loop :

do
{
statements;
}while(condition);

Lets take an example of

class mobile
{public static void main(String ar[])

{int x=1;
do{System.out.println(x);
x++;
}
while(x<=10);
}}

Output:
1
2
3
4
5
6
7
8
9
10

while loop is also called as entry restricted ,similarly the do-while loop is also know as entry level loop.

The question arises what is the differences  between while and do-while loop..??

The difference is that in do while loop , the loop must execute at first time if the condition false or true but in while loop if the condition is false then the JVM comes out to the loop ..


Lets take as example of do-while loop in which the while condition is false :


class mobile
{public static void main(String ar[])

{int x=1;
do{System.out.println(x);
x++;
}
while(x>=10);
}}

When you execute this program ,the loop will execute first time but after that it checks the condition which is false by me so the loop will exit.


 for more examples of do-while loop  click me .....😄😄


What is while loop ..??           What is for loop..??

What is switch statement..??   What is control statements..??











What is while loop..??

As you know that , loop is the executing of set of statement in multiple time. The while loop is same as for loop but the syntax is little difference. The while loop is also called entry restricted loop.

As you know that , all the loop statements are under the control statements, so lets checkout the syntax of while loop:

initialization;

while (condition)
{
statements;
}

increment;

Lets take an example of while loop :

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(a); // you can also use increment direct in print() method .
}
a++; // increment
}
}

Output :
1
2
3
4
5
6
7
8
9
10



Now the question arises why while loop called as entry restricted..??

Because the entry in loop is possible when the condition is true otherwise you cannot enter in the loop that is why while loop is also called entry restricted loop.

Lets take some other Examples:

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(a++); //increment passes in the println() method
}
}
}

Output is same as above appear.

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(++a);
}
}
}

Output:
2
3
4
5
6
7
8
9
10
11


for more examples of while click me...😄😄

What is for loop..??                      What is do-while loop..??

What is switch statement..??        What is JVM..??




















What is method in java??

Method is also called as function in C/C++, but there are many differences between java and c++.

Method: A method is a group of statements or collection of statements are grouped to perform actions as well as operations. As you know that main method is also a method in java program that is called by the JVM.

A method can be called by two ways :


  1. The first way is to creating object of class that belongs to the method .
  2. The second  way is , by declaring as static method .

To know about the static keyword click me..😄😄


Syntax of a method :

public static int methodname(int a )
{
// This is the body to perform action as you like
}

The syntax contains:

 public static: This portion called as modifier , so the question arises what is modifier.

int: It is return type to return some value.

methodname: methodname is the name to call it by using it name.

int a : It is the parameter of method .

and the last thing is the body of that method to perform different type of actions.

you can say that :

modifier returntype methodname (parameter)
{//body}

So lets take an example of method:

class number
{
public static void main(String as[])
{
System.out.println("This is main");
show();
}

public static void show()    //I used static keyword otherwise it will throw error.
{System.out.println("This is show");}

}

Output :
This is main
This is show


If you compile this program you will get an error because static member based on rules

"A non-static method cannot be referenced from static context"

So you must use static keyword other wise you will use that method by creating object of that class 
like :




class number
{
public static void main(String as[])
{
System.out.println("This is main");
number m=new number();
m.show();  //calling method by creating object of that class
}

public static void show()    //I used static keyword otherwise it will throw error.
{System.out.println("This is show method");}

}

The output is same as above appear , this is the second way to call method by using object of class.


What is Static Keyword..??               What is object..??

Why we use Static Keyword..??       What is class..??

Method Examples..!!                 What is loop..??

What is for loop..??             What is while and do-while loop..??

How many Types of loop...??





















24 Jan 2017

Examples of for loop..!

Lets take an example of for loop:

class remote
{public static void main(String b[])
{
   for(int a=0; a<=10; a++)
{
   System.out.println(a);
}
}
}

If you execute this program you will show that the numbers for 0 to 10 and if the condition will false the body will terminate.Now let's some changes in above example print statement out of loop :

class remote
{public static void main(String b[])
{
   for(int a=0; a<=10; a++)
{
   System.out.println(a);
}
System.out.println(a);
}
}

what is the output of this program for more examples comment me ....🙂🙂🙂










What is for loop in java?

First of all the question arises that, what is loop ..?   in simple way the doing same process multiple time is called loop as well in java "the execution of  a set of statements in multiple time is know as loop .  In java there are four types of loop exists :


  •  For loop
  • while loop
  • do-while loop
  • for each loop


Different types of loops have different ways to perform statement execution...🙂🙂🙂

So lets talk about the for loop syntax and their working process: 
 the syntax of for loop

for(initialization; condition; increment)
//statements execute if the conditions is true otherwise it escape from the loop 
{statements}

now how it works as you know about the  if-else condition with the help of boolean expression ,Similarly here the middle is condition ,  

  • first declare or initialize the value 
  •  it moves to the condition statement if the condition is true the statement is execute and then the increment part works and increase the value by one
  •  then again check the condition if true then execute the statement .
  •  JVM executes the statements under the for loop braces until the condition being false   
Lets assume an example of for loop...click me..😉😉