C Program to demonstrate looping without loops

This program demonstrates how to write a loop without any of the loop constructs. Of course, many programmers would call it a bad programming practice. Nevertheless, it is a nice program that can help us develop skills by writing a program differently.

Last Reviewed and Updated on May 17, 2017
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

Advertisement

C Program to demonstrate looping without loops

Ask the user to enter an int number. If he enters, 5, your program should print five stars. If he enters 2, your program should print 2 stars. Do not use any loops.

This program is a real candidate for a for loop. A statement label has been used only for demonstration purposes.

int main()
{

    cout << "How many stars?: ";

    int i;

    cin >> i;

    int counter = 0;

lblX:
    cout << "* ";

    counter++;

    if (counter < i)
    {

        goto lblX;

    }

    return 0;

}


My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.



Creative Commons License
This Blog Post/Article "C Program to demonstrate looping without loops" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2017-05-17.


Advertisement