Loop in Java With Summations
Let’s Discuss About What is Loops in Java:
In simple word loops in java are used to execute a block of code more than once.
Example:
Int i = 1;
while(i<=5)
{
System.out.println(“Hello”);
i++;
}
Output- Hello
Hello
Hello
Hello
Hello
→ as long as i<=5 it print Hello.
Here we are incrementing i so we are changing the value of i so i is going to start at 1 and it will stop at 5.
Types of Loops:
There are three types of loop in java
1.while loop
2.Do while loop
3.for loop
While loop– while loop is a pre-test loop, it is used when we don't know the number of iterations In advance.
It is also known as entry cantrol loop.
Syntax:
while(boolean -expression)
Statement
while(boolean -expression) {
statement(s)
}
→The statement(s) will keep executing as long as the boolean-expression evaluates to true.
→Each execution is called an iteration.
Flow chart-
Example: Write a program to find the summation of numbers from 1 to 10.
class whileLoop {
public static void main(String args[])
{
int num = 1, sum = 0;
// executes until the condition returns true
while (num <= 10) {
// summing up x
sum = sum + num;
// Increment the value of num for next iteration
num++;
}
System.out.println("Summation: " + sum);
}
}
Output:
Summation : 55
By using while loop we can create infinity loops
Eg:
Int i = 1;
while(i<=5)
System.out.println(“hello” =+1);
while(true)
System.out.println(“hello” );
The condition is always true.
Do while loop:- Do while loop is a post test loop , it is used when we want to execute loop body at least once even condition is false.
→ it is also known as exit control loop.
Syntax:-
Do
{
statement(s)
}
while(boolean expression);
→The statement(s) will keep executing as long as boolean expression evaluates to true.
Flow Chart:
Example: Java program to find summation of +ve natural numbers.
class Main {
public static void main(String[] args) {
int sum = 0;
int number = 0;
// create an object of Scanner class
Scanner input = new Scanner(System.in);
// do...while loop continues
// until entered number is positive
do {
// add only positive numbers
sum += number;
System.out.println("Enter a number");
number = input.nextInt();
} while(number >= 0);
System.out.println("Sum = " + sum);
input.close();
}
}
Output:
Enter a number
25 Enter a number
9
Enter a number
5
Enter a number
-3
Sum = 39
You can run this java program on Interviewbit.
Here, the user enters a positive number, that number is added to the sum variable. And this process continues until the number is negative. When the number is negative. the loop terminates and displays the sum without adding the negative number.
gvh
Small difference between while loop and do while loop
While loop check the condition then execute but do while loop execute then check the condition.
For loop:- For loop is the most commonly used loop it is used when we want to perform initialization, condition and increment decrement operation in single line
Syntax:-
for( variable; condition; change)
Statement
for( variable; condition; change){
statement(s)
}
→The statement(s) will keep executing as long as the statement evaluates to true.
Flow Chart:-
Example:
public class SumOfNatural {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 100; i++) {
sum = sum + i;
}
System.out.println("Sum of first 100 numbers is : " + sum);
}
}
Output: 5050
In the above code loops from 1 to given number num 100 and adds all numbers to the variable sum.
There is one more loop in for loop that is nested loop
→If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.
Example: wap to create half triangle using for loop in java.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output
*
* *
* * *
* * * *
* * * * *
I hope my article will be helpful for you. Thank you.
Java Script can be difficult, but it is definitely worth the effort. I faced difficulties, but through constant practice and a deep understanding of cycles, I gradually mastered this skill. I also found a java assignment for myself, used https://essays.edubirdie.com/java-assignment for this. When learning this language, the main thing is not to give up. Everything will work out, you just need to have patience.