If/else statements | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

If/else statements

Whats the difference of using curly brackets and not using curly brackets. Why would you need to use them?

21st Sep 2020, 12:57 AM
kawaii Panda
kawaii Panda - avatar
3 ответов
+ 3
we can omit the curly braces after if-else statements, or after any loop. If we do not use curly braces then only one statement after the if-else or loop will be considered under that block. For example 
21st Sep 2020, 1:02 AM
code0rdie
code0rdie - avatar
+ 2
if(condition) {    Line 1    Line 2 } if(condition) Line 1 Line 2
21st Sep 2020, 1:02 AM
code0rdie
code0rdie - avatar
+ 2
In the first case, the Line1 and Line2 both are in the if block. But in the second condition, the Line1 is in if block but Line2 is not in if block. So we can omit curly braces only there is a single statement under if-else or loop.
21st Sep 2020, 1:03 AM
code0rdie
code0rdie - avatar