可能であれば、 while in C++がこの 2 つの異なるコードをどのように解釈するかを知りたいです。
それらの 2 つの違いは、最初の while は既に値が与えられている前の var からロードされ、2 番目のコードは while を実行するときに変数の値が与えられることです。
以下のコードは、非常に単純な「ディレクトリ内のすべてのファイルを印刷する」プログラムの一部です。
1位。while を実行すると値が与えられます。意図したとおりにすべてのファイルを返します
ent = readdir(directory);
if(ent == NULL){
cout << "Cannot read directory!";
}else{
while((ent = readdir (directory)) != NULL){
cout << ent->d_name; //this one is the one which works fine; value is given when doing the while
}
}
2n. while を実行する前に変数に値が与えられます。while の最初の値を持つ無限ビュークルを返します。
ent = readdir(directory);
if(ent == NULL){
cout << "Cannot read directory!";
}else{
while((ent) != NULL){
cout << ent->d_name; //this one returns an infinite bucle of only the first value of the while
}
}
C++ はそれらをどのように解釈していますか?