Can the statement 'else' be used inside the block of an 'if' statement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can the statement 'else' be used inside the block of an 'if' statement?

In other words, does 'else' require its own brackets?

17th Oct 2016, 12:27 PM
Alper Çiftci
Alper Çiftci - avatar
4 Answers
+ 5
You can't put the else inside the block of its matching if if that's your question. You can only put it right after the if block. By the way, if there is only one statement in your block, you don't need to put brackets. This goes for if, else, while, for, etc. if (condition) { one_statement; } else { another_statement; } is equivalent to if (condition) one_statement; else another_statement;
17th Oct 2016, 1:02 PM
Zen
Zen - avatar
0
I just tested it. 'else' needs to be in its own curly braces.
17th Oct 2016, 12:31 PM
Alper Çiftci
Alper Çiftci - avatar
0
No .. You can't
21st Oct 2016, 9:03 AM
sahag
sahag - avatar
0
You have to use else by itself.
23rd Dec 2016, 12:24 AM
Philip Saunders
Philip Saunders - avatar