473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

help really confused with for construct execution

hello,
I am really confused over following for code snippets. No problem
that they are working codes but how? how they are evaluated by
compiler? It will be my pleasure if you explain me all following
snippets.

1) {
int i=3;
for (i--; i<7; i=7)
printf("%d",i++);
}
2) {
int i;
for (i=5; --i;)
printf("%d",i);
}
3) {
int i;
for (i=-10; !i; i++);
printf("%d",-i);
}
4) {
int i;
for (;(i=4)?(i-4):i++;)
printf("%d",i);
}

5) {
static int j;
for (j<5; j<5; j+=j<5)
printf("%d",j++);
}

Nov 15 '05 #1
6 1514


ra*******@gmail.com wrote:
hello,
I am really confused over following for code snippets. No problem
that they are working codes but how? how they are evaluated by
compiler? It will be my pleasure if you explain me all following
snippets.

1) {
int i=3;
for (i--; i<7; i=7)
printf("%d",i++);
} 'For' statments can be replaced by 'while' statments.So the above
statments equals to:
int i=3;
i--;
while(i<7){
printf("%d",i++);
i=7;
}
So the 'printf' statment is only executed once. 2) {
int i;
for (i=5; --i;)
printf("%d",i);
} As the above,it equals to:
int i;
while(i=5){/*Notice here!*/
printf("%d",i);
--i;
}
It is an infinite loop. 3) {
int i;
for (i=-10; !i; i++);
printf("%d",-i);
} It is bad that you add ';' after 'for' statment.It does nothing and
'!i' is 0 at first. 4) {
int i;
for (;(i=4)?(i-4):i++;)
printf("%d",i);
}
The value of the expression "(i=4)?(i-4):i++" is 0.So the loop does
nothing. 5) {
static int j;
for (j<5; j<5; j+=j<5)
printf("%d",j++);
}

This one is interesting.The first 'j<5' does nothing and 'j+=j<5'
equals to 'j++'.

Nov 15 '05 #2


Cong Wang wrote:
ra*******@gmail.com wrote:
2) {
int i;
for (i=5; --i;)
printf("%d",i);
}

As the above,it equals to:
int i;
while(i=5){/*Notice here!*/
printf("%d",i);
--i;
}
It is an infinite loop.


it is equivalent to

int i;
i = 5;
while(--i)
printf("%d",i);

so output will be 4321 and not an infinite loop.

Nov 15 '05 #3
> 5) {
static int j;
for (j<5; j<5; j+=j<5)

This is the same as:
for( ; j<5; j+=(j<5))

j<5 gives 1 when it is true, so j+=(j<5) equals to j+=1;
it gives 0 when it is false, so j+=(j<5) equals to j+=0.

Nov 15 '05 #4


ma*******@gmail.com wrote:
Cong Wang wrote:
ra*******@gmail.com wrote:
2) {
int i;
for (i=5; --i;)
printf("%d",i);
}

As the above,it equals to:
int i;
while(i=5){/*Notice here!*/
printf("%d",i);
--i;
}
It is an infinite loop.


it is equivalent to

int i;
i = 5;
while(--i)
printf("%d",i);

so output will be 4321 and not an infinite loop.

Very sorry!I didn't see it clearly.I apologize!

Nov 15 '05 #5


rahul8...@gmail.com wrote:
hello,
I am really confused over following for code snippets. No problem
that they are working codes but how? how they are evaluated by
compiler? It will be my pleasure if you explain me all following
snippets.

This smells like homework, but I feel generous today.
1) {
int i=3;
for (i--; i<7; i=7)
printf("%d",i++);
}
1. i is initialized to 3
2. the i-- expression is evaluated; i now equals 2
3. the i<7 expression is evaluated; 2 < 7 == true, so the loop body is
executed
4. the printf() statement is executed; as a side effect, i is
incremented by 1, and now equals 3 again
5. the i=7 expression is evaluated; i now equals 7
6. the i<7 expression is evaluated; 7 < 7 == false, so the loop exits
2) {
int i;
for (i=5; --i;)
printf("%d",i);
}
1. i is declared with no initial value
2. the i=5 expression is evaluated; i now equals 5
3. the --i expression is evaluated; the resulting value (4) is
non-zero (true), so the loop body is executed
4. print the current value of i (4)
5. There is no update expression to evaluate
6. Repeat steps 3 through 5 should be three more times
7. the --i expression is evaluated; the resulting value is 0 (false),
so the loop exits
3) {
int i;
for (i=-10; !i; i++);
printf("%d",-i);
}
1. i is declared with no initial value
2. the expression i=-10 is evaluated; i now equals -10
3. the expression !i is evaluated; since -10 is non-zero (true), the
expression !i evaluates to 0 (false), and the loop exits without doing
anything.
4) {
int i;
for (;(i=4)?(i-4):i++;)
printf("%d",i);
}

1. i is declared with no initial value
2. there is no initialization expression
3. the expression (i=4) is evaluated; if the result is non-zero (which
it is, since the result of i=4 is 4), then the expression i-4 is
evaluated, otherwise the expression i++ is evaluated. Since the
expression i-4 is evaluated, the result is 4-4, or 0 (false), so the
loop exits without doing anything.

5) {
static int j;
for (j<5; j<5; j+=j<5)
printf("%d",j++);
}


1. j is declared with no initial value but with static extent; this
implicitly initializes j to zero.
2. the first j<5 expression is executed; the result is 1 (true)
3. the second j<5 expression is evaluated; the result is 1 (true), so
the loop body is executed
4. the printf() statement is executed; as a side effect, j is
incremented by 1.
5. the expression j+=j<5 is evaluated; the result of the expression j
< 5 (1 if j is less than 5, 0 if j is greater than or equal to 5) is
added back to j.
6. repeat steps 3 through 5 until j >= 5 (should be two more times).

Nov 15 '05 #6
ra*******@gmail.com wrote:
hello,
I am really confused over following for code snippets. No problem
that they are working codes but how? how they are evaluated by
compiler? It will be my pleasure if you explain me all following
snippets.

1) {
int i=3;
for (i--; i<7; i=7)
printf("%d",i++);
}
2) {
int i;
for (i=5; --i;)
printf("%d",i);
}
3) {
int i;
for (i=-10; !i; i++);
printf("%d",-i);
}
4) {
int i;
for (;(i=4)?(i-4):i++;)
printf("%d",i);
}

5) {
static int j;
for (j<5; j<5; j+=j<5)
printf("%d",j++);
}


The statement

for (e1; e2; e3) {
...
}

is equivalent to

e1;
while (e2) {
...
e3;
}

That probably explains your questions.
August
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
1
by: Liza | last post by:
Hi guys, i'm trying to build a web service....... is there such thing as polymorphism in web services? i mean could i have two web services of the same name but different arguments such that i...
6
by: Mark Reed | last post by:
Hi all, I am trying to learn a little about programming (I know next to nothing so far) and have found some code which hides the toolbars. However, this bit of code is a little too effective and...
42
by: slickn_sly | last post by:
<code> line = 1; while( !feof( infile ) ) { if( line = 2 ) { fgets( name, 25, infile ); } else if( line = 3 ) {
4
by: Chronologic | last post by:
All, I have an issue I would like some expert help on. I understand, or so I believe, that C# does not support the concept of a "compile time macro". At least not in the sense I'm looking...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
7
by: Satish | last post by:
Hi Friends I am little confused about the shadows keyword in VB.NET could anyone explain with an example about Shadows keyword Many thanks Satish
13
by: Marvin | last post by:
Hi: I have been programming with OOP and C++ for over 10 years but I am new to javascript. I am very confused about one aspect and would appreciate it if someone could explain the differences...
7
by: Ronald S. Cook | last post by:
I've always been taught that stored procedures are better than writing SQL in client code for a number of reasons: - runs faster as is compiled and lives on the database server - is the more...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.