program tip

";"의 목적은 무엇입니까?

radiobox 2020. 10. 13. 07:39
반응형

";"의 목적은 무엇입니까? for 루프의 끝에서?


다음 코드를 찾았습니다.

int func_prim (int zahl) {
    int count;
    if (zahl < 0)
        return -1;

    for (count = 2; zahl % count != 0 && zahl >= count; count++);
    if (count == zahl)
        return 1;
    return 0;
}

기능의 요점은 숫자가 소수인지 아닌지를 확인하는 것입니다.

for-loop가 왜 ;끝에 있는지 이해하지 못합니다 .

                                                            v
for (count = 2; zahl % count != 0 && zahl >= count; count++);

그것 없이는 코드가 제대로 작동하지 않습니다.

설명은 무엇입니까?


다음과 같은 의미입니다.

for(count = 2; zahl % count != 0 && zahl >= count; count++)
{
}

for 루프에는 for키워드, 세미콜론으로 구분 된 세 개의 선택적 표현식이 포함 된 괄호, 루프의 각 반복에서 실행되는 본문이 있습니다.

예제에서 for 루프의 목표는 뒤에 나오는 if 문에서 count비교되는 값을 설정 하는 것 zahl입니다. 이것은 세미콜론으로 구분 된 표현식에서 이루어 지므로 루프 본문은 아무 작업도 수행 할 필요가 없습니다.

루프는 아무것도 할 필요가 없기 때문에 빈 문을 본문으로 사용합니다.

는 IF ;말은 생략하지 않았다 및 루프에 대한 자체가 루프의 몸을 될 것입니다 후 다른 변경 사항은 다음의 경우 문을 하였다. (이것은 의도 한 것이 아니며 당신이 관찰 한 바와 같이 프로그램을 중단시킬 것입니다.)

그러나 루프 본문을 ;같은 줄에 단일로 구성 하는 것이 빈 루프 본문을 작성하는 유일한 방법은 아니며 그렇게하는 가장 현명한 방법 일 수도 있습니다. 그것은 완벽하게 잘 작동하지만 문제는 다른 독자들과 아마도 같은 프로그래머가 나중에 프로젝트로 돌아와서 그것이 실제로 오류인지 궁금 할 수도 있다는 것입니다. 결국 하나는 C 스타일 언어로 코딩 할 때 줄 끝에 세미콜론을 자주 입력하므로 속하지 않는 추가 항목을 입력하기 쉽습니다.

와 한 줄 루프 경우 다른 문제는 코드에서, 그이다 ;몸이 선택한 스타일로, 어려운 인식 누군가가 실제로 때 퍼팅의 실수를 만든 ;사람이 속하지 않는 경우입니다.

따라서 다음 대안이 더 바람직 할 수 있습니다.

  • 퍼팅 ;- 다음 줄에, 들여 쓰기, SH1에서 알 수 있듯이
  • 루프 본문을 { }빈 문이 아닌 빈 블록으로 작성
  • 루프 본문을 continue;문으로 만들어 루프 가 다음 반복으로 이동하도록합니다 (루프 본문이 비어있을 때 발생하는 것과 동일 함) -sh1이 제안한대로

for 루프 끝에있는 세미콜론은 본문이 없음을 의미합니다. 이 세미콜론이 없으면 C는 if문이 for 루프의 본문 이라고 생각합니다 .


구문 for루프 ( 반복 문 )입니다

for ( clause-1 ; expression-2 ; expression-3 ) statement 

statementnull 문 ( ;) 일 수 있습니다 . C11 6.8.3 말한다

세미콜론으로 만 구성된 null 문은 작업을 수행하지 않습니다.

5 항에서는 예를 제공합니다.

프로그램 조각에서

char *s;
/* ... */
while (*s++ != '\0')
    ;

null 문은 반복 문에 빈 루프 본문을 제공하는 데 사용됩니다 .

같은 일이 일어나고 있습니다

for (count = 2; zahl % count != 0 && zahl >= count; count++);

;for문에 빈 루프 본문을 제공하는 데 사용됩니다 . ;문이 없으면 for루프 옆에있는 것으로 간주되어 실행됩니다.


다른 우수한 답변이 이미 말한 것 외에도 지적하고 싶습니다.

for(count=2; zahl % count != 0 && zahl >= count; count++);

(즉, for"카운터"를 증가시키는 데 사용되는 빈 문이 있는 루프)는 다음과 같습니다.

count=2;
while(zahl % count != 0  && zahl >= count)
{
    count++;
}

이는 나열된 대안 중 일부보다 코드의 목적을 더 명확하게 만듭니다. 제시된 사례에서와 같이 주석이 없으면 빈 문이있는 루프는 코드를 관리하거나 사용해야하는 다른 프로그래머를 혼동 할 수 있습니다 (그대로 여기에 OP가있는 경우).

컨텍스트는 문의 실제 범위를 식별하는 데 도움이 될 수 있지만 for빈 문이있는 while루프와 문이 있는 루프 사이 에서 후자는 범위를 이해하는 데 필요한 작업이 적습니다.


;애프터 for것을 루프 단순히 수단 for루프가 더 카운터를 증가보다 아무것도하지 않습니다 count.


for성명 :

The for statement is a loop statement whose structure allows easy variable initialization, expression testing, and variable modification. It is very convenient for making counter-controlled loops. Here is the general form of the for statement:

 for (initialize; test; step)
   statement

[...]

Null Statement:

The null statement is merely a semicolon alone.

 ;

A null statement does not do anything. It does not store a value anywhere. It does not cause time to pass during the execution of your program.

Most often, a null statement is used as the body of a loop statement, or as one or more of the expressions in a for statement. Here is an example of a for statement that uses the null statement as the body of the loop (and also calculates the integer square root of n, just for fun):

 for (i = 1; i*i < n; i++)
   ;

Here is another example that uses the null statement as the body of a for loop and also produces output:

 for (x = 1; x <= 5; printf ("x is now %d\n", x), x++)
   ;

A null statement is also sometimes used to follow a label that would otherwise be the last thing in a block.


In your case, the ; is the Null Statement of the for Statement:

int func_prim (int zahl) {
  int count;
  if (zahl < 0)
    return -1;

  for (count = 2; zahl % count != 0 && zahl >= count; count++)
    ;
  if (count == zahl)
    return 1;
  return 0;
}

Without it, the if becomes the for statement:

int func_prim (int zahl) {
  int count;
  if (zahl < 0)
    return -1;

  for (count = 2; zahl % count != 0 && zahl >= count; count++)
    if (count == zahl)
      return 1;
  return 0;
}

Therefore, behaving differently.


The for loop is there just to increase the value of count.


a for loop will (normally) have a body,

where the body is enclosed in braces { }

However, for a single statement body, the braces are optional.

; is an empty statement.

Combining the above it becomes obvious that the for loop executes until the condition becomes false.


The for loop is basically looping through all the numbers that are less than or equal to zahl but greater than 2 and storing it in the variable count. As it loops through all these numbers it is checking to see if zahl is divisible by count. If zahl is divisible by count, the loop is stopped. Otherwise, the loop is stopped when count equals zahl.

The if statement after the for loop checks to see if count is equal to zahl. If it is, then that must mean that the loop went through all the numbers less than zahl and greater than 2. This means that zahl is divisible by all the numbers less than itself and greater 2, which makes zahl prime.

참고URL : https://stackoverflow.com/questions/29836119/what-is-the-purpose-of-at-the-end-of-for-loop

반응형