+ 1
When should i use the { } braces in java pls can anyone answer
When should i use the { } braces pls can anyone answer cuz evrrytime theres a error for it
5 Answers
+ 4
Curly braces denote a block of code with local scope
{
int n = 5;
}
System.out.println(n);
This will lead to an error (unless there's a global variable n) because the scope of the variable n is only within the braces and the variable is unknown outside of the braces.
If an if statement evaluates to true, the following line will be executed. This can be extended to a block of several lines of code by using {}. Same with loops etc.
+ 4
someMethod(){Inside the braces is the body of this method when ever someMethod() is called what ever is inside here will be exectued}
+ 2
In java:
* classes
public class ClassName{
}
* methods
public void print(){
}
* constructors
public ClassName{
}
* loops
while(){
}
for(){
}
* if statements
if(){
}
* switch statements
switch(){
}
...
+ 1
Every time use brackets for content inside a for, while loop or if statement
+ 1
It depends on which programming language you are talking about.
If you are talking about java then it is used to enclose a block of statements after a if or loop.
Eg.
if(a>3)
{
B=5;
C=6;
D=7;
}
Without the braces only the first line would execute if the condition is true and the rest will execute always i.e. even if condition is false.