4 Replies - 620 Views - Last Post: 11 February 2015 - 11:37 PM Rate Topic: -----

#1 Blue Fish   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-November 14

Infinite For Loop (Really Really Beginner Question)

Posted 10 February 2015 - 11:39 PM

Hi,
I'm really new to C++
So I learned how to make a basic for loop and I decided to try my best to make an infinite one. I thought that was a good way of learning. (Learning a small task then experimenting with it) But besides that.

Every time I run it, it doesn't say anything and just closes. Visual Studio doesn't say there's anything wrong with my code.

Here's the code
#include <iostream>
#include <conio.h> 


using namespace std; 


int main () 

{
	int d = 9; 
		
	for(int k = 10; k < d; k = k +1)
	{
		cout << "For loop value = " << k << endl; 
		getch(); 
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Infinite For Loop (Really Really Beginner Question)

#2 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1594
  • View blog
  • Posts: 5,010
  • Joined: 20-March 10

Re: Infinite For Loop (Really Really Beginner Question)

Posted 10 February 2015 - 11:43 PM

10 is not less than 9.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Infinite For Loop (Really Really Beginner Question)

Posted 11 February 2015 - 06:42 AM

Infinite loops are easy to create, although I don't see why you'd want one. They're often made accidentally. Yours would become one if you changed k < d to k > d
Note also that:
  • main() needs to return an integer.
  • iostream is C++ while conio.h is nonstandard C. You should pick a language.

Was This Post Helpful? 0
  • +
  • -

#4 Blue Fish   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-November 14

Re: Infinite For Loop (Really Really Beginner Question)

Posted 11 February 2015 - 10:52 PM

View PostCTphpnwb, on 11 February 2015 - 06:42 AM, said:

Infinite loops are easy to create, although I don't see why you'd want one. They're often made accidentally. Yours would become one if you changed k < d to k > d
Note also that:
  • main() needs to return an integer.
  • iostream is C++ while conio.h is nonstandard C. You should pick a language.


Ohhh okay thanks
Was This Post Helpful? 0
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Infinite For Loop (Really Really Beginner Question)

Posted 11 February 2015 - 11:37 PM

Quote

They're often made accidentally. Yours would become one if you changed k < d to k > d


Technically it wouldn't be an infinite loop since it would break after the int overflowed :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1