Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

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(int i = 1; i <= 5; i++){
cout<<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:

int i = 1;

while(i <= 5){
cout<<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:

int i = 1;
do {
cout << 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
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
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
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
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