Loop without Loop!

repetition

Can you do an iteration without using any kind of loop (for, while, do-while) comes in programming? Well, this I found this cool code from programr. There were a lot more nice projects submitted by the users.

OK here’s it. Code is from C++ language.

Steps to run it on Visual studio (helpful for amateur programmers)

  1. Open new project.
  2. Select Visual C++ as the language and select win 32 console application.
  3. Name the project and then — OK.
  4. Click next on Win32 Application Wizard.
  5. Enable the check box Empty project and click Finish button. (here comes your project)
  6. Right click on Source file on Solution explorer and Add new item.
  7. Choose C++ file type, name it and add it.
  8. Paste the below code and run it. (build it first if it ask it so)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
 
int l=0;
 
int i=1;
void f1();
void f2();
 
int main()
{
cout<< "Welcome to the loop_without_a_loop!"<<endl ;
cout<<"the program will print 1- number you say without any type of loop available"<<endl;
cout<<"enter the length of loop"<<endl;
cin>>l;
 f1();
 
return 0;
}
void f1()
{
if (i<=l)
{
 
cout<<i<<endl;
f2();
}
}
void f2()
{
i=i+1;
f1();
}
#include <iostream>
using namespace std;

int l=0;

int i=1;
void f1();
void f2();

int main()
{
cout<< "Welcome to the loop_without_a_loop!"<<endl ;
cout<<"the program will print 1- number you say without any type of loop available"<<endl;
cout<<"enter the length of loop"<<endl;
cin>>l;
 f1();

return 0;
}
void f1()
{
if (i<=l)
{

cout<<i<<endl;
f2();
}
}
void f2()
{
i=i+1;
f1();
}

Program will ask you to enter the limit of the loop, and it will print the series of numbers until the limit.

It has a simple logic! Two functions (f1, f2) calling each other until the limit (condition) satisfied. Slick eh? 🙂

Leave a Reply