is this ok or do i have to place {} between else and if | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

is this ok or do i have to place {} between else and if

#include <iostream> using namespace std; int main() { int a=20,b=9,c=7; if((a>b)&&(a>c)) { cout<<"a is greater"; } else if((b>c)&&(b>a)) { cout<<"b is greater" ; } else { cout<<"c is greater" ; } return 0; }

19th Aug 2016, 7:11 AM
Kishan Khanpara
Kishan Khanpara - avatar
3 Respostas
+ 1
It will possibly compiled without any errors. On the other hand, for more than one line of statement, it's necessary to use curly braces. Within this, you may put one in this line: else if((b>c)&&(b>a)) as: else { if((b>c)&&(b>a)) and of course close it after the second else statement. The rule is: you MUST use curly braces after both if and else statements, except they contain only one line of statement. In this situation, if/else is one line only (no semicolon). By the way, I suggest to simply forget this simplified rule and always use curly braces. For intending: there is no rule. Use what you find useful.
19th Aug 2016, 10:28 AM
Tamas Szekffy
Tamas Szekffy - avatar
0
You don't need to indent out for each if else if, else statement. By that I mean they should all be lined up, (and yes) with the curly brackets {} around the blocks of code for each statement. if ( something ) { Do this } else if ( something ) { Do that } else { Do that other thing }
19th Aug 2016, 7:25 AM
Donovan
Donovan - avatar
0
it is correct there is nothing wrong in it but u can ignore curly braces here i wanna say is here it is not mandatory
24th Aug 2016, 6:00 AM
PRANJAL AGNIHOTRI
PRANJAL AGNIHOTRI - avatar