logo

for цикъл в C

The for цикъл на език C се използва за повторение на операторите или на част от програмата няколко пъти. Често се използва за преминаване през структурите от данни като масив и свързан списък.

Синтаксис на for цикъл в C

Синтаксисът на for цикъл в език c е даден по-долу:

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

Блок-схема на for цикъл в C

for цикъл в блок-схема на език c

C for примери за цикъл

Нека видим простата програма на for цикъл, която отпечатва таблица от 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 програма: Отпечатайте таблица за даденото число с помощта на 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 

Свойства на израз 1

  • Изразът представлява инициализацията на променливата на цикъла.
  • Можем да инициализираме повече от една променлива в израз 1.
  • Израз 1 не е задължителен.
  • В C не можем да декларираме променливите в Expression 1. Въпреки това, това може да бъде изключение в някои компилатори.

Пример 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++)>

Пример 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++)>

Свойства на израз 2

  • Израз 2 е условен израз. Той проверява дали определено условие е изпълнено. Ако не е, цикълът се прекратява.
  • Израз 2 може да има повече от едно условие. Въпреки това, цикълът ще се повтаря, докато последното условие стане невярно. Други условия ще се третират като изявления.
  • Израз 2 не е задължителен.
  • Израз 2 може да изпълни задачата на израз 1 и израз 3. Това означава, че можем да инициализираме променливата, както и да актуализираме променливата на цикъла в самия израз 2.
  • Можем да предадем нулева или ненулева стойност в израз 2. В C обаче всяка ненулева стойност е вярна, а нулата е невярна по подразбиране.

Пример 1

променливи на nginx
 #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++)>

Пример 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++)>

Пример 3

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

Изход

 infinite loop 

Свойства на израз 3
  • Израз 3 се използва за актуализиране на променливата на цикъла.
  • Можем да актуализираме повече от една променлива едновременно.
  • Израз 3 не е задължителен.

Пример 1

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

Тяло на примка

Скобите {} се използват за определяне на обхвата на цикъла. Ако обаче цикълът съдържа само един израз, тогава не е необходимо да използваме фигурни скоби. Възможен е цикъл без тяло. Скобите работят като разделител на блокове, т.е. стойностната променлива, декларирана вътре в цикъла for, е валидна само за този блок, а не извън него. Помислете за следния пример.

java чете csv файл
 #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++)>

Инфинитив за цикъл в C

За да направим for цикъл безкраен, не е нужно да даваме израз в синтаксиса. Вместо това трябва да предоставим две точки и запетая, за да потвърдим синтаксиса на for цикъла. Това ще работи като безкраен for цикъл.

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

Ако стартирате тази програма, ще видите горния оператор безкрайно много пъти.