I just created a simple project with if statements to check if the statement is true, but then I stumbled into something weird.
This is my sample code
if(x ==1)
{
do something;
}
else if( x==2)
{
do something;
}
else if( x==3)
{
do something;
}
else if( x==4)
{
do something;
}
{
do something;
}
nope, you read that right. And I typed that right also. I have left out the else statement, but I have created curly brackets for it. When I compile, it runs fine. Is this some kind of bug? Or am I just new to this? I just wanted to know. Thanks for all the answers
Your last code block without an
if/else ifwill execute. Curly braces are for code separation, and don’t require conditional/iteration or any other precursor to have meaning.For an exageration to further explain my point:
That is possible, as well.
As per @slugonamission‘s comment, he brought up a great point. The code block also creates a scope isolation for variables that are created inside of this code block.