#18 While vs For Loop | Which to use and When?

Telusko
3 Jun 202106:58

Summary

TLDRIn this JavaScript tutorial, Ivan and Wendy explore the best loop for different scenarios, emphasizing that no single loop is universally superior. They demonstrate the 'for' loop for known start and end points, such as printing numbers divisible by three up to 100. They then illustrate the 'while' loop's utility in situations without a clear end, like printing digits of a number in reverse order. The video concludes with an assignment to reverse a given number using loops, highlighting the practical application of these concepts.

Takeaways

  • 🔁 There is no 'best' loop in JavaScript; each loop has its own advantages and drawbacks and is suited for different scenarios.
  • 🔢 The 'for' loop is ideal when you know the starting and ending points, such as printing numbers from 1 to 100 or numbers divisible by 3.
  • 🔑 To print numbers divisible by 3 from 1 to 100, you can use a 'for' loop with a modulus operator to check divisibility.
  • 🚀 Encourages viewers to pause the video and try coding examples themselves to enhance learning.
  • 🔄 The 'while' loop is preferable when the number of iterations is not known in advance but the stopping condition is clear.
  • 📉 Demonstrates using a 'while' loop to print digits of a number in reverse order by repeatedly using modulus and division operations.
  • 💡 Shows how to handle floating-point numbers in JavaScript by converting them to integers for loop iterations.
  • 🔄 Explains the difference between 'while' and 'do-while' loops, with 'do-while' executing the block before checking the condition.
  • 🗣 Uses an analogy of a chat conversation to illustrate the difference between 'while' and 'do-while' loops.
  • 📝 Assigns a practical exercise to the viewers: to print the reverse of a given number using the concepts learned in the video.
  • 🎥 Ends with a prompt for viewers to comment, subscribe, and engage with the content.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is to discuss different types of loops in JavaScript and when to use each one for optimal results.

  • Why is there no 'best' loop according to the video?

    -According to the video, there is no 'best' loop because each loop has its own advantages and drawbacks, and they are suitable for different scenarios.

  • What is the recommended loop to use when you know the starting and ending points?

    -The 'for' loop is recommended when you know the starting and ending points, as it works perfectly for scenarios like iterating from a known start to a known end.

  • Can you give an example of when to use a 'for' loop?

    -An example of using a 'for' loop is when you want to print numbers from 1 to 100 or to print all numbers between 1 and 200 that are divisible by 3.

  • What is the condition checked in the 'for' loop to print numbers divisible by 3?

    -The condition checked in the 'for' loop to print numbers divisible by 3 is `i % 3 == 0`, which uses the modulus operator to determine divisibility.

  • What is the scenario where a 'while' loop is considered the best?

    -A 'while' loop is considered the best when you do not know the starting or ending point but have a clear condition for when to stop.

  • Can you provide an example of a situation where a 'while' loop is more appropriate than a 'for' loop?

    -An example is when you want to print each digit of a number individually in reverse order, and you don't know the number of digits in advance.

  • How does the 'while' loop in the video example work to print the digits of a number in reverse order?

    -The 'while' loop works by repeatedly taking the modulus of the number by 10 to get the last digit, then dividing the number by 10 to remove the last digit, and continuing this process until the number is greater than zero.

  • What is the difference between a 'while' loop and a 'do-while' loop?

    -The difference is that a 'while' loop checks the condition first before executing the block, whereas a 'do-while' loop executes the block first and then checks the condition.

  • What is the assignment given at the end of the video?

    -The assignment is to take a given number and print its reverse using the concepts learned from the video.

  • What is the significance of the 'do-while' loop example involving a chat?

    -The 'do-while' loop example involving a chat illustrates that the loop will execute at least once, even if the condition (the person being offline) would normally prevent it, similar to sending a message before checking if the person is online.

Outlines

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Mindmap

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Keywords

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Highlights

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Transcripts

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now
Rate This

5.0 / 5 (0 votes)

Related Tags
JavaScriptLoopsFor LoopWhile LoopDo-While LoopCoding TutorialProgrammingIvan WendyBest LoopCode Examples