While Loop in C or C++

While Loop in C or C++

While Loop in C or C++

Loops are used to repeat some steps until the desired condition is satisfied. Loops basically execute certain piece of code which programmer does not want to write again and again. For e.g. if I want print numbers from 1 to 100, I have to declare 100 variables assign values to them and print them. It will definitely make my program large and will consume a lot of memory. Thus to avoid this problems looping structure provides easiest way by re-executing everything written inside it. While loop in C or C++ is also one kind of looping structure. It comes under branching looping structure. It’s general syntax is

Syntax -
while(condition)
{
  task1;
  task2;
  task3;
}

Let’s see the code which will use while loop to calculate sum of n numbers.

1. Code

C program

#include<stdio.h>

int main()
{
    int num, sum = 0,i;
    printf("\nEnter a no:");
    scanf("%d",&num);
    i = num;

    while(num > 0)
    {
        sum = sum + num;
        num--;
    }

    printf("\nSum of %d is :%d",i,sum);

    return 0;
}

C++ program

#include<iostream>

using namespace std;

int main()
{
    int num, sum = 0,i;
    cout<<"\nEnter a no :";
    cin>>num;
    i = num;

    while(num > 0)
    {
        sum = sum + num;
        num--;
    }

    cout<<"\nSum of "<<i<<" is : "<<sum;

    return 0;
}

2. Output

While Loop in C or C++

Also Read : How is C++ different from C.

3. Program Parts

  1. #include is a pre-processor directive. It is used to include header files.
  2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
  3. .h is an extension for header file.
  4. int is data type. It indicates that the program returns a value.
  5. main() is main function that indicates the compiler that the user written programs starts from here.
  6. i are variable names.
  7. while is looping structure in C/C++. It repeats the code inside it until a specific condition is satisfied.
  8. print/cout is the keyword use to print a message.
  9. Scanf/cin is the keyword use to store values in variables.
  10. return statement is use to return a value. If any error occurs the program will return 0.

4. Explanation

As usual the compiler will check if the required header files are present or not. If not then it will throw an error else it will go to next step. In the next step it will enter the main function. Here it will first declare necessary variables. Then it will accept a number which will tell the compiler how many numbers we want to add. The num value is stored in i to use later.

Now while loop is encountered. First the condition is checked and then only code inside it is executed. If value of num is greater then 0 then while loop is executed. Inside while loop num is added with sum and sum is stored in sum variable only. Then num– is encountered which decrements the value of num by 1. Once num becomes 0 while loop is completed and compiler comes out of while loop. At last the sum of n numbers is printed. And that’s it we have successfully calculated sum of n numbers.

Conclusion

So that’s the use of while loop in C or C++. Tell us if you liked this content. Also do share it with your friends and programming budies. Want other examples of while loop? Then tell us in the comment sections. Also tell us what other programs you want us to post.

For daily updates follow us on instagram. Do visit again!

ErroFreeProgram.

2 COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *