working of operators in while loop(c programming)
up vote
0
down vote
favorite
I am beginner to c programming. Hope you guys can help me.
i=0;
while(i++<12)
printf("%dn",i);
My question not regarding operators or how the code works. I have checked various forms so i know what this piece of code does and the final value in i will be 13. What I want to know is this:
From my perspective operators must follow operator priority(preference). So ++ must be evaluated before <.
Operator priority link: https://www.geeksforgeeks.org/c-operator-precedence-associativity/
lets i=3 for now,
Step 1:while(i++<12)
Step 2:while((i+1)<12)
Step 3:while(4<12)
Step 4:true and enters while loop
If operator preference is neglected(but I don't know why):
Step 1:while(i++<12)
Step 2:while(i<12)
Step 3:while(3<12) which returns value 1
Step 4:while(1++) operator ++ on the return value 1 from above step
Step 5:while(2) which is true and enters while loop
I hope you guys can understand my question. Please help.
c while-loop operator-keyword
|
show 7 more comments
up vote
0
down vote
favorite
I am beginner to c programming. Hope you guys can help me.
i=0;
while(i++<12)
printf("%dn",i);
My question not regarding operators or how the code works. I have checked various forms so i know what this piece of code does and the final value in i will be 13. What I want to know is this:
From my perspective operators must follow operator priority(preference). So ++ must be evaluated before <.
Operator priority link: https://www.geeksforgeeks.org/c-operator-precedence-associativity/
lets i=3 for now,
Step 1:while(i++<12)
Step 2:while((i+1)<12)
Step 3:while(4<12)
Step 4:true and enters while loop
If operator preference is neglected(but I don't know why):
Step 1:while(i++<12)
Step 2:while(i<12)
Step 3:while(3<12) which returns value 1
Step 4:while(1++) operator ++ on the return value 1 from above step
Step 5:while(2) which is true and enters while loop
I hope you guys can understand my question. Please help.
c while-loop operator-keyword
2
You really need to study C.1=0
does not make any sense, whatsoever.
– gsamaras
Nov 7 at 10:02
"First i>12 will be checked to return value 1. Then ++(increment operator) must be performed on the return value '1' " I don't understand what you mean here. Do you mean that the > check returns value 1 or that i++ returns value 1? Where did you get 2 from?
– Lundin
Nov 7 at 10:04
is that1=0
a typo fori=0
?
– Chris Turner
Nov 7 at 10:04
1
Yes. Sorry I am beginner for the forums. I will try to not make mistakes again
– KRISHNA I
Nov 7 at 10:05
2
No,i++
means thati
is incremented after the comparisoni < 12
.
– Weather Vane
Nov 7 at 10:14
|
show 7 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am beginner to c programming. Hope you guys can help me.
i=0;
while(i++<12)
printf("%dn",i);
My question not regarding operators or how the code works. I have checked various forms so i know what this piece of code does and the final value in i will be 13. What I want to know is this:
From my perspective operators must follow operator priority(preference). So ++ must be evaluated before <.
Operator priority link: https://www.geeksforgeeks.org/c-operator-precedence-associativity/
lets i=3 for now,
Step 1:while(i++<12)
Step 2:while((i+1)<12)
Step 3:while(4<12)
Step 4:true and enters while loop
If operator preference is neglected(but I don't know why):
Step 1:while(i++<12)
Step 2:while(i<12)
Step 3:while(3<12) which returns value 1
Step 4:while(1++) operator ++ on the return value 1 from above step
Step 5:while(2) which is true and enters while loop
I hope you guys can understand my question. Please help.
c while-loop operator-keyword
I am beginner to c programming. Hope you guys can help me.
i=0;
while(i++<12)
printf("%dn",i);
My question not regarding operators or how the code works. I have checked various forms so i know what this piece of code does and the final value in i will be 13. What I want to know is this:
From my perspective operators must follow operator priority(preference). So ++ must be evaluated before <.
Operator priority link: https://www.geeksforgeeks.org/c-operator-precedence-associativity/
lets i=3 for now,
Step 1:while(i++<12)
Step 2:while((i+1)<12)
Step 3:while(4<12)
Step 4:true and enters while loop
If operator preference is neglected(but I don't know why):
Step 1:while(i++<12)
Step 2:while(i<12)
Step 3:while(3<12) which returns value 1
Step 4:while(1++) operator ++ on the return value 1 from above step
Step 5:while(2) which is true and enters while loop
I hope you guys can understand my question. Please help.
c while-loop operator-keyword
c while-loop operator-keyword
edited Nov 7 at 12:11
asked Nov 7 at 10:00
KRISHNA I
63
63
2
You really need to study C.1=0
does not make any sense, whatsoever.
– gsamaras
Nov 7 at 10:02
"First i>12 will be checked to return value 1. Then ++(increment operator) must be performed on the return value '1' " I don't understand what you mean here. Do you mean that the > check returns value 1 or that i++ returns value 1? Where did you get 2 from?
– Lundin
Nov 7 at 10:04
is that1=0
a typo fori=0
?
– Chris Turner
Nov 7 at 10:04
1
Yes. Sorry I am beginner for the forums. I will try to not make mistakes again
– KRISHNA I
Nov 7 at 10:05
2
No,i++
means thati
is incremented after the comparisoni < 12
.
– Weather Vane
Nov 7 at 10:14
|
show 7 more comments
2
You really need to study C.1=0
does not make any sense, whatsoever.
– gsamaras
Nov 7 at 10:02
"First i>12 will be checked to return value 1. Then ++(increment operator) must be performed on the return value '1' " I don't understand what you mean here. Do you mean that the > check returns value 1 or that i++ returns value 1? Where did you get 2 from?
– Lundin
Nov 7 at 10:04
is that1=0
a typo fori=0
?
– Chris Turner
Nov 7 at 10:04
1
Yes. Sorry I am beginner for the forums. I will try to not make mistakes again
– KRISHNA I
Nov 7 at 10:05
2
No,i++
means thati
is incremented after the comparisoni < 12
.
– Weather Vane
Nov 7 at 10:14
2
2
You really need to study C.
1=0
does not make any sense, whatsoever.– gsamaras
Nov 7 at 10:02
You really need to study C.
1=0
does not make any sense, whatsoever.– gsamaras
Nov 7 at 10:02
"First i>12 will be checked to return value 1. Then ++(increment operator) must be performed on the return value '1' " I don't understand what you mean here. Do you mean that the > check returns value 1 or that i++ returns value 1? Where did you get 2 from?
– Lundin
Nov 7 at 10:04
"First i>12 will be checked to return value 1. Then ++(increment operator) must be performed on the return value '1' " I don't understand what you mean here. Do you mean that the > check returns value 1 or that i++ returns value 1? Where did you get 2 from?
– Lundin
Nov 7 at 10:04
is that
1=0
a typo for i=0
?– Chris Turner
Nov 7 at 10:04
is that
1=0
a typo for i=0
?– Chris Turner
Nov 7 at 10:04
1
1
Yes. Sorry I am beginner for the forums. I will try to not make mistakes again
– KRISHNA I
Nov 7 at 10:05
Yes. Sorry I am beginner for the forums. I will try to not make mistakes again
– KRISHNA I
Nov 7 at 10:05
2
2
No,
i++
means that i
is incremented after the comparison i < 12
.– Weather Vane
Nov 7 at 10:14
No,
i++
means that i
is incremented after the comparison i < 12
.– Weather Vane
Nov 7 at 10:14
|
show 7 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
An example:
i = 0;
while(i<12) //compare i with 12
printf("%dn",i);
i = 0;
while(i++<12) //compare i with 12 and THEN i=i+1
printf("%dn",i);
i = 0;
while(++i<12) //first i=i+1 and then compare i with 12
printf("%dn",i);
Thanks Mike. I know the procedure you explained. What I want to say is: while(i++<12) is in the code I expected that both ++ and < will be evaluated before passing the value to while.
– KRISHNA I
Nov 7 at 11:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
An example:
i = 0;
while(i<12) //compare i with 12
printf("%dn",i);
i = 0;
while(i++<12) //compare i with 12 and THEN i=i+1
printf("%dn",i);
i = 0;
while(++i<12) //first i=i+1 and then compare i with 12
printf("%dn",i);
Thanks Mike. I know the procedure you explained. What I want to say is: while(i++<12) is in the code I expected that both ++ and < will be evaluated before passing the value to while.
– KRISHNA I
Nov 7 at 11:54
add a comment |
up vote
0
down vote
An example:
i = 0;
while(i<12) //compare i with 12
printf("%dn",i);
i = 0;
while(i++<12) //compare i with 12 and THEN i=i+1
printf("%dn",i);
i = 0;
while(++i<12) //first i=i+1 and then compare i with 12
printf("%dn",i);
Thanks Mike. I know the procedure you explained. What I want to say is: while(i++<12) is in the code I expected that both ++ and < will be evaluated before passing the value to while.
– KRISHNA I
Nov 7 at 11:54
add a comment |
up vote
0
down vote
up vote
0
down vote
An example:
i = 0;
while(i<12) //compare i with 12
printf("%dn",i);
i = 0;
while(i++<12) //compare i with 12 and THEN i=i+1
printf("%dn",i);
i = 0;
while(++i<12) //first i=i+1 and then compare i with 12
printf("%dn",i);
An example:
i = 0;
while(i<12) //compare i with 12
printf("%dn",i);
i = 0;
while(i++<12) //compare i with 12 and THEN i=i+1
printf("%dn",i);
i = 0;
while(++i<12) //first i=i+1 and then compare i with 12
printf("%dn",i);
answered Nov 7 at 10:36
Mike
1,5931421
1,5931421
Thanks Mike. I know the procedure you explained. What I want to say is: while(i++<12) is in the code I expected that both ++ and < will be evaluated before passing the value to while.
– KRISHNA I
Nov 7 at 11:54
add a comment |
Thanks Mike. I know the procedure you explained. What I want to say is: while(i++<12) is in the code I expected that both ++ and < will be evaluated before passing the value to while.
– KRISHNA I
Nov 7 at 11:54
Thanks Mike. I know the procedure you explained. What I want to say is: while(i++<12) is in the code I expected that both ++ and < will be evaluated before passing the value to while.
– KRISHNA I
Nov 7 at 11:54
Thanks Mike. I know the procedure you explained. What I want to say is: while(i++<12) is in the code I expected that both ++ and < will be evaluated before passing the value to while.
– KRISHNA I
Nov 7 at 11:54
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187178%2fworking-of-operators-in-while-loopc-programming%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You really need to study C.
1=0
does not make any sense, whatsoever.– gsamaras
Nov 7 at 10:02
"First i>12 will be checked to return value 1. Then ++(increment operator) must be performed on the return value '1' " I don't understand what you mean here. Do you mean that the > check returns value 1 or that i++ returns value 1? Where did you get 2 from?
– Lundin
Nov 7 at 10:04
is that
1=0
a typo fori=0
?– Chris Turner
Nov 7 at 10:04
1
Yes. Sorry I am beginner for the forums. I will try to not make mistakes again
– KRISHNA I
Nov 7 at 10:05
2
No,
i++
means thati
is incremented after the comparisoni < 12
.– Weather Vane
Nov 7 at 10:14