logo

för loop i C

De för loop i C-språk används för att upprepa påståendena eller en del av programmet flera gånger. Det används ofta för att gå igenom datastrukturer som arrayen och länkad lista.

Syntax för för loop i C

Syntaxen för for loop i c-språk ges nedan:

jämför sträng java
 for(Expression 1; Expression 2; Expression 3){ //code to be executed } 

Flödesschema över för loop i C

för loop i c-språk flödesschema

C för loop Exempel

Låt oss se det enkla programmet för for loop som skriver ut tabell med 1.

 #include int main(){ int i=0; for(i=1;i<=10;i++){ printf('%d 
',i); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <h3>C Program: Print table for the given number using C for loop</h3> <pre> #include int main(){ int i=1,number=0; printf(&apos;Enter a number: &apos;); scanf(&apos;%d&apos;,&amp;number); for(i=1;i<=10;i++){ printf('%d 
',(number*i)); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number: 2 2 4 6 8 10 12 14 16 18 20 </pre> <pre> Enter a number: 1000 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 </pre> <h3>Properties of Expression 1</h3> <ul> <li>The expression represents the initialization of the loop variable.</li> <li>We can initialize more than one variable in Expression 1.</li> <li>Expression 1 is optional.</li> <li>In C, we can not declare the variables in Expression 1. However, It can be an exception in some compilers.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include int main() { int a,b,c; for(a=0,b=12,c=23;a<2;a++) { printf('%d ',a+b+c); } < pre> <p> <strong>Output</strong> </p> <pre> 35 36 </pre> <p> <strong>Example 2</strong> </p> <pre> #include int main() { int i=1; for(;i<5;i++) { printf('%d ',i); } < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 </pre> <h3>Properties of Expression 2</h3> <ul> <li>Expression 2 is a conditional expression. It checks for a specific condition to be satisfied. If it is not, the loop is terminated.</li> <li>Expression 2 can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.</li> <li>Expression 2 is optional.</li> <li>Expression 2 can perform the task of expression 1 and expression 3. That is, we can initialize the variable as well as update the loop variable in expression 2 itself.</li> <li>We can pass zero or non-zero value in expression 2. However, in C, any non-zero value is true, and zero is false by default.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include int main() { int i; for(i=0;i<=4;i++) { printf('%d ',i); } < pre> <p> <strong>output</strong> </p> <pre> 0 1 2 3 4 </pre> <p> <strong>Example 2</strong> </p> <pre> #include int main() { int i,j,k; for(i=0,j=0,k=0;i<4,k<8,j<10;i++) { printf('%d %d %d
',i,j,k); j+="2;" k+="3;" } < pre> <p> <strong>Output</strong> </p> <pre> 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12 </pre> <p> <strong>Example 3</strong> </p> <pre> #include int main() { int i; for(i=0;;i++) { printf(&apos;%d&apos;,i); } } </pre> <p> <strong>Output</strong> </p> <pre> infinite loop </pre> <h4>Properties of Expression 3 <ul> <li>Expression 3 is used to update the loop variable.</li> <li>We can update more than one variable at the same time.</li> <li>Expression 3 is optional.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include void main () { int i=0,j=2; for(i = 0;i<5;i++,j=j+2) { printf('%d %d
',i,j); } < pre> <p> <strong>Output</strong> <pre> 0 2 1 4 2 6 3 8 4 10 </pre> </p><h3>Loop body</h3> <p>The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don&apos;t need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example.</p> <pre> #include void main () { int i; for(i=0;i<10;i++) { int i="20;" printf('%d ',i); } < pre> <p> <strong>Output</strong> </p> <pre> 20 20 20 20 20 20 20 20 20 20 </pre> <h3>Infinitive for loop in C</h3> <p>To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.</p> <pre> #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)></pre></5;i++,j=j+2)></pre></h4></4,k<8,j<10;i++)></pre></=4;i++)></pre></5;i++)></pre></2;a++)></pre></=10;i++){></pre></=10;i++){>

C Program: Skriv ut tabell för det givna numret med C för loop

 #include int main(){ int i=1,number=0; printf(&apos;Enter a number: &apos;); scanf(&apos;%d&apos;,&amp;number); for(i=1;i<=10;i++){ printf(\'%d 
\',(number*i)); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number: 2 2 4 6 8 10 12 14 16 18 20 </pre> <pre> Enter a number: 1000 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 </pre> <h3>Properties of Expression 1</h3> <ul> <li>The expression represents the initialization of the loop variable.</li> <li>We can initialize more than one variable in Expression 1.</li> <li>Expression 1 is optional.</li> <li>In C, we can not declare the variables in Expression 1. However, It can be an exception in some compilers.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include int main() { int a,b,c; for(a=0,b=12,c=23;a<2;a++) { printf(\'%d \',a+b+c); } < pre> <p> <strong>Output</strong> </p> <pre> 35 36 </pre> <p> <strong>Example 2</strong> </p> <pre> #include int main() { int i=1; for(;i<5;i++) { printf(\'%d \',i); } < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 </pre> <h3>Properties of Expression 2</h3> <ul> <li>Expression 2 is a conditional expression. It checks for a specific condition to be satisfied. If it is not, the loop is terminated.</li> <li>Expression 2 can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.</li> <li>Expression 2 is optional.</li> <li>Expression 2 can perform the task of expression 1 and expression 3. That is, we can initialize the variable as well as update the loop variable in expression 2 itself.</li> <li>We can pass zero or non-zero value in expression 2. However, in C, any non-zero value is true, and zero is false by default.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include int main() { int i; for(i=0;i<=4;i++) { printf(\'%d \',i); } < pre> <p> <strong>output</strong> </p> <pre> 0 1 2 3 4 </pre> <p> <strong>Example 2</strong> </p> <pre> #include int main() { int i,j,k; for(i=0,j=0,k=0;i<4,k<8,j<10;i++) { printf(\'%d %d %d
\',i,j,k); j+="2;" k+="3;" } < pre> <p> <strong>Output</strong> </p> <pre> 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12 </pre> <p> <strong>Example 3</strong> </p> <pre> #include int main() { int i; for(i=0;;i++) { printf(&apos;%d&apos;,i); } } </pre> <p> <strong>Output</strong> </p> <pre> infinite loop </pre> <h4>Properties of Expression 3 <ul> <li>Expression 3 is used to update the loop variable.</li> <li>We can update more than one variable at the same time.</li> <li>Expression 3 is optional.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include void main () { int i=0,j=2; for(i = 0;i<5;i++,j=j+2) { printf(\'%d %d
\',i,j); } < pre> <p> <strong>Output</strong> <pre> 0 2 1 4 2 6 3 8 4 10 </pre> </p><h3>Loop body</h3> <p>The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don&apos;t need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example.</p> <pre> #include void main () { int i; for(i=0;i<10;i++) { int i="20;" printf(\'%d \',i); } < pre> <p> <strong>Output</strong> </p> <pre> 20 20 20 20 20 20 20 20 20 20 </pre> <h3>Infinitive for loop in C</h3> <p>To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.</p> <pre> #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)></pre></5;i++,j=j+2)></pre></h4></4,k<8,j<10;i++)></pre></=4;i++)></pre></5;i++)></pre></2;a++)></pre></=10;i++){>
 Enter a number: 1000 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 

Egenskaper för uttryck 1

  • Uttrycket representerar initieringen av loopvariabeln.
  • Vi kan initiera mer än en variabel i uttryck 1.
  • Uttryck 1 är valfritt.
  • I C kan vi inte deklarera variablerna i uttryck 1. Det kan dock vara ett undantag i vissa kompilatorer.

Exempel 1

 #include int main() { int a,b,c; for(a=0,b=12,c=23;a<2;a++) { printf(\'%d \',a+b+c); } < pre> <p> <strong>Output</strong> </p> <pre> 35 36 </pre> <p> <strong>Example 2</strong> </p> <pre> #include int main() { int i=1; for(;i<5;i++) { printf(\'%d \',i); } < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 </pre> <h3>Properties of Expression 2</h3> <ul> <li>Expression 2 is a conditional expression. It checks for a specific condition to be satisfied. If it is not, the loop is terminated.</li> <li>Expression 2 can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.</li> <li>Expression 2 is optional.</li> <li>Expression 2 can perform the task of expression 1 and expression 3. That is, we can initialize the variable as well as update the loop variable in expression 2 itself.</li> <li>We can pass zero or non-zero value in expression 2. However, in C, any non-zero value is true, and zero is false by default.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include int main() { int i; for(i=0;i<=4;i++) { printf(\'%d \',i); } < pre> <p> <strong>output</strong> </p> <pre> 0 1 2 3 4 </pre> <p> <strong>Example 2</strong> </p> <pre> #include int main() { int i,j,k; for(i=0,j=0,k=0;i<4,k<8,j<10;i++) { printf(\'%d %d %d
\',i,j,k); j+="2;" k+="3;" } < pre> <p> <strong>Output</strong> </p> <pre> 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12 </pre> <p> <strong>Example 3</strong> </p> <pre> #include int main() { int i; for(i=0;;i++) { printf(&apos;%d&apos;,i); } } </pre> <p> <strong>Output</strong> </p> <pre> infinite loop </pre> <h4>Properties of Expression 3 <ul> <li>Expression 3 is used to update the loop variable.</li> <li>We can update more than one variable at the same time.</li> <li>Expression 3 is optional.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include void main () { int i=0,j=2; for(i = 0;i<5;i++,j=j+2) { printf(\'%d %d
\',i,j); } < pre> <p> <strong>Output</strong> <pre> 0 2 1 4 2 6 3 8 4 10 </pre> </p><h3>Loop body</h3> <p>The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don&apos;t need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example.</p> <pre> #include void main () { int i; for(i=0;i<10;i++) { int i="20;" printf(\'%d \',i); } < pre> <p> <strong>Output</strong> </p> <pre> 20 20 20 20 20 20 20 20 20 20 </pre> <h3>Infinitive for loop in C</h3> <p>To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.</p> <pre> #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)></pre></5;i++,j=j+2)></pre></h4></4,k<8,j<10;i++)></pre></=4;i++)></pre></5;i++)></pre></2;a++)>

Exempel 2

 #include int main() { int i=1; for(;i<5;i++) { printf(\'%d \',i); } < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 </pre> <h3>Properties of Expression 2</h3> <ul> <li>Expression 2 is a conditional expression. It checks for a specific condition to be satisfied. If it is not, the loop is terminated.</li> <li>Expression 2 can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.</li> <li>Expression 2 is optional.</li> <li>Expression 2 can perform the task of expression 1 and expression 3. That is, we can initialize the variable as well as update the loop variable in expression 2 itself.</li> <li>We can pass zero or non-zero value in expression 2. However, in C, any non-zero value is true, and zero is false by default.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include int main() { int i; for(i=0;i<=4;i++) { printf(\'%d \',i); } < pre> <p> <strong>output</strong> </p> <pre> 0 1 2 3 4 </pre> <p> <strong>Example 2</strong> </p> <pre> #include int main() { int i,j,k; for(i=0,j=0,k=0;i<4,k<8,j<10;i++) { printf(\'%d %d %d
\',i,j,k); j+="2;" k+="3;" } < pre> <p> <strong>Output</strong> </p> <pre> 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12 </pre> <p> <strong>Example 3</strong> </p> <pre> #include int main() { int i; for(i=0;;i++) { printf(&apos;%d&apos;,i); } } </pre> <p> <strong>Output</strong> </p> <pre> infinite loop </pre> <h4>Properties of Expression 3 <ul> <li>Expression 3 is used to update the loop variable.</li> <li>We can update more than one variable at the same time.</li> <li>Expression 3 is optional.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include void main () { int i=0,j=2; for(i = 0;i<5;i++,j=j+2) { printf(\'%d %d
\',i,j); } < pre> <p> <strong>Output</strong> <pre> 0 2 1 4 2 6 3 8 4 10 </pre> </p><h3>Loop body</h3> <p>The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don&apos;t need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example.</p> <pre> #include void main () { int i; for(i=0;i<10;i++) { int i="20;" printf(\'%d \',i); } < pre> <p> <strong>Output</strong> </p> <pre> 20 20 20 20 20 20 20 20 20 20 </pre> <h3>Infinitive for loop in C</h3> <p>To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.</p> <pre> #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)></pre></5;i++,j=j+2)></pre></h4></4,k<8,j<10;i++)></pre></=4;i++)></pre></5;i++)>

Egenskaper för uttryck 2

  • Uttryck 2 är ett villkorligt uttryck. Den kontrollerar att ett specifikt villkor är uppfyllt. Om det inte är det, avslutas slingan.
  • Uttryck 2 kan ha mer än ett villkor. Slingan kommer dock att upprepas tills det sista villkoret blir falskt. Övriga förhållanden kommer att behandlas som uttalanden.
  • Uttryck 2 är valfritt.
  • Uttryck 2 kan utföra uppgiften för uttryck 1 och uttryck 3. Det vill säga vi kan initiera variabeln samt uppdatera loopvariabeln i själva uttryck 2.
  • Vi kan skicka noll eller icke-noll värde i uttryck 2. Men i C är alla icke-noll värden sant, och noll är falskt som standard.

Exempel 1

år in i kvartalen
 #include int main() { int i; for(i=0;i<=4;i++) { printf(\'%d \',i); } < pre> <p> <strong>output</strong> </p> <pre> 0 1 2 3 4 </pre> <p> <strong>Example 2</strong> </p> <pre> #include int main() { int i,j,k; for(i=0,j=0,k=0;i<4,k<8,j<10;i++) { printf(\'%d %d %d
\',i,j,k); j+="2;" k+="3;" } < pre> <p> <strong>Output</strong> </p> <pre> 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12 </pre> <p> <strong>Example 3</strong> </p> <pre> #include int main() { int i; for(i=0;;i++) { printf(&apos;%d&apos;,i); } } </pre> <p> <strong>Output</strong> </p> <pre> infinite loop </pre> <h4>Properties of Expression 3 <ul> <li>Expression 3 is used to update the loop variable.</li> <li>We can update more than one variable at the same time.</li> <li>Expression 3 is optional.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include void main () { int i=0,j=2; for(i = 0;i<5;i++,j=j+2) { printf(\'%d %d
\',i,j); } < pre> <p> <strong>Output</strong> <pre> 0 2 1 4 2 6 3 8 4 10 </pre> </p><h3>Loop body</h3> <p>The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don&apos;t need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example.</p> <pre> #include void main () { int i; for(i=0;i<10;i++) { int i="20;" printf(\'%d \',i); } < pre> <p> <strong>Output</strong> </p> <pre> 20 20 20 20 20 20 20 20 20 20 </pre> <h3>Infinitive for loop in C</h3> <p>To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.</p> <pre> #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)></pre></5;i++,j=j+2)></pre></h4></4,k<8,j<10;i++)></pre></=4;i++)>

Exempel 2

 #include int main() { int i,j,k; for(i=0,j=0,k=0;i<4,k<8,j<10;i++) { printf(\\'%d %d %d
\\',i,j,k); j+="2;" k+="3;" } < pre> <p> <strong>Output</strong> </p> <pre> 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12 </pre> <p> <strong>Example 3</strong> </p> <pre> #include int main() { int i; for(i=0;;i++) { printf(&apos;%d&apos;,i); } } </pre> <p> <strong>Output</strong> </p> <pre> infinite loop </pre> <h4>Properties of Expression 3 <ul> <li>Expression 3 is used to update the loop variable.</li> <li>We can update more than one variable at the same time.</li> <li>Expression 3 is optional.</li> </ul> <p> <strong>Example 1</strong> </p> <pre> #include void main () { int i=0,j=2; for(i = 0;i<5;i++,j=j+2) { printf(\\'%d %d
\\',i,j); } < pre> <p> <strong>Output</strong> <pre> 0 2 1 4 2 6 3 8 4 10 </pre> </p><h3>Loop body</h3> <p>The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don&apos;t need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example.</p> <pre> #include void main () { int i; for(i=0;i<10;i++) { int i="20;" printf(\\'%d \\',i); } < pre> <p> <strong>Output</strong> </p> <pre> 20 20 20 20 20 20 20 20 20 20 </pre> <h3>Infinitive for loop in C</h3> <p>To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.</p> <pre> #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)></pre></5;i++,j=j+2)></pre></h4></4,k<8,j<10;i++)>

Exempel 3

 #include int main() { int i; for(i=0;;i++) { printf(&apos;%d&apos;,i); } } 

Produktion

 infinite loop 

Egenskaper för uttryck 3
  • Uttryck 3 används för att uppdatera loopvariabeln.
  • Vi kan uppdatera mer än en variabel samtidigt.
  • Uttryck 3 är valfritt.

Exempel 1

byt ut en färg i gimp
 #include void main () { int i=0,j=2; for(i = 0;i<5;i++,j=j+2) { printf(\\'%d %d
\\',i,j); } < pre> <p> <strong>Output</strong> <pre> 0 2 1 4 2 6 3 8 4 10 </pre> </p><h3>Loop body</h3> <p>The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don&apos;t need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example.</p> <pre> #include void main () { int i; for(i=0;i<10;i++) { int i="20;" printf(\\'%d \\',i); } < pre> <p> <strong>Output</strong> </p> <pre> 20 20 20 20 20 20 20 20 20 20 </pre> <h3>Infinitive for loop in C</h3> <p>To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.</p> <pre> #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)></pre></5;i++,j=j+2)>

Slingkropp

Klammerna {} används för att definiera slingans omfattning. Men om slingan bara innehåller ett påstående behöver vi inte använda klammerparenteser. En slinga utan kropp är möjlig. Klammerna fungerar som en blockseparator, dvs. värdevariabeln som deklareras inuti för loop är endast giltig för det blocket och inte utanför. Betrakta följande exempel.

 #include void main () { int i; for(i=0;i<10;i++) { int i="20;" printf(\\'%d \\',i); } < pre> <p> <strong>Output</strong> </p> <pre> 20 20 20 20 20 20 20 20 20 20 </pre> <h3>Infinitive for loop in C</h3> <p>To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.</p> <pre> #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)>

Infinitiv för loop i C

För att göra en for-loop oändlig behöver vi inte ge något uttryck i syntaxen. Istället för det måste vi tillhandahålla två semikolon för att validera syntaxen för for-loopen. Detta kommer att fungera som en oändlig för loop.

 #include void main () { for(;;) { printf(&apos;welcome to javatpoint&apos;); } } 

Om du kör det här programmet kommer du att se ovanstående uttalande oändligt många gånger.