基本控制結構

除非另有說明,否則所有控制結構都使用塊語句 。這些用花括號 {} 表示。

這不同於正常的陳述 ,這也並不需要花括號,還配備了一個僵硬的警告中,只有行緊跟前面的語句將被考慮。

因此,編寫任何這些控制結構而沒有花括號是完全有效的,只要在開頭後只有一個語句,但強烈建議不要這樣做,因為它可能導致錯誤的實現或程式碼損壞。

例:

// valid, but discouraged
Scanner scan = new Scanner(System.in);
int val = scan.nextInt();
if(val % 2 == 0)
    System.out.println("Val was even!");

// invalid; will not compile
// note the misleading indentation here
for(int i = 0; i < 10; i++)
    System.out.println(i);
    System.out.println("i is currently: " + i);