Skip to content
Related Articles

Related Articles

Improve Article

C program to check syntax of ‘for’ loop

  • Difficulty Level : Basic
  • Last Updated : 26 Nov, 2019

As defined by C standards, the for loop syntax is:

for (initialisation; condition; increment/decrement)
        ... 

Syntactically, there should be two semicolons, one opening parenthesis, one closing parenthesis, and correct spelling of “for”. Hence, to check only the syntax of for loop, what a compiler does is check the following conditions:

  • Only “for” is written, and not “For”, “FOR”, “foR” or any of its variants.
  • Total statement consists of two semicolons “;” before the closing parenthesis “)” ends.
  • Presence of an opening parenthesis “(” after the “for” keyword, and presence of a closing parenthesis “)” at the end of statement.

Examples:

Input : for (i = 10; i < 20 i++) 
Output : Semicolon Error

Input : for(i = 10; i < 20; i++
Output : Closing parenthesis absent at end 

Code –




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
//array to copy first three characters of string str
char arr[3]; 
  
void isCorrect(char *str)
{
      
    //semicolon, bracket1, bracket2 are used 
        //to count frequencies of
    //';', '(', and ')' respectively
    //flag is set to 1 when an error is found, else no error
    int semicolon = 0, bracket1 = 0, bracket2 = 0, flag = 0;
      
    int i;
    for (i = 0; i < 3; i++)
        arr[i] = str[i];
          
    //first 3 characters of the for loop statement is copied
    if(strcmp(arr, "for") != 0)
    {
        printf("Error in for keyword usage");
        return;
    }
      
    //Proper usage of "for" keyword checked
    while(i != strlen(str))
    {
        char ch = str[i++];
        if(ch == '(')
        {
            //opening parenthesis count
            bracket1 ++;
        }
        else if(ch == ')')
        {
            //closing parenthesis count
            bracket2 ++;
              
        }
        else if(ch == ';')
        {
            //semicolon count
            semicolon ++;
        }
        else continue;
          
    }
      
    //check number of semicolons
    if(semicolon != 2)
    
        printf("\nSemicolon Error");
        flag++;
    }
      
    //check closing Parenthesis
    else if(str[strlen(str) - 1] != ')')
    
        printf("\nClosing parenthesis absent at end");
        flag++;
    }
      
    //check opening parenthesis
    else if(str[3] == ' ' && str[4] != '(' )
    
        printf("\nOpening parenthesis absent after for keyword");
        flag++;
    }
      
    //check parentheses count
    else if(bracket1 != 1 || bracket2 != 1 || bracket1 != bracket2)
    
        printf("\nParentheses Count Error");
        flag++;
    }
      
    //no error 
    if(flag == 0)
        printf("\nNo error");
              
}
  
int main(void) {
      
    char str1[100] = "for (i = 10; i < 20; i++)";
    isCorrect(str1);
      
    char str2[100] = "for i = 10; i < 20; i++)";
    isCorrect(str2);
      
    return 0;
}

Output :

No error
Opening parenthesis absent after for keyword 

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :