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; }
3 ответов
+ 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.
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
}
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