Re: condition precedences in 'if'

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 08/12/2010 11:00 AM, mahmoodn wrote:
> 
> Dear all,
> 
> First consider this:
> bool flag = false;
> while (condition) {
>    ...
>    if ( !flag && ANOTHER_CONDITION ) {
>       ....
>    }
> }
> If the "while" loop iterates for 10 times, the both flag and
> ANOTHER_CONDITION are checked 10 times.
> 
> Now consider this:
> bool flag = false;
> while (condition) {
>    ...
>    if ( !flag && ANOTHER_CONDITION ) {
>       ....
>       flag = true;
>    }
> }
> If the "while" loop iterates for 10 times and in the first iteration "flag =
> true;" is executed, then how many times, ANOTHER_CONDITION is checked? 1 or
> 10?

Once.  It's part of the definition of && that its right-hand-side doesn't get
used if its left-hand-size evaluates to false.

Andrew.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux