Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my for loop works with "&&" and not with ","

Tags:

c

for-loop

I am stuck at this exercise of c program that have a comma in C for loop if, I replace , with && it works same

for(i = 5, j = i - 1 ; i > 0 , j > 0 ; --i ,j = i - 1)
        printf("\n%d",i);

In this loop I get how for( i = 5,j = i - 1 ; ? ; --i ,j= i - 1) but the part where ? is there I don't get how that is working 1,1 = true ? 1,0 = false ? C is trick that's why love it 3> can you explain me how that part is working

like image 567
MONUDDIN TAMBOLI Avatar asked Mar 12 '21 18:03

MONUDDIN TAMBOLI


People also ask

Why is my while loop not working?

The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).

What does a for loop need to work?

A for loop has two parts: a header specifying the iteration, and a body which is executed once per iteration. The header often declares an explicit loop counter or loop variable, which allows the body to know which iteration is being executed.

What are the 3 things in a for loop?

Similar to a While loop, a For loop consists of three parts: the keyword For that starts the loop, the condition being tested, and the EndFor keyword that terminates the loop.

Why is my for loop not running in C?

Make sure malloc didn't return zero. Make sure scanf returns the number of inputs you expected to read. Add in printouts to make sure the values it read in are what you expected/wanted to read in.


2 Answers

but the part where ? is there i don't get how that is working

The comma operator in C evaluates the expression before the comma, and then the expression after the comma, and then returns the value of the expression after the comma. So the value of the expression 1, 0 in C is 0. The result of 1, 1 is 1. The result of foo(x), bar(x) is the value of bar(x).

This doesn't come up very often because in practice, the comma operator isn't used all that often. It can be handy in a few cases, such as in for or while loops where you might want to manipulate more than one variable each time through the loop. But in general, combining expressions with the comma operator just creates uncertainty about how those expressions will be evaluated, what the result of the overall expression is, and so on. Whenever possible, separate the expressions and execute them one at a time.

Why my for loop works with "&&" and not with ","

The && operator combines (using logical AND) the result of both expressions instead of throwing the result of the first expression away, so depending on the expression you can get a different result than you do with ,. 1 ? 0 and 1 ? 1 give the same result for both , and && because the result of the && depends on the second expression in both cases. But 0 ? 0 and 0 ? 1 will give different results — , again returns the value of the second expression, and && returns 0 because both expressions are considered and 0 AND anything is 0.

like image 155
Caleb Avatar answered Oct 16 '22 20:10

Caleb


; i > 0 , j > 0 ;

Formally, the "," is know as the Comma operator:

The comma operator expression has the form
lhs , rhs
where
lhs - any expression rhs - any expression other than another comma operator (in other words, comma operator's associativity is left-to-right)

First, the left operand, lhs, is evaluated and its result value is discarded. Then, a sequence point takes place, so that all side effects of lhs are complete. Then, the right operand, rhs, is evaluated and its result is returned by the comma operator as a non-lvalue.

More informally, in ;i > 0 , j > 0; the i > 0 is evaluated and ignored to determined if the loop should or not terminate, and then j > 0 is evaluated and its value is used to determined if the loop should (or not) continue.

I am stuck at this exercise of c program that have a comma in C for loop if, I replace , with && it works same

In this case it works the same with "," or "&&" because when i = 1 then j = 0 and consequently j > 0 evaluates to false and you exit the loop. When j > 0 evaluates to false i > 0 also evaluates to false, hence the reason why in your case it is the same using i > 0 && j > 0 or i > 0 , j > 0. Nevertheless, i > 0 && j > 0 and i > 0 , j > 0 are not semantically the same.

In your case:

for(i = 5, j = i - 1 ; i > 0 && j > 0 ; --i ,j = i - 1)

actually, you can simplified i > 0 && j > 0 to just i > 0 since whenever j > 0 evaluates to false i > 0 will also evaluate to false since j = i - 1.

like image 42
dreamcrash Avatar answered Oct 16 '22 22:10

dreamcrash