Semicolon at end of 'if' statement

前端 未结 18 1618
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 01:01

Today, after half an hour of searching for a bug, I discovered that it is possible to put a semicolon after an if statement instead of code, like this:

if(a          


        
18条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 01:26

    I can think of a scenario where an empty statement is required (not for if condition but for while loop).

    When a program just want an explicit confirmation from the user to proceed. This may be required when the work after the user confirmation depends on some other things and user want to take control of when to proceed.

        System.out.println("Enter Y to proceed. Waiting...");
        System.out.println("");
    
        while(!(new Scanner(System.in).next().equalsIgnoreCase("Y")));
    
        System.out.println("Proceeding...");
        // do the work here
    

提交回复
热议问题