5 Replies - 386 Views - Last Post: 13 September 2010 - 11:15 AM Rate Topic: -----

#1 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

For Loop (C)

Posted 13 September 2010 - 10:40 AM

I'm angry at myself for not being able to work this out but anyway.

Why isn't this working?

#include <stdio.h>

main()
{
    int x;

    for (x = 10; x = 0; x = x - 1)
        printf("x is %d\n", x);

}


I don't see why it doesn't work. x is assigned 10 and told to -1 to x until 0 is reached right?
Is This A Good Question/Topic? 0
  • +

Replies To: For Loop (C)

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: For Loop (C)

Posted 13 September 2010 - 10:51 AM

for (x = 10; x = 0; x = x - 1) How do you think x = 0 is evaluated?
Was This Post Helpful? 0
  • +
  • -

#3 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: For Loop (C)

Posted 13 September 2010 - 10:58 AM

View PostOler1s, on 13 September 2010 - 09:51 AM, said:

for (x = 10; x = 0; x = x - 1) How do you think x = 0 is evaluated?


How it's evaluated? I don't know. Isn't it checked it doesn't equal 0 everytime the loop is executed to check if it is still true? :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: For Loop (C)

Posted 13 September 2010 - 11:00 AM

x = 0 ASSIGNS the value 0 to x. = is for ASSIGNMENT, == is for COMPARISON.

The middle statement in a for loop says "run while this statement is true". Think about that when reformulating it.
Was This Post Helpful? 1
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: For Loop (C)

Posted 13 September 2010 - 11:03 AM

Quote

Isn't it checked it doesn't equal 0 everytime the loop is executed to check if it is still true?
How do you reach this conclusion?

Evaluate the statements logically. x=0 is assigning x to 0. Then x is evaluated. Since x is 0, it's the same as evaluating 0. 0 translates to false. You may as well have written the following: for(x = 10; false; x = x -1) . This loop will never iterate. The condition is always false. How do you compare x to 0? There's a few different operators, like <, and >. What's the operator for "not equal to"?

EDIT: Or rather, since this is C, there's no boolean type, but 0 is treated as false.

This post has been edited by Oler1s: 13 September 2010 - 11:04 AM

Was This Post Helpful? 2
  • +
  • -

#6 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: For Loop (C)

Posted 13 September 2010 - 11:15 AM

Thanks for the help, makes sense now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1