# For Loop (C)

Page 1 of 1

## 5 Replies - 605 Views - Last Post: 13 September 2010 - 11:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=190221&amp;s=34368d3b408a1d7d80743ef771947452&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Matty919

Reputation: 1
• 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

• D.I.C Lover

Reputation: 1396
• 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?

### #3 Matty919

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

## Re: For Loop (C)

Posted 13 September 2010 - 10:58 AM

Oler1s, 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?

• Saucy!

Reputation: 6236
• Posts: 24,001
• 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.

### #5 Oler1s

• D.I.C Lover

Reputation: 1396
• 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

### #6 Matty919

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

## Re: For Loop (C)

Posted 13 September 2010 - 11:15 AM

Thanks for the help, makes sense now.