LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-06-2009, 01:57 AM   #1
japhy
LQ Newbie
 
Registered: Jan 2008
Distribution: (k)ubuntu, gentoo
Posts: 2

Rep: Reputation: 0
can't put an if statement as an increment in a for loop. Why?


I'm a novice, but I've put plenty of effort into figuring this out myself, and I'm baffled. When I look up the format for a for loop, it says there should be an "expression" in each of the three spots. an if statement counts as a single expression, no? But this:


1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i;
6
7 for (; i < 10; if (1) i++);
8
9 return 0;
10 }

gives me the error:
forLoopTest.c:7: error: expected expression before ‘if’.

and I found ten different examples of people fixing the error "expected expression before" . . . , and they all looked like totally different problems.

I know it doesn't work, and I can't think of any reason I'd need to use an if statement there, but I'm curious why the if statement doesn't work. And knowing what that error means would actually be useful.
 
Old 02-06-2009, 03:41 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by japhy View Post
should be an "expression" in each of the three spots. an if statement counts as a single expression, no?
No. an "if statement" is (surprise) a statement. Not an expression.

An expression results in a value, a statement 'does' something.
 
Old 02-07-2009, 06:07 AM   #3
shyamkumar1986
LQ Newbie
 
Registered: Feb 2009
Posts: 19

Rep: Reputation: 1
for (; i < 10; if (1) i++);

The part in bold has to be an expression - not an evaluation criteria per se. That is the syntactical limitation placed by the programming language


you could write the if (1) i++ part inside the for loop itself instead. In other words your code would look like this:


#include <stdio.h>

int main(void)
{
int i;

for (; i < 10; ) {
if (1)
i++;
printf("aaa");

}

return 0;

}



Hope this helps!

Last edited by shyamkumar1986; 02-09-2009 at 08:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Piping into a Case Statement embedded in a while loop telecom_is_me Programming 5 07-02-2008 04:45 PM
Something I put in rc.local falls into infinite loop, machine cannot boot... kindloaf Linux - Software 4 03-17-2008 03:01 PM
BASH Need help figuring out how to loop SELECT statement mcdrr Programming 2 05-08-2007 04:47 AM
facing problem in increment operator of set :infinite LOOP ashwinipahuja Programming 0 06-03-2004 12:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration