Looping in java
Before we know about loop first to fall we need to understand which type of problem we will phase
If we can imagine programming without loop.
Example: if we can say wap for print 1 to 100. And we don’t know about loops.
So what is our approach :
- System.out.print(“1 2 3 4 5……99 100”);
- And if we need output like this
Then we will write System.out.println(“1”); System.out.println(“2”); System.out.println(“3”);
So we can see if we don’t know about loop so our life in programming its so difficult whenever
We need to iteration in programme plus our time and efficiency of a programme both will effect.
So if you want overcome from this problem then one person is here for our help and that person
Name is loop.
What is loop: Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Java provides Four ways for executing the loops.
- While Loop
- Do While Loop
- For Loop
- For Each Loop
So now above programme will make very easily with loops and complete with some statements.
Now next question is why java provide for way of looping?
Ans: all are use for looping purpose in java but way of working is totally different like
If i will say WAP for add two numbers
Then there is a different way to write this programme but output will be same.
First Way : int c=a+b;
System.out.println(“result of c is :”+c);
Second Way : System.out.println(“output is this:”+a+b);
Third Way : System.out.println(“result is::”+10+20);
See output is same but way of working is different same if we will use looping in java then we have
Four ways but goal is same but we need to decide which way is better as per requirements.
Now first to fall we will solve above problem with help of while loop.
here is a link for that…
Now we will solve above problem with help of do while loop.