Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why there is semicolon after loop while();

Tags:

c

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?

like image 330
user3376115 Avatar asked Mar 03 '14 19:03

user3376115


Video Answer


1 Answers

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.

like image 153
Shafik Yaghmour Avatar answered Nov 15 '22 05:11

Shafik Yaghmour