logo

C の For ループと While ループ

for ループと while ループの違いを理解する

for ループ、while ループ、do-while ループなどの C++ の反復ステートメントを使用すると、条件が true になるまで一連の命令を繰り返し実行し、条件が false になると終了します。反復ステートメントには、for ループなどの事前定義された条件や、while ループなどの無制限の条件を含めることができます。

C++ では、言語の適用性、能力、柔軟性を高めるために、さまざまな 'for' ループのバリエーションが暗黙的に含まれています。たとえば、for ループを使用すると、ループ内で複数の変数を使用したり、「for」ループで converge 関数を使用したりしてループを制御できます。対照的に、while ループでは多くのバリエーションを使用できません。標準構文で使用する必要があります。

for ループと while ループの間にはいくつかの大きな違いがあります。比較表を使用してさらに説明します。

C の For ループと While ループ

for ループは次のように定義されます

Java には 2 種類の for ループがあります。 1 つ目は「従来の」形式で、2 つ目は「for-each」形式です。

設定メニューを開く

for ループ ステートメントの最も一般的な形式。

 for (initialization; condition; iteration) { //body of for loop } 
    初期化:for ループのループ制御変数は、ループの最初の反復中に 1 回だけ初期化されます。ループ制御変数はここで初期化されます。ループ変数がプログラム内で再度使用されず、ループの制御変数としてのみ使用される場合、その変数は「for」ループ内で宣言および初期化されます。状態:「for」ループの条件は、ループが反復されるたびに実行されます。
  • 反復ステートメントは、ループ制御変数をインクリメントまたはデクリメントする式です。

ループが実行されると、最初に初期化条件が実行され、続いて条件チェックが実行されます。条件が満たされると、ループの本体が実行され、その後に反復ステートメントが続きます。次に、条件が再度チェックされて、ループがさらに反復されるか終了するかが決定されます。

Java では、初期化ステートメントと反復ステートメントの両方に複数のステートメントを含めることができます。各ステートメントはカンマで区切られます。 Java では、カンマは区切り文字です。 C++ では、コンマは有効な式で使用できる演算子です。

for-each ループの構文

「for-each」形式は、for ループのより高度なバージョンです。 for-each ループは次の一般的な形式を取ります。

 for(type iter_variable: collection) statement-block 

「type」パラメータは反復変数のタイプを指定し、その後に反復変数が続きます。コレクション変数の要素は反復変数に渡されます。型は、コレクション変数の要素の型と一致する必要があります。 for ループの for-each 形式は、ループの繰り返しを最初から最後まで自動化し、値に順番にアクセスします。

休憩のためのJava

for ループで使用できるコレクションにはさまざまな種類があります。配列をコレクションとして使用して説明しましょう。

 public class Main { public static void main(String[] args) { int array[]={10, 20, 30, 40, 50, 60}; int add=0; for( int c: array) { System.out.println( 'value in c ' + c); add = add+c; } System.out.println('additon of array elements is ' +add); } } 

出力:

 value in c 10 value in c 20 value in c 30 value in c 40 value in c 50 value in c 60 additon of array elements is 210 

この場合、「c」は反復変数です。これは、array[] から値を 1 つずつ、配列内の最小のインデックスから最大のインデックスまで受け取ります。ループは、配列のすべての要素が検査されるまで繰り返されます。 「break」を使用すると、ループを途中で中断できます。一方、反復変数は読み取り専用変数であるため、反復変数を変更しても配列には影響しません。

While ループは次のように定義されます

while ループは、C++ と Java の両方で最も基本的なループです。 while ループの操作は C++ と Java で似ています。

構文

以下は while ループ宣言です。

 while ( condition) { statements; //body of loop } 

while ループは最初に条件をチェックし、次に while ループ内の条件が true になるまでステートメントを実行します。 while ループでは、条件には任意のブール式を使用できます。式がゼロ以外の値を返す場合、条件は true です。ゼロ値を返す場合、条件は false です。

Linuxでスクリプトを実行する方法

条件が true の場合、ループはそれ自体を繰り返します。条件が false の場合、制御はループの直後のコード行に渡されます。本体ループまたはステートメントは、空のステートメント、単一のステートメント、またはステートメントのブロックにすることができます。

while ループがどのように動作するかを見てみましょう。以下の例のコードは 1 から 10 までを出力します。

 public class Main { public static void main (String args[]) { int n=0; while(n<10) { n++; system.out.println('n=" +n); } } } &lt;/pre&gt; &lt;p&gt; &lt;strong&gt;Output:&lt;/strong&gt; &lt;/p&gt; &lt;pre&gt; n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 n=10 &lt;/pre&gt; &lt;p&gt;The initial value of " n' in this case is 0, which makes the condition while loop true. control then enters loop's body, where value of 'n' incremented accordance with first statement.< p> <p>The value of &apos;n&apos; is printed, then control returns to the condition in a while loop, where the value of &apos;n&apos; is now 1, satisfying the condition once more, and the body of the loop is executed once more. This continues until the condition becomes false, at which point the loop is terminated.</p> <p>The &apos;while&apos; loop, like the &apos;for&apos; loop, can initialise the control variable at the beginning of the loop, i.e. during condition checking.</p> <pre> //for example while((ch = getchar( ) ) != &apos;A&apos;) { System.out.println(&apos; The input alphabet &apos; +ch); } </pre> <p>At the top of the loop, the control variable &apos;ch&apos; is initialised, and the loop&apos;s condition is verified.</p> <h4>Note: If there is only one statement in the body of the loop, whether it is a for loop or a while loop, the curly braces are not required.</h4> <h3>In C, what is the difference between a for loop and a while?</h3> <table class="table"> <tr> <th>Parameters</th> <th>For Loop</th> <th>While Loop</th> </tr> <tr> <td> <strong>Declaration</strong> </td> <td>for(initialization ; condition ; iteration ) { <br> //body of &apos;for&apos; loop <br> }</td> <td>initialization <br>while ( condition ) { <br>statements; <br>//body of loop <br>}</td> </tr> <tr> <td> <strong>Format.</strong> </td> <td>At the top of the loop, initialization, condition checking, and iteration statements are written.</td> <td>At the top of the loop, only initialization and condition checking are performed.</td> </tr> <tr> <td> <strong>Use.</strong> </td> <td>The &apos;for&apos; loop was only used when the number of iterations was already known.</td> <td>When the number of iterations is unknown, the &apos;while&apos; loop is used.</td> </tr> <tr> <td> <strong>Condition.</strong> </td> <td>If the condition is not included in the &apos;for&apos; loop, the loop iterates indefinitely.</td> <td>If the condition is not included in the &apos;while&apos; loop, a compilation error occurs.</td> </tr> <tr> <td> <strong>Initialization</strong> </td> <td>The initialization is never repeated in a &apos;for&apos; loop.</td> <td>If initialization is performed during condition checking in a while loop, initialization is performed each time the loop iterates.</td> </tr> <tr> <td> <strong>Iteration assertion</strong> </td> <td>Because the iteration statement in the &apos;for&apos; loop is written at the top, it executes only after all statements in the loop have been executed.</td> <td>The iteration statement in a &apos;while&apos; loop can be written anywhere in the loop.</td> </tr> </table> <h2>The Key Differences Between for and while loop</h2> <ul> <li>Initialization, condition checking, and increment or decrement of iteration variables are all done explicitly in the loop syntax only. In contrast, in the while loop, we can only initialise and check the condition in the loop syntax.</li> <li>When we know the number of iterations that must occur in a loop execution, we use the for loop. On the other hand, if we do not know how many iterations must occur in a loop, we use a while loop.</li> <li>If you do not include a condition statement in the for loop, the loop will loop indefinitely. In contrast, failing to include a condition statement in the while loop will result in a compilation error.</li> <li>The initialization statement in the for loop syntax is only executed once at the beginning of the loop. If the while loop&apos;s syntax includes an initialization statement, the initialization statement will be executed each time the loop iterates.</li> <li>The iteration statement in the for loop will run after the body of the for loop. On the contrary, because the iteration statement can be written anywhere in the body of the while loop, there may be some statements that execute after the iteration statement in the body of the while loop is executed.</li> </ul> <h2>Conclusion</h2> <p>Loops are thus a collection of commands that must be used in a specific order. If the loop structure is incorrect, the programming will display the syntax error. Loops run to obtain a result or to satisfy a condition or set of conditions. It is the foundation of all programming languages.</p> <p>During execution, the loop structure asks a question and executes until the answer is satisfactory. The same question is asked again and again until the new statement is applied. The looping process continues indefinitely until the programme reaches a breakpoint. In the event that the breaking point is not reached, the programme will crash.</p> <p>The for and while loops are both conditional statements. A for loop is a single-line command that will be executed repeatedly. While loops can be single-lined or contain multiple commands for a single condition.</p> <p>Both the for loop and the while loop are important in computer languages for obtaining results. The condition is met if the command syntax is correct.</p> <p>Both the for loop and the while loop are iteration statements, but they have distinct characteristics. The for loop declares everything (initialization, condition, iteration) at the top of the loop body. In contrast, only initialization and condition are at the top of the body of the loop in a while loop, and iteration can be written anywhere in the body of the loop.</p> <hr></10)>

ループの先頭で、制御変数 'c​​h' が初期化され、ループの条件が検証されます。

文字列形式

注: for ループであっても while ループであっても、ループ本体にステートメントが 1 つだけある場合は、中括弧は必要ありません。

C では、for ループと while の違いは何ですか?

パラメーター For ループ While ループ
宣言 for(初期化 ; 条件 ; 反復) {
// 'for' ループの本体
}
初期化
while (条件) {
声明。
//ループ本体
}
フォーマット。 ループの先頭には、初期化、条件チェック、および反復ステートメントが書き込まれます。 ループの先頭では、初期化と条件チェックのみが実行されます。
使用。 「for」ループは、反復回数がすでにわかっている場合にのみ使用されました。 反復回数が不明な場合は、「while」ループが使用されます。
状態。 条件が「for」ループに含まれていない場合、ループは無限に繰り返されます。 条件が「while」ループに含まれていない場合、コンパイル エラーが発生します。
初期化 初期化が「for」ループ内で繰り返されることはありません。 while ループの条件チェック中に初期化が実行される場合、ループが反復されるたびに初期化が実行されます。
反復アサーション 「for」ループ内の反復ステートメントは先頭に記述されるため、ループ内のすべてのステートメントが実行された後でのみ実行されます。 「while」ループ内の反復ステートメントは、ループ内のどこにでも記述できます。

for ループと while ループの主な違い

  • 初期化、条件チェック、反復変数の増加または減少はすべて、ループ構文でのみ明示的に行われます。対照的に、while ループでは、ループ構文での初期化と条件の確認のみが可能です。
  • ループの実行で発生する必要がある反復回数がわかっている場合は、for ループを使用します。一方、ループ内で何回反復を行う必要があるかわからない場合は、while ループを使用します。
  • for ループに条件ステートメントを含めないと、ループは無限にループします。対照的に、while ループに条件ステートメントを含めないと、コンパイル エラーが発生します。
  • for ループ構文の初期化ステートメントは、ループの先頭で 1 回だけ実行されます。 while ループの構文に初期化ステートメントが含まれている場合、ループが反復されるたびに初期化ステートメントが実行されます。
  • for ループ内の反復ステートメントは、for ループ本体の後に実行されます。逆に、繰り返し文は while ループ本体のどこにでも記述できるため、while ループ本体の繰り返し文が実行された後に実行される文もあるかもしれません。

結論

したがって、ループは特定の順序で使用する必要があるコマンドのコレクションです。ループ構造が正しくない場合、プログラミングで構文エラーが表示されます。ループは、結果を取得するか、条件または一連の条件を満たすために実行されます。これはすべてのプログラミング言語の基礎です。

実行中、ループ構造は質問をし、満足のいく答えが得られるまで実行します。新しい声明が適用されるまで、同じ質問が何度も繰り返されます。ループ処理は、プログラムがブレークポイントに到達するまで無限に続きます。ブレークポイントに到達しない場合、プログラムはクラッシュします。

for ループと while ループはどちらも条件付きステートメントです。 for ループは、繰り返し実行される単一行のコマンドです。 While ループは単一行にすることも、単一の条件に対して複数のコマンドを含めることもできます。

for ループと while ループはどちらも、結果を取得するためにコンピューター言語で重要です。コマンド構文が正しい場合、条件は満たされます。

for ループと while ループは両方とも反復ステートメントですが、それぞれに異なる特性があります。 for ループは、ループ本体の先頭ですべて (初期化、条件、反復) を宣言します。対照的に、while ループでは初期化と条件だけがループ本体の先頭にあり、反復はループ本体のどこにでも書くことができます。