6

I am reading a code of my friend an I see this:

#include <stdio.h>
#include <conio.h>

void main()
{
    char string1[125], string2 [10];
    int i, j;
    printf("\nstring 1: ");
    gets(string1);
    printf("\nNstring2 : ");
    gets(string2);
    i = 0;
    while (string1[i] != 0)
    {
        j = 0;
        while (string1[i++] == string2[j++] &&string1[i-1] != 0 && string2[j-1] != 0)
            ;//i dont know what it mean and why we can put ;after while loop
        if (string1[i-1] != 0 && string2[j-1] == 0)
        printf("\nfound at position %d", i-j);
    }
    getch();
}

why we can put ; after while loop , anyone can help?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
user3376115
  • 59
  • 1
  • 1
  • 6
  • 3
    that's usually called a 'semicolon'. not sure if english is your first language, so just fyi. – Corley Brigman Mar 03 '14 at 19:14
  • 1
    In this case the `while` condition does all the "work", so no loop body is needed. – Hot Licks Mar 03 '14 at 19:19
  • But it is a good point: Reflexively (and incorrectly) adding a `;` after a `for` or `while` or `if` is frequently the cause of errors in newbie programs (and the occasional program by not-so-newbies). So seeing the `;` there should be a red flag -- double check that it really belongs if you see it. – Hot Licks Mar 03 '14 at 19:22
  • You can also put `;` on a line on its own. It doesn't have to be in a loop. Alternatively you can write `{ }` which is an empty block and if used as a block it is equivalent to a null statement. – Brandin Mar 03 '14 at 19:28
  • It's good practice to put such an "empty statement" ; on its own line, to emphasize that it's deliberate. It's even better to put in a comment explaining what's going on and why you have an empty statement. `/* without running off the end of string1 or string2, advance i and j to the first character not matching between string1 an string2 */` or something like that -- without other comments, I'm not sure what the author is trying to do. – Phil Perry Mar 11 '14 at 13:54

4 Answers4

13

The ; is just a null statement, it is a no op but it it the body of the while loop. From the draft C99 standard section 6.8.3 Expression and null statements:

A null statement (consisting of just a semicolon) performs no operations.

and a while statement is defined as follows from section 6.8.5 Iteration statements:

while ( expression ) statement

So in this case the statement of the while loop is ;.

The main effect of the while loop is here:

string1[i++] == string2[j++]
        ^^^             ^^^

So each iteration of the loop increments i and j until the whole condition:

string1[i++] == string2[j++] &&string1[i-1] != 0 && string2[j-1] != 0

evaluates to false.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 1
    You might want to point out that the `while` still has an exit condition, because of the post-incremented variables. – Robert Harvey Mar 03 '14 at 19:17
  • if it is null, why do we have to use it, can i delete that line ? – user3376115 Mar 03 '14 at 19:23
  • @user3376115 no we can not delete it b/c then the body of the while loop would be the subsequent if statement. Which would have a completely different effect. – Shafik Yaghmour Mar 03 '14 at 19:24
  • It's best to document your intent and include the `continue` statement. – Fiddling Bits Mar 03 '14 at 19:24
  • user, a while () loop _always_ needs a statement following it. Even if you don't want one, it still needs _some_ statement. A semicolon on its own is a null statement, and by adding the null statement, the while () is happy having a statement. If you remove the null statement, then the next statement will now belong to the while (). Many people write "while (xxx) /* nothing */;" or "while (xxx) {}" to make it more clear. – gnasher729 Mar 11 '14 at 13:43
  • @gnasher729 hmmm, that is what I said, did you perhaps mean to comment on the question? – Shafik Yaghmour Mar 11 '14 at 13:45
2

Usually, in a while loop, you have initialization, a comparison check, the loop body (some processing), and the iterator (usually either an addition of an index, or a pointer traversal e.g. next), something like this:

index = 0 // initialization
while(index < 4) { // comparison, loop termination check
     printf('%c\n', mystring[index]); // Some processing
     index += 1; // iterate to next loop
}

Without at least the last item, you won't ever exit the loop, so normally the loop body has more than one statement in it. In this case, they use post-increments like this:

while (string1[i++] == string2[j++]);

This does the comparison (the ==) and the iteration (the post-increment ++) in the comparison statement itself, and has no body, so there's no reason to add any other statements. A blank loop body can be represented by just a semicolon.

Corley Brigman
  • 11,633
  • 5
  • 33
  • 40
1

Semicolon is like empty instruction. If we don't put any instruction after while or use loop while with {} we must use semicolon to tell compiler that all we want from while loop is doing this empty instruction.

Ardel
  • 315
  • 2
  • 9
0

That is called a semicolon. In programming standards, the ; signifies an end of statement, or in this case that it is a null statement. It is effectively a non operation in the body of the while loop, so it is not actually doing anything.

Rivasa
  • 6,510
  • 3
  • 35
  • 64