question about an incrimenting method in for loop

This is a discussion on question about an incrimenting method in for loop within the C Programming forums, part of the General Programming Boards category; Hello, I'm new to programming and when i was creating a simple program which prints a set of numbers, I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    question about an incrimenting method in for loop

    Hello,
    I'm new to programming and when i was creating a simple program which prints a set of numbers, I incremented the for loop as x=+1 but when I executed this, 1 was written again and again without an end of execution, does anyone have an idea on what might be wrong.
    below is the code for reference, Also I'm using Bcc 5.5 compiler

    Code:
    #include<stdio.h>
    #include<math.h>
    void main()
    {
    	int x;
    	x=0;
    	for(x=0;x<10;x=+1)
    	{
    		if (x==5)
    		break;
    		printf ("%d\n", x);
    	}
    }

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Clearly you meant +=, not =+. Also, main() should be defined as int main(void) and must return 0.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jack41 View Post
    Hello,
    I'm new to programming and when i was creating a simple program which prints a set of numbers, I incremented the for loop as x=+1 but when I executed this, 1 was written again and again without an end of execution, does anyone have an idea on what might be wrong.
    below is the code for reference, Also I'm using Bcc 5.5 compiler

    Code:
    #include<stdio.h>
    #include<math.h>
    void main()
    {
    	int x;
    	x=0;
    	for(x=0;x<10;x=+1)
    	{
    		if (x==5)
    		break;
    		printf ("%d\n", x);
    	}
    }
    What you wrote... for(x=0;x<10;x=+1)
    How the compiler saw it... for(x=0;x<10;x=(+1))

    When setting up your source code use some spacing...
    Code:
    for ( x = 0; x < 10; x += 1 )
    It makes no difference to the compiler but it makes mistakes a whole lot easier for us humans to spot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about method
    By nimitzhunter in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2010, 08:52 PM
  2. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 11:35 AM
  3. Virtual Method question...
    By keira in forum C++ Programming
    Replies: 14
    Last Post: 05-23-2008, 08:56 PM
  4. Question about this method...
    By Raigne in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2008, 07:43 PM
  5. A question about C++ random generator and unsigned method
    By joenching in forum C++ Programming
    Replies: 13
    Last Post: 03-14-2005, 04:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21