In part 4 of my Java Video Tutorial I cover looping in Java. This tutorial uses a lot of what I previously taught in the first Java Video Tutorial, so watch the previous parts first.
Here I start out explaining while loops. I provide numerous examples including how to calculate PI with a while loop.
I then go on to explain how do while loops work. I finish the tutorial by covering the for loop.
The code follows the video and you should use it to follow along through the tutorial for better understanding.
If you like videos like this share it
Code From the Video
import java.util.Scanner; // Library that allows us to capture user input public class LessonFour { // Creates a Scanner object that monitors keyboard input static Scanner userInput = new Scanner(System.in); public static void main(String[] args) { /* * The while loop : A while loop executes the code between {} until the * condition is no longer true */ // while loops create a loop iterator before the loop begins int i = 1; while(i < 20) { // Use continue to skip over printing 3 if(i == 3) { i+=2; // When using continue, don't forget to change the iterator continue; // continue skips a loop iteration, but not the loop } System.out.println(i); i++; // Just print odd numbers if((i%2) == 0) { i++; } // i is greater than 10 jump out of the loop if(i > 10) { break; // Forces the program to leave the loop prematurely } } /* * Code that calculates the value for PI */ double myPi = 4.0; // My starting value for pi // Starting value for my loop iterator in the loop double j = 3.0; // The while loop while(j<11) { // I calculate pi with this formula // PI = 4 - 4/3 + 4/5 - 4/7... myPi = myPi - (4/j) + (4/(j+2)); j += 4; System.out.println(myPi); } System.out.println("Value of PI: " + Math.PI); /* * Execute while loop until the user quits it */ String contYorN = "Y"; int h = 1; // equalsIgnoreCase checks if the input string is equal to y or Y while (contYorN.equalsIgnoreCase("y")) { System.out.println(h); System.out.print("Continue y or n?"); contYorN = userInput.nextLine(); // Accepts a string input from the user h++; } /* * The do while loop : Used when you want to guarantee the code * between {} will execute at least once. The code is executed and * then java checks if it should execute again */ // loop iterator for the do while loop // It must be created before the expression is evaluated below int k = 1; do { System.out.println(k); k++; } while (k <= 10); // Counts from 1 to 10 /* * The for loop : Another looping tool in Java * for( declare iterator; conditional statement; change iterator value) */ for (int l=10; l >= 1; l--) { System.out.println(l); } // System.out.println(l); The variable l is not callable outside of for int m, n; // You don't have to declare variables in the for loop // You can use multiple variables in the for loop for (m=1, n=2; m <= 9; m+=2, n+=2) { System.out.println(m + "\n" + n); } // You can have for loops inside of for loops, but I'll save // that topic until I talk about arrays } }
nice video again as usual…keep going…
Thank you 🙂
as you explain continue keyword in while loop, like
if(i == 3)
{
i+=2;
continue;
}
it’s working fine.
but my question is, if i do the statement like this:
if(i == 3)
{
System.out.println(“Hello world”);
continue;
}
it prints “hello world” infinite times.
The reason why you get an infinite loop is because you don’t change the value of your iterator. The value of i must cease to be equal to 3 to jump out of the loop. continue is mainly used to jump back to the beginning of loops. You don’t want to call continue until you change the value of the iterator. Does that make sense?
okay so it means whenever i m using continue, i have to change the value of iterator, means if i want to print the printf statement also
i have to do like this;
if(i==3){
i+=2;
System.out.println(“hello world”);
continue;}
Yes that code works. Always put continue after you increment the iterator
okay…
firstly, check this out
int l=0;
for(l=10;l<20;l++){
System.out.println(l)
}
System.out.println(“Value of l” + l)
Now, what is the value of l which is output in the bold line?
Also,
in case of multiple condition declaration, is the default thing to do OR or AND for the for loop?I hope you get what I want to say in the second question.
The value would be 19. You would use || in situations when just one needs to equal true and && when they both need to be true. Is that the question?
Hey!
Great tutorial site. You should create this course(and others) on http://www.udemy.com and make some revenue from it.I have purchased several courses on their website.
Cheers
Kev
Thank you 🙂 I really enjoy providing everything for free. I’ve never been very money motivated, and so the nice comments I receive are more than enough compensation
Hello,
First of all Thank you very much for this video and it is very helpful…
At 3:08 minutes of your video, i.e., i%2==0, you have mentioned if it equals to 0, then we are dealing with odd numbers. But Isn’t it even numbers that we are dealing with?
Sorry about the typo 🙁 I some times get slipped up because I improv many of these videos. Over the last year I have gone out of my way to eliminate mistakes like this.
There are actually brackets around i%2 .So thats not a problem .Derek is right about dealing with odd numbers and not even numbers.
There’s no typo Derek.
good examples for the loops. keep it up
Thank you 🙂
Hi derek, thanks for the tutorial!
I have a question about the last problem. for (m=1, n=2; m<=9; m+=2, n+=2) i guess you have to put comma to separate and semicolon to end and maybe that is how it works! O.k now the question, why did you declared only "m" should be <=9 and not "n" and how do you get 1 to 10 when you add +=2?
Hi, You’re very welcome 🙂 I only checked for m <= 9 because that was enough to stop the loop. There is no other reason. 1 through 10 print because m=1, n=2 at the beginning of the loop. One prints the odds and the other prints the evens. Sorry about the confusion
Thank you so much great tutorials!
You’re very welcome 🙂
nice……………….
Thank you 🙂