{} these signs | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

{} these signs

Can someone please explain how to properly use these in your programming everytime i use it it says error on line 15 sum expected "}"

26th Nov 2016, 11:18 PM
Mo Molden
Mo Molden - avatar
3 Answers
+ 3
Curly braces are used to delimit blocks of code, mainly for functions or loops. Every braces you open should, sooner or later, be closed. When writing some code, you usually want to indent it to make it more readable, curly braces should be closed at the end of an indented block. For instance, a function that returns the highest number: int highest(int a, int b) { if (a < b) { return b; } else { return a; } } Here, we open a curly bracket that will stay until the end of the function, inside which some braces are used as well. If we count them, we have 3 opening braces and 3 closing ones, everything is OK. (note that this function could be written in more elegant ways, but I just wanted to show you how it works for functions as well as an if)
26th Nov 2016, 11:31 PM
Pierre Varlez
Pierre Varlez - avatar
+ 2
simply put, any commands that englobe multiple statements (functions, ifs, loops) needs to be englobed in those wrappers. for example: if (a certain condition is met) { <-- open one of these do this... do that... do the other thing... done, exit... } <-- close wrapper to say that those conditions belong to the if statement more code here... hope this helps. :)
26th Nov 2016, 11:28 PM
Frédéric Charette
Frédéric Charette - avatar
+ 2
thank you both very much i knew about them but ive never had anyone explain it to me
26th Nov 2016, 11:34 PM
Mo Molden
Mo Molden - avatar