C program to check syntax of ‘for’ loop
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.
Input : for (i = 10; i < 20 i++) Output : Semicolon Error Input : for(i = 10; i < 20; i++ Output : Closing parenthesis absent at end
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.