question about loops in C

Status
Not open for further replies.

kappa_am

Full Member level 6
Joined
Jul 16, 2012
Messages
331
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Vancouver
Visit site
Activity points
3,859
I am trying to understand a program that is written by C. unfortunately I am not able to under stand these three statements in "while" and IF condition. I will be grateful if someone explain these to me.
what are the meaning of:
binom = choose(n, ++c)) <= e
e == 0 || (binom = choose(n - 1, c - 1)) > e
--n && c && ((b <<= 1) || 1)

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
do {
e -= binom;
} while ((binom = choose(n, ++c)) <= e);
 
 
do {
if (e == 0 || (binom = choose(n - 1, c - 1)) > e)
c--, b |= 1;
else
e -= binom;
} while (--n && c && ((b <<= 1) || 1));

 
Last edited:

I make the first one equivalent to :


Code C - [expand]
1
2
3
4
do {
e -= binom;
binom = choose (n,++c);
} while ((binom <= e);



It is a combined assignment and comparision, and bad style in my view as it is unnecassarily difficult to read.


Code C - [expand]
1
if (e == 0 || (binom = choose(n - 1, c - 1)) > e)



This is subtle due to "Short circuit evaluation", but does the following logic:

if (e == 0) the expression evaluates as true and the rest is not evaluated, else
assign binom = choose (n-1, c-1),
if the resulting binom value is > e evaluate as true else evaluate as false.
It could use a comment, simply so that it does not need careful checking on review.


Code C - [expand]
1
while (--n && c && ((b <<= 1) || 1));



This is another where short circuit evaluation is the key.

Note that ((b<<=1) || 1) will always evaluate as true, but has a side effect which is really the point of this part, namely left shifting b one bit.
Considering short circuit evaluation, this reads as :
if --n == 0 evaluate as false else if c != 0 left shift b one bit and evaluate as true, else evaluate as false.

Again horrible style because it is hard to read, it really needs a comment at least if not restructuring into something far easier to follow.

73 Dan.
 
Last edited by a moderator:
Thank you
it was very helpful
what about following statements; specially I wonder why first for has not any initial value!
what are the meanings of the following two statements:

Code C - [expand]
1
2
for (; i <= b; i <<= 1)
for (i = 1 << (n - 1); i > 0 && c > 0; --n, i >>= 1)

 

Again it is written by someone who never wanted anyone else to understand it!

In line 1, the initial value must be set up somewhere else before this line. A 'for' loop only uses the first value to initialize the variable, if omitted it will retain the value it had before the loop. What the line is saying is:
"while the value of 'i' is less than or equal to 'b', double the value of 'i'".

Line 2 is even more ambiguous, it says "start with 'i' equal to 2 raised to the power of (n-1) and loop while both 'i' and 'c' are greater than zero and at each pass of the loop, decrement 'n' and also halve 'i' ".

Brian.
 

It is a combined assignment and comparision, and bad style in my view as it is unnecassarily difficult to read.

I agree wholeheartedly. There are times to use the combined operators, times they MUST be used, and this is not one of those times.
 

Oh I have used them, sometimes it really is the cleanest way, but like using goto it is a sign that you want to think about what you are doing because there is probably an easier way.

As we all know code is much harder to debug then it is to write in the first place, so it follows that if you write code as cleverly as you can, you are by definition not smart enough to debug it.

If that dogs dinner had come up in any code review I was doing it would have gone back with **much** red pen.

Regards, Dan.
 

Thank you all
and another question is what's meaning of this?
while main program accept one parameter!

Code C - [expand]
1
int main (int argc, char ** argv)

 

You will only find that kind of line in programs running under an operating system such as Linux, Windoze or OSX. It allows parameters to be passed to the program and the program to return a value to the operating system.

'argc' is the number (the quantity) of entries on the command line.
'argv' is a list of pointers to each of those parameters, usually argv[0] is the first entry on the command line so it is the name of the program itself.
The 'int main' means it will return an integer number to the operating system when the program terminates. Unless you change it with a 'return' instruction, it will usually be zero to indicate sucessful operation or a positive number to indicate a filing system error.

Brian.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…