Help me with this C++ figure of speech

Stratum9

Senior member
Apr 13, 2002
602
0
0
I'm not a programmer, but I understand it somewhat. I grabbed the folowing snipet of code from a C++ book to illustrate my simile and I need to know if it's accurate.

My thoughts run like badly written code stuck in an endless loop:

for(int i = 0; i >= 0; i++)
{
cout << i << endl;
}


If I understand this correctly, this will continuously output "0" because it creates an endless loop since "i" will always be greater than "0". Am I correct?

Anyone want to write a more impressive example of an endless loop?
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
no i will be incremented so it will print

0
1
2
3
4
5
etc.


a better infinte loop would be

while(1)
{
cout << "yu0 r teh n00b!" << endl;
}

 

Stratum9

Senior member
Apr 13, 2002
602
0
0
Originally posted by: Ameesh
no i will be incremented so it will print

0
1
2
3
4
5
etc.


a better infinte loop would be

while(1)
{
cout << "yu0 r teh n00b!" << endl;
}

So this outputs "yu0 r teh n00b!" over and over again? That's sort of what I'm looking for.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: Stratum9
Originally posted by: Ameesh
no i will be incremented so it will print

0
1
2
3
4
5
etc.


a better infinte loop would be

while(1)
{
cout << "yu0 r teh n00b!" << endl;
}

So this outputs "yu0 r teh n00b!" over and over again? That's sort of what I'm looking for.

yeah, each on its own line.

 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
how about this?

#include <stdio.h>
int main(int a,int b,int c)
{
putc(main(a++,b,c));
}
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
This isn't C++, but it is valid code that executes an endless loop, and I think it's nicer looking than anything anyone else has posted:

wait while time

Yes, that one line is it. Guess what language it is ;)
 

Krakerjak

Senior member
Jul 23, 2001
767
0
0
Originally posted by: notfred
This isn't C++, but it is valid code that executes an endless loop, and I think it's nicer looking than anything anyone else has posted:

wait while time

Yes, that one line is it. Guess what language it is ;)

english
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: notfred
This isn't C++, but it is valid code that executes an endless loop, and I think it's nicer looking than anything anyone else has posted:

wait while time

Yes, that one line is it. Guess what language it is ;)

how is that better then:

while(1);
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: notfred
This isn't C++, but it is valid code that executes an endless loop, and I think it's nicer looking than anything anyone else has posted:

wait while time

Yes, that one line is it. Guess what language it is ;)

Perl (?)
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: Ameesh
Originally posted by: notfred
This isn't C++, but it is valid code that executes an endless loop, and I think it's nicer looking than anything anyone else has posted:

wait while time

Yes, that one line is it. Guess what language it is ;)

how is that better then:

while(1);

I actually think that the following is better:

while (true);

Because the fact that any integer > 0 decomposes into a positive boolean in the context of a conditional is idiomatic to the C language, and it's not evident to the non-programmer; not to mention, C++ frowns on it, and most modern languages wouldn't allow such a thing (Java, C#).
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
Originally posted by: glugglug
how about this?

#include <stdio.h>
int main(int a,int b,int c)
{
putc(main(a++,b,c));
}

Would that even compile? I didn't think you could call main explicitly in C/C++.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Originally posted by: mugsywwiii
Originally posted by: glugglug
how about this?

#include <stdio.h>
int main(int a,int b,int c)
{
putc(main(a++,b,c));
}

Would that even compile? I didn't think you could call main explicitly in C/C++.

try it :)

Actually it will compile, but now that I think about it, it wouldn't work as intended (outputing entire characterset over and over). The inward recursion would happen before ever returning to give a value to putc and the stack will overflow.

This version will have some output before overflowing:
#include <stdio.h>
int main(int a,int b,int c)
{
putc(a);
return main(++a,b,c);
}


personally my favorite obfuscated C is xmas.c (google it).
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: Descartes
Originally posted by: Ameesh
Originally posted by: notfred
This isn't C++, but it is valid code that executes an endless loop, and I think it's nicer looking than anything anyone else has posted:

wait while time

Yes, that one line is it. Guess what language it is ;)

how is that better then:

while(1);

I actually think that the following is better:

while (true);

Because the fact that any integer > 0 decomposes into a positive boolean in the context of a conditional is idiomatic to the C language, and it's not evident to the non-programmer; not to mention, C++ frowns on it, and most modern languages wouldn't allow such a thing (Java, C#).


nitpicking nancy forogot this thread is about c++
 

hans007

Lifer
Feb 1, 2000
20,212
18
81
no it will output all integers till infinity starting at 0.



basically the statement says



start i at 0. if i is greater than or equal to 0 increment i by 1. and for every loop print i out.


i suppose , it really isnt infinite, since on a 32 bit machine, this would stop at 2 billion something since it would overflow and presumeably trigger an overflow flag, or wrap around to negative 2 billion. since 32bits can represent 4 billion and change, and signed, that is positive 2 billion (which is why we have a 4 gb ram limit on 32bit machines)