그만큼 C 언어의 for 루프 명령문이나 프로그램의 일부를 여러 번 반복하는 데 사용됩니다. 배열이나 연결리스트와 같은 데이터 구조를 순회하는 데 자주 사용됩니다.
C의 for 루프 구문
C 언어의 for 루프 구문은 다음과 같습니다.
문자열을 정수로 변환하는 방법 java
for(Expression 1; Expression 2; Expression 3){ //code to be executed }
C의 for 루프 흐름도
C for 루프 예
1의 테이블을 인쇄하는 간단한 for 루프 프로그램을 살펴보겠습니다.
#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('Enter a number: '); scanf('%d',&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('%d',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'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('welcome to javatpoint'); } } </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('Enter a number: '); scanf('%d',&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('%d',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'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('welcome to javatpoint'); } } </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에서는 식 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('%d',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'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('welcome to javatpoint'); } } </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('%d',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'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('welcome to javatpoint'); } } </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에는 두 개 이상의 조건이 있을 수 있습니다. 그러나 루프는 마지막 조건이 false가 될 때까지 반복됩니다. 다른 조건은 명령문으로 처리됩니다.
- 식 2는 선택 사항입니다.
- 표현식 2는 표현식 1과 표현식 3의 작업을 수행할 수 있습니다. 즉, 표현식 2 자체에서 변수를 초기화할 뿐만 아니라 루프 변수를 업데이트할 수도 있습니다.
- 표현식 2에서는 0 또는 0이 아닌 값을 전달할 수 있습니다. 그러나 C에서는 0이 아닌 모든 값이 true이고 0은 기본적으로 false입니다.
실시예 1
자바 메인 메소드
#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('%d',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'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('welcome to javatpoint'); } } </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('%d',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'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('welcome to javatpoint'); } } </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('%d',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'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('welcome to javatpoint'); } } </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 루프 내부에 선언된 값 변수는 해당 블록에 대해서만 유효하고 외부에서는 유효하지 않습니다. 다음 예를 고려하십시오.
#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('welcome to javatpoint'); } } </pre> <p>If you run this program, you will see above statement infinite times.</p> <hr></10;i++)>
C의 부정사 for 루프
for 루프를 무한하게 만들려면 구문에 표현식을 제공할 필요가 없습니다. 그 대신 for 루프의 구문을 검증하기 위해 두 개의 세미콜론을 제공해야 합니다. 이것은 무한 for 루프로 작동합니다.
#include void main () { for(;;) { printf('welcome to javatpoint'); } }
이 프로그램을 실행하면 위의 문장을 무한히 보게 될 것입니다.
10;i++)>5;i++,j=j+2)>4,k<8,j<10;i++)>=4;i++)>5;i++)>2;a++)>=10;i++){>=10;i++){>