+ 1

is it necessary to put the curly Braces { } after the if() and else? Coz in the code below i didnt put braces and it worked well

#include <iostream> using namespace std; int main() { int x=1; if(x>0) cout<<"positive number"<<endl; else cout<<"negative number"; return 0; }

8th Sep 2017, 8:23 AM
RiGeL
RiGeL - avatar
4 Respuestas
+ 2
You only need to use brackets if it's more than one line. You only used one line in this case, so it works.
8th Sep 2017, 8:38 AM
aklex
aklex - avatar
+ 1
Curly brackets are only required if you want to put more than only one instruction in the block...
8th Sep 2017, 8:39 AM
visph
visph - avatar
+ 1
it is not necessary if you have only one instruction after the If or Else. if there is more than one instruction, you need braces
8th Sep 2017, 8:39 AM
Paul
+ 1
Thank you All
8th Sep 2017, 8:41 AM
RiGeL
RiGeL - avatar