logo

for zanka v C

The zanka for v jeziku C se uporablja za večkratno ponavljanje stavkov ali dela programa. Pogosto se uporablja za prečkanje podatkovnih struktur, kot sta matrika in povezani seznam.

Sintaksa zanke for v C

Sintaksa zanke for v jeziku c je podana spodaj:

java izbor sort
 for(Expression 1; Expression 2; Expression 3){ //code to be executed } 

Diagram poteka zanke for v C

zanka for v diagramu poteka jezika c

Primeri zanke C for

Oglejmo si preprost program zanke for, ki natisne tabelo 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++){>

Program C: Natisnite tabelo za dano število z uporabo zanke C for

 #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 

Lastnosti izraza 1

  • Izraz predstavlja inicializacijo spremenljivke zanke.
  • V izrazu 1 lahko inicializiramo več kot eno spremenljivko.
  • Izraz 1 ni obvezen.
  • V C ne moremo deklarirati spremenljivk v izrazu 1. Vendar pa je to lahko izjema v nekaterih prevajalnikih.

Primer 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++)>

Primer 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++)>

Lastnosti izraza 2

  • Izraz 2 je pogojni izraz. Preveri, ali je določen pogoj izpolnjen. Če ni, se zanka prekine.
  • Izraz 2 ima lahko več kot en pogoj. Vendar se bo zanka ponavljala, dokler zadnji pogoj ne postane napačen. Drugi pogoji bodo obravnavani kot izjave.
  • Izraz 2 ni obvezen.
  • Izraz 2 lahko izvede nalogo izraza 1 in izraza 3. To pomeni, da lahko inicializiramo spremenljivko kot tudi posodobimo spremenljivko zanke v samem izrazu 2.
  • V izrazu 2 lahko posredujemo ničelno ali neničelno vrednost. Vendar je v C katera koli neničelna vrednost resnična, ničla pa je privzeto napačna.

Primer 1

zimsko narečje
 #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++)>

Primer 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++)>

Primer 3

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

Izhod

 infinite loop 

Lastnosti izraza 3
  • Izraz 3 se uporablja za posodobitev spremenljivke zanke.
  • Posodobimo lahko več kot eno spremenljivko hkrati.
  • Izraz 3 ni obvezen.

Primer 1

niz predolg
 #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)>

Telo zanke

Oklepaji {} se uporabljajo za določanje obsega zanke. Če pa zanka vsebuje samo en stavek, nam oklepaji niso potrebni. Možna je zanka brez telesa. Oklepaji delujejo kot ločilo blokov, tj. spremenljivka vrednosti, deklarirana znotraj zanke for, velja samo za ta blok in ne zunaj. Razmislite o naslednjem primeru.

 #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 za zanko v C

Da bi bila zanka for neskončna, nam v sintaksi ni treba podati nobenega izraza. Namesto tega moramo zagotoviti dve podpičji za potrditev sintakse zanke for. To bo delovalo kot neskončna zanka for.

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

Če zaženete ta program, boste zgornji stavek videli neskončno krat.