While Loop: Definition, Example & Results

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: For Loop: Definition, Example & Results

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:04 What Is a Loop?
  • 0:33 While Loops
  • 2:51 Infinite Loops
  • 3:16 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: Sebastian Garces

Sebastian has taught programming and computational thinking for University students and has Master's degree in Computer and Information Technology

Are you tired of repeating instructions in your code? Explore one of the easiest loop structures that will allow you to make multiple instructions and repeat in just milliseconds. In this lesson, we will discuss the while loop and practice a few examples.

What Is a Loop?

Sometimes you might find yourself in a situation where you want to repeat instructions in your code like counting multiple things or accessing some information a certain number of times. Let's say we are required to print the numbers from 1 to 10 in our code, like the code below:

#include <stdio.h>
int main () {
 printf("1");
 printf("2");
 printf("3");
 printf("4");
 printf("5");
 printf("6");
 printf("7");
 printf("8");
 printf("9");
 printf("10");
 return 0;
}

What if we were required to print the numbers from 1 to 1000? Would you do it like this?

This is when loops become handy. A loop is an instruction that will let you iterate code as much as you want based on a specific condition.

While Loop

A while loop is one of the most common types of loop. The main characteristic of a while loop is that it will repeat a set of instructions based on a condition. As far as the loop returns a boolean value of TRUE, the code inside it will keep repeating. We use this kind of loop when we don't know the exact number of times a code needs to be executed.


While Loop Diagram
While Loop Diagram


Keep in mind that you need a way to set the condition to false to stop the 'while' loop, otherwise, the loop will execute the code infinitely. Having this in mind, if you're required to print the numbers from 1 to 10, this is what it would look like:

#include <stdio.h>
main () {
 int number = 1;
 while (number <= 10) {
  printf("%d\n", number);
  number++;
 }
 return 0;
}

In this case, printing on every single line, the numbers from 1 to 10:


1
2
3
4
5
6
7
8
9
10


As you can see, the condition inside the while (as in, a number less than or equal to 10) will be executed 10 times before it becomes false. Once that happens, the code inside the while block won't be executed anymore and the rest of the code will keep running until the end of the file. In this case, it was easy to identify when the loop will stop. Now let's see another example where you're required to know how many times a number is divisible by 2.

#include <stdio.h>
int main () {
 int counter = 0;
 number = 100;
 while (number > 1) {
  number = number / 2;
  counter++;
 }
  return 0;
}

Consider in this example that we're only using integer numbers, eliminating the decimals on the operations. In this case, the example will repeat the operation 6 times, printing the number of times that 100 can be divided by 2.

In this case, the loop repetitions completely depend on the number that we are trying to divide, making essential the use of the while loop. Let's look at some examples of these.

First, let's take a closer look at decrementing. Most loops start at a number and increase from there. But you can also decrement in a while loop. The following example starts at 100, performs some steps, and decrements by 1 every time through the loop.

The following code starts at 100 and steps back to 0. Notice how the condition is while cardNumber > 0.

int cardNumber = 100;
while(cardNumber > 0) {
 //some cool code here
 cardNumber--;
}

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