![]() ![]() Then, the last digit is removed from originalNumber after division by 10. Then, remainder is powered by 3 (number of digits) using Math.pow () function and added to result. On each iteration, the last digit of num is stored in remainder. It repeats a statement or block of statements as long as the value of its control expression is true. Then, a while loop is used to loop through originalNumber until it is equal to 0. The while loop statement is found in almost all programming languages. This design arranges multiple actions in a concise and understandable structure. In the following example we are using the while loop to print integer value from 1 to 10.īut we will jump out of the while loop if the value is a multiple of 7. Java while loop will work well in our case. When the value of counter variable becomes 11 then the while loop condition fails and so, we come out of the loop.Įxample #2: Write a program in Java to print 1 to 10 using while loop but quit if multiple of 7 is encountered In the java while loop condition, we are checking if i value is greater than or equal to 0. We are also incrementing the value of counter by 1 using the ++ increment operator. The loop's Boolean-expression is evaluated before the first iteration of the loop which means that if the condition is evaluated to false, the loop might not run even once. ![]() ![]() It repeats a statement or a block of statements while its controlling Boolean-expression is true. Inside the body of the loop we are printing "Hello World" using the println() method. The while loop is Java's most fundamental loop statement. This means we will execute the code inside the body of the while loop as long as counter is less than or equal to 10. So, we start with counter = 1 and the condition for the while loop is counter <= 10. We will execute the body of the while loop till counter <= 10. To solve this problem we will use a counter variable and initialise it with integer value 1. Or, we use the break statement to jump out of the loop.Įxample #1: Write a program in Java to print "Hello World" ten times using while loop While loop is used to execute some statements repeatedly until the condition returns false. Syntax A while keyword is followed by round brackets, () Within these brackets, the condition is written This condition is checked each time a loop is run. Try it Syntax while (condition) statement condition An expression evaluated before each pass through the loop. The condition is evaluated before executing the statement. To come out of the loop we make the condition false by updating its value in the body of the loop. The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. A while loop in Java is a control flow statement that executes code repeatedly based on a given Boolean expression. Where, condition is some condition and if it is satisfied then we execute the body of the while loop otherwise, we ignore it. If we use the above approach then it's going to take a while to code and it will be an inefficient way of solving the problem. But now lets say the requirement changed and we are now required to display "Hello World" 1,000,000 times! The statement may be a single statement or a block of statements, and the condition. To solve this we can print "Hello World" five times like the following. The Java while loop has two components, a condition and a statements. So, imagine we are given a task to write a program in Java to print "Hello World" 5 times. In this tutorial we will learn about while loop in Java programming language.Ī loop is a block of code getting executed over and over again as long as the given condition is satisfied. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |