Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Loops

In computer programming, loops are used to repeat a block of code.

For example, let’s say we want to show a message 100 times. Then instead of writing the print statement 100 times, we can use a loop.

That was just a simple example; we can achieve much more efficiency and sophistication in our programs by making effective use of loops.

Here we will see 3 types of loops:

  • for loop
  • while loop
  • do...while loop

for loop

The syntax of for-loop is:

for (initialization; condition; update) {
// body of-loop
}

Here,

  • initialization - initializes variables and is executed only once
  • condition - if true, the body of for loop is executed, if false, the for loop is terminated
  • update - updates the value of initialized variables and again checks the condition

Flowchart of for Loop

Example:

for(var i = 1; i <= 5; i++){
console.log(i + " ");
}
// Prints 1 2 3 4 5

While loop

The syntax of while loop is:

while (condition) {
statememt(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.

Flowchart of while Loop

Example:

var i = 1;

while(i <= 5){
console.log(i + " ");
i++;
}
// Prints 1 2 3 4 5

do…while loop

The do…while loop is nearly identical to the while loop, but instead of checking the conditional statement before the loop starts, the do…while loop checks the conditional statement after the first run, then continuing onto another iteration.

The syntax of do...while loop is:

do {
//body
} while (condition);

Flowchart of do…while loop

Example:

var i = 1;
do {
console.log(i + " ");
i++;
} while(i <= 5); // the contition is being checked after the first run

When is do-while used?

A do-while loop is used where your loop should execute at least one time.

For example, let’s consider a scenario where we want to take an integer input from the user until the user has entered a positive number.
In this case, we will use a do-while as we have to run loop at-least once because we want input from user at-least once. This loop will continue running until the user enters a positive number.

Try the following example in the editor below.

You are given an integer N, print all the odd values, for all i, where 0 <= i < N. Print each values on a seperate line.

Start solving Loops on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test