While loop statements - Repetition control structure - Java programming tutorial for beginners
Coding River
While loop statements - Repetition control structure - Java programming tutorial for beginners
Hi and welcome to this channel, In today’s video, we will start our discussion on Repetition control structures. Sometimes when writing a program you would want to repeat a set of statements a bunch of times. One way you can do that is by typing the statements in the program over and over again.
This way of doing or this approach is ineffective or even impossible especially when you have huge amounts of statements to type.
That’s exactly the reason why we need to learn about repetition control structures. Because they provide a simpler approach when we want to repeat a set of statements in our code.
Repetition structures are also called Looping structures and in Java, there are three types of looping structures which are the while loop, the for loop, and the do … while loop.
In this video, we will start discussing the while loop structure
Here is the syntax of a while loop statement int score = 0; while (score less than or equal to 20) System.out.println(i + “ “);
while, here is a reserved word Following, in the brackets, is the logical expression also called a loop condition or simply a condition Next, we have the action statements which is also called the body of the loop The action statements are executed only when the loop condition is met The action statement can be either a simple statement meaning that you will have only one statement Or a compound statement when you have more than one statement In case of a compound statement, you always need to surround your action statements with curly braces like this
int score = 0 while (score less than or equal to 20) System.out.println(i + “ “); score = score + 5;
Here is what will happen when we run this portion of code
First of all, note that we have declared and initialized an integer variable score. This variable is called the Loop control variable. Next, the logical expression in the while loop statement is then evaluated. Because the condition score less than or equal to 20 evaluates to true, then the action statements in the body of the loop will execute The first action statement will output the value of score While the second action statement will increment the value of the variable score by 5
After, the logical expression in the while loop is evaluated again Because the value of the variable score is now 5 the logical expression score less than or equal to 20 will still evaluate to true and the body of the while loop will execute By outputting the values of the variable score And incrementing the value of the variable score by 5 again
This process of evaluating the logical expression and executing the body of the while loop will continue unt ... https://www.youtube.com/watch?v=RMg1dWefngo
11960917 Bytes