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.










share|improve this question




















  • 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 for i=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 that i is incremented after the comparison i < 12.
    – Weather Vane
    Nov 7 at 10:14

















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.










share|improve this question




















  • 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 for i=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 that i is incremented after the comparison i < 12.
    – Weather Vane
    Nov 7 at 10:14















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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 that 1=0 a typo for i=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 that i is incremented after the comparison i < 12.
    – Weather Vane
    Nov 7 at 10:14
















  • 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 for i=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 that i is incremented after the comparison i < 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














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);





share|improve this answer





















  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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);





share|improve this answer





















  • 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

















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);





share|improve this answer





















  • 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















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);





share|improve this answer












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);






share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()