Tutorial 32 and general question

Hey, In java tutorial 32- Arrays in Methods, I saw that after you made the' for statement' you didn't put the curly brackets- { } for whatever you wanted to do with the conditions..and I thought that they are needed whenever I use a for loop..but there was no error msg and so I wonder how and when should I use or not use them after a for statement or if there are more exceptions to it.

You put brackets when you want to execute multiple lines of code for a given condition. You can use braces for one statement to be executed after a condition, but is optional.

int i=0;int j=0;if(j==0) i++;j++;System.out.println(i) would result in 1 with j also being equal to 1.

int i=0;int j=0;if(j==0){i++;}j++;System.out.println(i) would result in 1.This is the same code as above except it uses braces for clarity.

int i=0;int j=0;if(j==1){i=5;j=6;}System.out.println(j) would print 6.Here braces are necessary because if j==1, we want i to be 5 and j to equal 6 so they are different statements that we want to execute under the same condition.

1

Java / Android Development

124,370 followers

About

Very popular language used to create desktop applications, website applets, and Android apps.