loop-counter

Is it possible to use a for loop to change a variable name in C?

家住魔仙堡 提交于 2019-12-24 04:05:11
问题 This is a generic question, so there is no actual code that I am trying to troubleshoot. But what I want to know is, can I use a for loop to change the name of a variable in C? For instance, if I have part1 , part2 , part3 , part... , as my variable names; is there a way to attach it to my loop counter so that it will increment with each passing? I toyed around with some things, nothing seemed to work. 回答1: In C, you can't 'change the name of the loop variable' but your loop variable does not

Should I expect to see the counter in `for` loop changed inside its body? [closed]

会有一股神秘感。 提交于 2019-12-12 10:29:03
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . I'm reading someone else's code and they separately increment their for loop counter inside the loop, as well as including the usual afterthought. For example: for( int y = 4; y < 12; y++ ) { // blah if( var < othervar ) { y++; } // blah } Based on the majority of code others

how to test condition properly: je or jge

核能气质少年 提交于 2019-12-11 14:49:46
问题 I sometimes use this pattern to iterate array of something: mov [rsp+.r12], r12 ; Choose a register that calls inside the loop won't modify mov r12, -1 .i: inc r12 cmp r12, [rbp-.array_size] je .end_i ; ... program logic ... jmp .i .end_i: mov r12, [rsp+.r12] I understand that it is enough to test for equality but should not one "securely" test for "greater or equal"(prevent situation that will not occur). Should one use je or jge in this cases? I am asking about concrete tip that can reduce

Who “invented” i,j,k as integer counter variable names? [duplicate]

房东的猫 提交于 2019-12-10 15:45:45
问题 This question already exists : Closed 10 years ago . Possible Duplicate: Why are we using i as a counter in loops I've used these myself for more than 15 years but cannot really remember how/where I picked up that habit. As it is really widespread, I'm curious to know who originally suggested / recommended using these names for integer loop counters (was it the K&R book?). 回答1: i = integer Comes from Fortran where integer variables had to start with the letters I through N and real variables

Any risk of using float variables as loop counters and their fractional increment/decrement for non “==” conditions?

拥有回忆 提交于 2019-12-01 18:43:19
问题 Are we safe to use floats as loop-counters and to increment/decrement them by fractional amounts at each iteration,like in the seemingly risk-free program below?Of course I know that using floats as operands for the == operator is a dumb thing to do.But what's wrong with using floats as operands for other comparison operations for "normal" purposes? By "normal" I mean that,well,even though floats may not be the exact numerical representation of the number,but isn't a variation like 0

Any risk of using float variables as loop counters and their fractional increment/decrement for non “==” conditions?

对着背影说爱祢 提交于 2019-12-01 18:26:40
Are we safe to use floats as loop-counters and to increment/decrement them by fractional amounts at each iteration,like in the seemingly risk-free program below?Of course I know that using floats as operands for the == operator is a dumb thing to do.But what's wrong with using floats as operands for other comparison operations for "normal" purposes? By "normal" I mean that,well,even though floats may not be the exact numerical representation of the number,but isn't a variation like 0.000000001 irrelevant and can be ignored in most cases? (For example in the following program that isn't even

When should I use std::size_t?

旧街凉风 提交于 2019-11-26 15:36:42
I'm just wondering should I use std::size_t for loops and stuff instead of int ? For instance: #include <cstdint> int main() { for (std::size_t i = 0; i < 10; ++i) { // std::size_t OK here? Or should I use, say, unsigned int instead? } } In general, what is the best practice regarding when to use std::size_t ? A good rule of thumb is for anything that you need to compare in the loop condition against something that is naturally a std::size_t itself. std::size_t is the type of any sizeof expression and as is guaranteed to be able to express the maximum size of any object (including any array)

Should I use std::size_t or int in my for loops?

佐手、 提交于 2019-11-26 04:29:38
问题 I\'m just wondering should I use std::size_t for loops and stuff instead of int ? For instance: #include <cstdint> int main() { for (std::size_t i = 0; i < 10; ++i) { // std::size_t OK here? Or should I use, say, unsigned int instead? } } In general, what is the best practice regarding when to use std::size_t ? 回答1: A good rule of thumb is for anything that you need to compare in the loop condition against something that is naturally a std::size_t itself. std::size_t is the type of any sizeof