Tân Nguyễn Tân Nguyễn - 23 days ago 8
C# Question

What does C# syntax ;; mean?

My case:

// no syntax error
if (true) Console.Write("hi!");

// no syntax error
if (true) Console.Write("hi!"); ;

// no syntax error
if (true) Console.Write("hi!"); ; ;

// no syntax error
if (true) Console.Write("hi!"); ; ; ;


So, What does the second/third/fourth
;
mean? Should the compiler throw syntax error?

Answer

; means empty statement. It does nothing.

In some cases (for example robotic programming), it is useful to instruct the program to continuously pool environment condition and do nothing until the condition fulfilled.

Example

// turn motor on so robot is going forward
TurnMotorOn();

// Do nothing (means motor is still on and robot is still going forward) until there is obstacle in front of the robot.
while(!ThereIsObstacle()) ;

// turn motor off so robot is stopped.
TurnMotorOff();