Copyright

Nested While Loops in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Infinite While Loops in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:03 What Is a Nested While Loop?
  • 0:26 Nested While Loop…
  • 1:13 Example
  • 2:16 Use of Break in Nested…
  • 2:49 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Sudha Aravindan

Sudha has a Doctor of Education Degree and is currently working as a Information Technology Specialist.

How would you print out a chart to record scores for players in a basketball game? In Java, you can do this using a nested while loop. In this lesson, we'll explore how to use nested while loops in Java.

What Is a Nested While Loop?

A nested while loop is a while statement inside another while statement. In a nested while loop, one iteration of the outer loop is first executed, after which the inner loop is executed. The execution of the inner loop continues till the condition described in the inner loop is satisfied. Once the condition of the inner loop is satisfied, the program moves to the next iteration of the outer loop.

Basic Structure of Nested While Loop

Consider the code snippet of a nested while loop:


class NestedWhileLoop {
 public static void main(String args[]) {
  int outerLoop = 1;
  while(outerLoop < 4) {
   int innerLoop = 3;
   while(innerLoop < 6) {
    System.out.println(outerLoop = ":" + innerLoop);
    innter++;
   }
  outerLoop++;
  }
 }
}


The output from this program will be like this:


1 : 3

1 : 4

1 : 5

2 : 3

2 : 4

2 : 5

3 : 3

3 : 4

3 : 5


Notice how the outer loop starts with the first iteration for the starting number 1. Then the inner loop is executed in full till the inner loop number reaches 5 (less than 6). The process is then repeated for the next iteration of the outer loop till the condition of the outer loop (number less than 4) is met.

Here the outer loop is the code:


int outerLoop = i;
while(outerLoop < 4) {
 outerLoop++;
}


The inner loop starts just after the while statement of the outer loop:


int innerLoop = 3;
while(innerLoop < 6) {
 System.out.println(outerLoop + ":" + innerLoop);
 inner++;
}


Following the first iteration of the outer loop, the inner loop is executed in full. Only after the complete execution of all the iterations of the inner loop is the next iteration of the outer loop executed.

Example

Here's an example question we can work with:

  • How can we use a nested while loop to print out a chart to record the player number, number of tries, and if the player scored a hoop or not?

A coach is putting together a basketball team. There are 10 players for the try outs, the players are numbered 1 through 10. Each player gets 6 chances at the hoop. Use a nested while loop in Java to print out a chart to record the number of the player and the number of tries each player had. The chart will be used on the field to fill in the scores as the player scores a hoop or not.


import java.io.*;
class BasketballTryouts {
 public static void main(String[] args) {
  int playerNumber = 1;
  while(playerNumber < 11) {
   int numberOfTries = 1;
   while(numberOfTries) < 7) {
    System.out.println("Player Number " + playerNumber + ": Trial Number " + numberOfTries + ": Score (Y/N): ______");
    numberOfTries++;
   }
playerNumber++;
  }
 }
}


The output for this program could be something like:


Results


and so on.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support