Re: Strange abortion of for loop
From: Ioannis Vranos (ivr_at_guesswh.at.emails.ru)
Date: 04/14/04
- Next message: Dave Moore: "Re: Birthday Problem"
- Previous message: Rolf Magnus: "Re: Problems with multiplications of doubles and/or floats"
- In reply to: hall: "Strange abortion of for loop"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 14 Apr 2004 18:19:40 +0300
"hall" <Xcrackhead_eX@yahoo.se> wrote in message
news:c5jh9q$3m2$1@eol.dd.chalmers.se...
> Hi.
>
> I've come across someting strange. I was trying to make a for-loop
> execute repetadly until the function called inside it does not return
> true during the entire loop (see program below).
>
> The two lines that confuse me are marked as (1) and (2).
>
> count=0;
> bool s(true);
> while(s){
> s=false;
> for (int x=0; x<10; ++x){
> //s = s||f(x,count); //(1)
>
> bool tmp =f(x, count); s=s||tmp; //(2)
s=s||tmp means that if s is true it gets reassigned the true value else if
tmp is true s becomes true else if noone is true it is reassigned the false
value since s||tmp evaluates to false.
> }
> count ++;
> }
> where f(x,count) is a bool function, returning true for count<3
>
> I thought that they both do the same, ie fun() is executed and bool
> variable s = s OR (return from function). However, line (1) will abort
> the execution of the for loop already when x=0,
I can't see how line (1) can abort the for loop unless an exception is
thrown or a signal is raised. If you mean that the function call is not
executed 10 times, it happens when after f(x,count) returns true and s gets
that value, at the next evaluations, s evaluates to true so the second
expression is not checked (and thus your functions is not executed at all).
If you want your function to be run 10 times you can make it:
for (int x=0; x<10; ++x){
s = f(x,count) || s;
Ioannis Vranos
- Next message: Dave Moore: "Re: Birthday Problem"
- Previous message: Rolf Magnus: "Re: Problems with multiplications of doubles and/or floats"
- In reply to: hall: "Strange abortion of for loop"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|