Never ending Program

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
imgettionowned




Wordewatician 500

Posts: 709
Joined: Sat Jun 25, 2005 8:53 am

Never ending Program

Post by imgettionowned »

I just started experimenting with loops for c++, so here's a never ending program based upon a for loop.

just compile and run it

Code: Select all

#include <iostream>
int main()
{
	using namespace std;
	cout << "Press Enter When You Want It To Begin: ";
	cin.get();
	int a,b,c;
	b=-1;
	c=100000;
	for (a=0;a = b; a++)
		cout << c++;
	
	cin.get();
	cin.get();
	return 0;
}
It's pretty amazing to watch how fast a pc can actually do math, even though it's as simple as adding one

Edit: I am not sure if it is neverending due to restrictions on how big an integer can get
live2board





Posts: 17
Joined: Sun Mar 27, 2005 10:08 am

Post by live2board »

If you go "unsigned long a, b, c" you can get alot bigger numbers.

Or to create a never-ending loop just test for a condition you know will never return as true. For example:

Code: Select all

int a = 1;
    while (a == 0)
    {
            cout << "Kevin 0nwz j00!!" << endl;
    }
"a" will never evaluate to be equal to zero, therefore it will repeat endlessly.
Onetoomanysodas





Posts: 325
Joined: Mon Mar 22, 2004 3:59 pm
Location: Everywhere but nowhere
Contact:

Post by Onetoomanysodas »

Actually it will do just the opposite.

Since 'a' will never be evaluated to equal zero, the loop statement will never be executed and the program will end.

What would work for an infinite loop is this conditional

Code: Select all

while (a != 0)
Doctor





Posts: 567
Joined: Sun Nov 27, 2005 5:48 pm

Post by Doctor »

I'm not much of C++ but I think this will work, won't it?

Code: Select all

while(1) 
{
     cout << "[statement] \n";
}
Last edited by Doctor on Sun Mar 19, 2006 4:47 pm, edited 3 times in total.
Image
Communism || Well in Halomods Com, I own you. -Shout
Userbar put your total signature image at 140 pixels in height. Sorry. <-- My sig has become the #1 place for Moderator tags -_-
You could always, oh I don't know, edit our comments out? =p | I would put it back in. -Shout -wer its at brudda-
User avatar
FleetAdmiralBacon




Critic Pyre Articulatist 500

Posts: 2377
Joined: Sat Nov 26, 2005 7:01 pm
Location: Ohio
Contact:

Post by FleetAdmiralBacon »

Code: Select all

void main()
{
     yo:
     cout << "Bacon owns joo\n";
     goto yo;
}
Image
Everything you'll ever need to know about me.
Need help with Linux, C#, C, Python, Java, PHP, Javascript, CSS, or VB? I'm available from 3:00PM-8:00PM EST on weekdays.
Onetoomanysodas





Posts: 325
Joined: Mon Mar 22, 2004 3:59 pm
Location: Everywhere but nowhere
Contact:

Post by Onetoomanysodas »

Doctor wrote:I'm not much of C++ but I think this will work, won't it?

Code: Select all

while(1) 
{
     cout << "[statement] \n";
}
Yes that is a common way of processing always true procedures. do() with no while after the ending brace will continue until the condition evaluates false.
FleetAdmiralBacon wrote:

Code: Select all

void main()
{
     yo:
     cout << "Bacon owns joo\n";
     goto yo;
}
-- That's nubish >_>
User avatar
FleetAdmiralBacon




Critic Pyre Articulatist 500

Posts: 2377
Joined: Sat Nov 26, 2005 7:01 pm
Location: Ohio
Contact:

Post by FleetAdmiralBacon »

mb:
it also works, and is the easiest way to make a never ending program.
goto mb;
Image
Everything you'll ever need to know about me.
Need help with Linux, C#, C, Python, Java, PHP, Javascript, CSS, or VB? I'm available from 3:00PM-8:00PM EST on weekdays.
live2board





Posts: 17
Joined: Sun Mar 27, 2005 10:08 am

Post by live2board »

Onetoomanysodas wrote:Actually it will do just the opposite.

Since 'a' will never be evaluated to equal zero, the loop statement will never be executed and the program will end.

What would work for an infinite loop is this conditional

Code: Select all

while (a != 0)
Lol oops. Definately shows how long I've been away from C++. And Goto's are just not cool at all.
User avatar
xbox7887




Socialist Coagulator Decryptor Advisor
Eureka Commentator Wave Scorched Earth

Posts: 2160
Joined: Mon Dec 27, 2004 6:19 pm
Location: New Lenox, Illinois
Contact:

Post by xbox7887 »

Code: Select all

mov	eax, 1		;loop indicator
xor	ebx, ebx 	;resets ebx

loop:
inc	ebx		;ebx+1
cmp	eax, 0		;checks to exit
jne	loop		;otherwise loops
This is just a dummied down version of what you're trying to accomplish in x86 assembly. The only thing wrong with it is there is no way to stop the loop. Stopping the process to quit an application isn't always a good idea ;P
OwnZ joO




Articulatist 500

Posts: 980
Joined: Thu Nov 10, 2005 4:24 pm

Post by OwnZ joO »

Not doubting you, but why is it not always good to end a process?
User avatar
xbox7887




Socialist Coagulator Decryptor Advisor
Eureka Commentator Wave Scorched Earth

Posts: 2160
Joined: Mon Dec 27, 2004 6:19 pm
Location: New Lenox, Illinois
Contact:

Post by xbox7887 »

Because good applications should be able to quit out themselves...
Onetoomanysodas





Posts: 325
Joined: Mon Mar 22, 2004 3:59 pm
Location: Everywhere but nowhere
Contact:

Post by Onetoomanysodas »

It's not always good to end a windows process for a large application if it is writing data or nested in a loop modifying the memory. This could just cause a program that saves files to lose or corrupt a file while it's buffering it. Sometimes API functions being interrupted or when a program can't find the proper data in memory at the last address it stored to due to a bug in the software or something else can cause windows to alert you or a memory overflow if a loop persists (pretty rare but it can occur as easily in Internet Explorer with an HTML document).
Post Reply