Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Understand Nested Loops

This video was last updated on May 3 2022

Learn how to nest one loop inside another, and even terminate a loop early if and when you need to.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

So far in this course, you’ve been working with loops that operate over lists; in other words, your loop always goes from start to finish with no interruption.

var daysOfTheWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
for (var day in daysOfTheWeek) {
    print(day);
}
for (var day in daysOfTheWeek) {
    if (day == 'Thursday') {

    }
    print(day);
}
for (var day in daysOfTheWeek) {
    if (day == 'Thursday') {
        break;
    }
    print(day);
}
  for (var day in daysOfTheWeek) {
    if (day == 'Thursday') {
        continue;
    }
    print(day);
  }
var movieOne = ['Peter Jackson', 'Ian Mckellen', 'Viggo Mortensen'];
var movieTwo = ['Jackie Chan', 'Alan Smithee', 'Sylvester Stallone'];
var movieThree = ['Chris Pratt', 'Kurt Russell', 'Sean Gunn'];
var movies = [movieOne, movieTwo, movieThree];
var totalCredits = 0;
var processedMovies = 0;
for (var movie in movies) {
    for (var credit in movie) {
        print(credit);
        totalCredits += 1;
    }
    processedMovies += 1;
}
print('---');
print('total processed movies: $processedMovies');
print('total processed credits: $totalCredits');
  var processedCredit = 0;
  for (var movie in movies) {
    processedCredit = 0;
    for (var credit in movie) {
      print(credit);
      processedCredit += 1;
    }
    totalCredits += processedCredit;
    processedMovies += 1;
  }
if (credit == 'Alan Smithee') {
    break;
}
outerloop:
for (var movie in movies) {
if (credit == 'Alan Smithee') {
    continue outerloop;
}

Reviews

Comments