デクリメントを使用するタイミングとポストデクリメントを使用するタイミング
そして、次のコードスニペットのために、私は前または後の減少を使うべきです。
static private void function(int number)
{
charArr = new char[number];
int i = 0;
int tempCounter;
int j = 0;
while(charrArr!=someCharArr)
{
tempCounter = number - 1;
password[tempCounter] = element[i%element.Length];
i++;
//This is the loop the I want to use the decrementing in.
//Suppose I have a char array of size 5, the last element of index 5 is updated
//in the previous statement.
//About the upcoming indexes 4, 3, 2, 1 and ZERO.
//How should I implement it?
// --tempCounter or tempCounter-- ?
while (charArr[--tempCounter] == element[element.Length - 1])
{
}
}
}
値が残りの式に渡される前に変数をデクリメントする場合は、事前デクリメントを使用します。一方、ポストデクリメントは、変数がデクリメントされる前に式を評価します。
int i = 100, x;
x = --i; // both are 99
そして
int i = 100, x;
x = i--; // x = 100, i = 99
増分についても同じことが明らかに当てはまります。
あなたが持っているべきです++i;(それが問題ではない)、そして持っているべきですtempCounter--そうでなければ、charArrの "最初の"インデックスを見逃すでしょう。
last index - 1へindex = ZERO。だからループの条件で私は使用する必要がありますtempCounter--または--tempCounter? - sikas--i;速いかもしれませんが、決して遅くなることはありませんi--;。しかし私達は両方とものような行を知っていますi--;特にiがint型の場合、コンパイラによって最適化されます。 - EnabrenTanewhile(tempCounter > 0) { charArr[tempCounter--] = /* value */ } - EnabrenTane
tempCounter = number;、password[tempCounter - 1]そしてcharArr[--tempCounter]という事実にもかかわらず、while-loopは初期化されていない配列に対しても機能します。tempCounter負になることができます。 - sjngm