- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java while loop with Examples
A loop is a Java programming feature to run a particular part of a code in a repeat manner only if the given condition is true.
In Java, while loop is an iteration control flow model where the condition runs repeatedly until the encoded Boolean condition became true. It is a repeating if condition which can be used to execute some block of a statements. When the Boolean number iteration is not fixed in a process then it is necessary to use the while loop in a code.
How A While Loop Works In A Java Environment?
In a Java environment, the while loop check a given condition first. If the process return TRUE the code will run forward. Else the particular condition provide or return a FALSE value the whole process will terminate at that point. And the while loop might not execute at all.
Here is the process how a while loop execute a particular task –
Evaluate a textExpression in the parenthesis() method.
If, textExpression give an evaluation as TRUE, the while loop will execute.
Again scan the textExpression.
The whole method will run until the textExpression became FALSE.
When we get a FALSE evaluation. The whole loop will terminate.
The loop term become a circular loop in motion by using a flowchart model algorithm.
There are some attributes of a working loop.
Flag initialization.
Expression of a text.
One or multiple actions.
Updating a flag.
Here is the work flow model of a while loop in Java −
The Test Expression − The text is an expression by which we have to test the condition whether it fulfils the logic or not. If the condition evaluates a true condition then we will execute the whole source code with the loop and go to update the expression. Otherwise, the process will take an exit from the while loop. Example: i <= 10
To update an Expression − After the executing of a loop, this particular expression perform the iteration process with the increments/decrements of the loop variable by some value present in the data. Example: i++;
Here is the process how a while loop executes itself in a process −
Control falls under the encoded while loop.
The flow jumps to the mentioned Condition.
Condition is tested whether it is true or false.
If Condition is true, then the flow goes into the Body of the code again.
If Condition yields false, the flow goes outside of the loop.
The statements mentioned here inside the body of the loop get executed.
Updating takes place after the process execution.
Control flows back to the Step 2 again.
The while loop has ended here.
The flow has gone outside for the next give condition.
Algorithm
Step 1 − Start.
Step 2 − Build an instance scanner class.
Step 3 − Number declaration.
Step 4 − Ask to initialize that particular number.
Step 5 − To print a multiplication table use while loop.
Step 6 − Display result.
Step 7 − Terminate the process.
Syntax for the While Loop
while (condition){ Increment / decrement statement } Or; while (test_expression){ // statements update_expression; }
In the while loop the expression is tested. We will find a multiplication table of that particular number. To replace that multiplication table the user can apply while loop in the code.
Approach
Approach 1 − General illustrations of while loop.
Example 1: Print a Sentence Certain Number Of Times
Here in this Java built code, we can use while loop to print an output for multiple number of times. Here time complexity is O(1) and auxiliary space is same as before
public class whileLoopDemo { public static void main(String args[]){ int i = 1; while (i < 10) { System.out.println("Hello Coder"); i++; } } }
Output
Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder
Example 2: Summation of Some Numbers
Here in this Java built code we have used while loop to print an output for multiple numbers summation. Here time complexity is O(1) and auxiliary space remains same.
public class whileLoopexampletp { public static void main(String args[]){ int x = 1, sum = 0; while (x <= 20) { sum = sum + x; x++; } System.out.println("Summation of the numbers: " + sum); } }
Output
Summation of the numbers: 210
Example 3: Print an Infinite Loop
Here in this Java build code we have used while loop to run an infinite while loop. Here time complexity is O(1) and auxiliary space remains the same.
public class Whiledotp { public static void main(String[] args) { while(true){ System.out.println("infinitive while loop"); } } }
Output
infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop continue…………
Example 4: Multiplication Table of a Number
In this code, we have learned how to get a multiplication table for a particular number. When we enter a particular number as an input and the code build up by a while loop. It prints the whole multiplication table as an output.
import java.util.*; public class Main{ public static void main(String []args){ Scanner sc=new Scanner(System.in); System.out.println("Enter a number: "); int n=sc.nextInt(); int i=1; System.out.println("The multiplication table of the number"+n+" is: "); while(i<=10){ System.out.println(n+" * "+i+" = "+ (n*i)); i++; } } }
Output
Enter a number: 8 The multiplication table of the number8 is: 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 80
Conclusion
While loop is an iteration control and condition checking process in Java environment. In this article, we have learned a lot of while loop. We have learned how to use the conditions and benefits of their unique behaviour. And which while loop how we can use. Today we built some code using Java environment.
ultiplication Table of a Numbe