C Programming for Beginners

Loops and While Loop

Loops And While Loops

Loops and While Loop

In the last chapter we have gone through C language conditional statements

In this chapter, we will learn about the Loop. Many programming languages support 3 types of loops.

Types of Loops

  1. WHILE Loop,
  2. DO WHILE Loop and
  3. FOR Loop.

In this chapter, we will focus more on the WHILE loop.

So if you want to write a simple program in C, like printing a variable; It’s pretty simple. We have done this in the last chapters many times, What you will do is You will create a main statement You will initialize the variable, Let’s say it’s i is equal to 1. You will use printf statement to display the value So this is the program. Its output will be i equal to 1

main()
{

const int i = 1;
printf(“%d”,i);

}

Output :

1

Now suppose you want to print 1 to 9. What will you do? Will you write the printf statement 9 times? NO! it’s too much work.What you will do is you will write a printf statement one time and you will execute it 9 times 

Let’s open our source code again. What we will do is, We will put up the printf statement inside while.

main()
{
int i = 1;
while (i < 10)
{
printf(“%d “, i);
i++;
}
}

If you want to copy paste the code please refer our Newtum Github repository link over here.

Output :

1 2 3 4 5 6 7 8 9

In the while statement, we will write a condition that is i less than 10, Remember we have used this condition in IF and ELSE statements.

Here we are telling our program to run print value statement till 9. But the value of i is only 1 

Here we have initialized the variable i equal to 1 This is called “initialization”

Next, we have given the condition and after that, we have given the increment Now, the only thing is printf statement

Instead of printf, we can write any logic that we want So What we understand is for Loop execution.

Three things are very important

  1.   First is the initialization
  2.   Second is Condition
  3.   Third is Increment

Just try to understand the program. First Computer reaches the part of initialization that is i equal to 1.  Next, it reaches the while condition. In WHILE, It checks whether i is less than 10 but currently the value of i is 1, So 1 is definitely less than 10. The result is True

So it goes to the printf statement and it prints the value of i, that is 1. In the next statement it says, i equals to i plus 1

So the value of i becomes 2. Now what happens is, It goes to the end of the bracket and again it jumps to the condition

In the condition it checks whether i is less than 10 or not But the value of i is 2 So Again the condition is True Since the condition is True, The system enters the Loop again and it prints the value of i that is 2. Now it enters the increment code, In increment, value of i becomes 3. Now it again enters the condition and in the condition, system checks that whether i is less than 10 or not.

But again the condition is true, because 3 is less than 10. So What happens is, it again enters the loop. Now, suppose we have done the printing till 9.

After printing the value of 9, System goes to increment, and the value of i becomes 10.Now it again checks the while condition .Condition is, i less than 10 or Not.But 10 is definitely not less than 10, So the condition Breaks And system skips the while Loop and goes to the next line.

Here the while loop ends. The system reaches the end of the program.

Infinite loop

Be aware of loops, one small mistake and your loop is lost in infinity. We term it as infinite loops. You miss anything then your program will run indefinitely. 

Let’s open our program again. Now remove the increment. Currently the value of i is one. It never changes to 2 and will never reach to 10. Let’s see what happens.

Run the program now. and it’s continuously printing 1. It won’t stop. this is called as an infinite loop. Sometimes we write infinite loops  for certain rare cases, but we will study about them a bit later.

This was the very basic example of while loop. Suppose we want to print all even numbers till 20.

Now we know 2,4,6,8,10,12 are all the even numbers. The common factor is, the difference between i.e. 2. This common factors helps to decide the increment or decrement in the loop.

Increment in the loop

Let’s write our program first. let’s declare a variable i.  Our series of even numbers starts with 2. So we will assign value 2 to i, Write down WHILE loop. Inside the WHILE loop,

we will write a printf statement to print the value of i. Now is the important part, We will increment the value of i by 2. We will write i=i+2,

main()
{

int i = 2;
while (i < 10)
{
printf(“%d “, i);
i=i+2;
}

}

If you want to copy paste the code please refer our Newtum Github repository link over here.

Output :

2 4 6 8

Here, the trick is increment. We have set the initial value to 2. Now when the increment is done it becomes 4, In the next iteration it becomes 6. 

In the first example, we have given 1 as the increment and now we have given 2 as the increment. We can give any type of increment depending upon the requirement.

Decrement loop

Now suppose we get the requirement that we need to print 10 to 1 i.e. in reverse order. Such loops are called as decrement loops. In the decrement loop we decrease the value.

Let’s write an example. We declared a variable i. We set its value to 10.  In decrement loop, we are starting from the top. Now write WHILE statement. here I am writing i > 0. Inside WHILE we will write printf. Now here we will give the decrement i=i-1.

Let’s execute the program.

main()
{
int i = 10;
while (i > 0)
{
printf(“%d “, i);
i=i-1;
}
}

If you want to copy paste the code please refer our Newtum Github repository link over here.

Now you see we got the output 10 to 1.

Output :

10 9 8 7 6 5 4 3 2 1

As you know there are 3 important parts of any loop,

  1. 1st is initialization,
  2. second is condition and
  3. third is the increment.

When you change any of the parameter’s behaviors, the loop changes.

When we created the decrement loop, we changed the initialization, because as per the requirement we wanted to print 10 to 1, so initialization should start from 10.

Look at the condition now, Earlier we have written conditions like counter i < 10, so that when i reaches to value 10, loop breaks. But in this case, our loop starts from 10 and goes on reducing. So we write the condition, i > 0. Here, i starts with 10, then it will become 9, 8, 7 and finally 0. As soon as it reaches 0, the loop breaks.

Next is decrement operators, here we have written i = i – 1, it’s because we want to decrement the value of i every time it is printed. So the initial value of i was 10, now 10-1 will become 9 and so on. Until it reaches 0.

So this was about decrement loop now we will study about increment and decrement Operator.

Increment and decrement Operator Loop

Open our very first program of WHILE loop. We have printed 1 to 10. We will do a small change in the increment line over here. Here we have written i  = i + 1 , but now we will write i++.

i++ is similar to i = i + 1 , it will increment the value of i by 1. i++  is called increment operator. Let’s run the program. See, we got the correct output.

Now open the decrement program again, here we have written i = i – 1. In the decrement part we can replace this with i–. it’s called decrement operator. Hope you got this.

main()
{
int i = 10;
while (i > 0)
{
printf(“%d “, i);
i–;
}
}

Output :

10 9 8 7 6 5 4 3 2 1

Similar to i++ and i–, we have ++i and –i, they make a difference, we will study about the difference in later chapters.

We need to study one more operator i.e i +=1 or i +=2. It’s another simple way of writing i = i +1 or i = i + 2 respectively.

Let’s open the program where we have printed all the even numbers about 20.

main()
{

int i = 2;
while (i <= 20)
{
printf(“%d “, i);
i=i+2;
}

}

Just run the program.

If you want to copy paste the code please refer our Newtum Github repository link over here.

Output :

2 4 6 8 10 12 14 16 18 20

Now, look at the increment part. Here we have written i = i + 2. Instead of i = i + 2 , we can write i +=2. Run the program. We got our output.

We can use 2, or 5 or 10 to increment using this operator. All this depends on the program we want to write.

So in this chapter, we had to go through WHILE loop. To summarize we can say that while loop is used to do the same type of task again and again.

Initialization, condition and increment are the must-have things for while loop. Miss anything and you may end up in an infinite loop.  

The simple way of increment is i = i + 1 but we can use i++ as an increment operator and i– as a decrement operator.

We can also use i +=2 operator to give custom increment as per the requirement.

Well, this is about WHILE loop. We will study more about loops in the next chapter.

 It’s quite a while that we are learning C programming together. So stay in the loop and keep learning.

 

Facebook Comments
(Visited 1 times, 1 visits today)