Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlexander Nowak
498 PointsWhat is the point/use in code blocks?
I am trying to understand why code blocks are used and how I can use then in my coding.
3 Answers
Kevin Faust
15,353 PointsHey Alexander,
Code blocks { } are used to mark the start and end of a piece of code. If you like, you can think of the opening curly brace { as standing for the word BEGIN to mark the beginning of a section of code. and the closing curly brace } would then stand for the work END to mark the end of a section of code.
Hope that helped and happy coding!
Kevin
Alexander Nowak
498 PointsHi Kevin.
Thanks for that!!
But why are they needed? Why cant you just type all your code in one go? Why does it all have to be sectioned into various code blocks?
Cheers
Alex
Kevin Faust
15,353 PointsHey Alexander,
All programming languages use code blocks. You cant write code without code blocks.You can write some lines of simple code but as you dive deeper and explore classes and objects, its not possible without code blocks. Think of the code blocks as the first capital letter and punctuation in a sentence. Java is a object oriented programming language with classes, objects, methods, etc. How do you create all those things? With code blocks.
Lets take a look at this java code:
public class Hello{
public static void main(String arg[]){
System.out.println("Hello.");
}
public void sayWhatsUp () {
System.out.println("Whats Up?");
}
}
When we create our hello class, the word "Hello" is printed out. If we call the sayWhatsUp() method, it will print out the word "Whats Up?" to the console. You might have not gotten this far but just understand that the code above can print out two things. First off without the code blocks, the code checker cant distinguish what belongs to what and what we want to happen. In the above code, we dont want both those words to be printed. We only wants "What's up" to be printed only when we call it.
It is not even possible to create this code or any code without code blocks. Lets look at it with no code blocks:
public class Hello
public static void main(String arg[])
System.out.println("Hello.");
public void sayWhatsUp ()
System.out.println("Whats Up?");
First off this is pretty hard to read, second off it breaks all the rules of java, third this code will stop working before the code checker even finishes reading the first line.
To write code, you must write it in code blocks. Otherwise think of it as run on sentences with no periods and also with terrible grammar. If you had a 500 page book with run on sentences and terrible grammar and things dont even make sense, would you want to read it? I highly doubt it. We want things to be easy to read, organized, and neat. Same thing with java code. Even if your still wondering why we need code blocks, as you progress along the track, you absolutely will see and realize how we need code blocks not only in java but in all other languages. Just continue along the track, really undestand what Craig does in each video, and before no time you will have a solid grasp on this concept.
Dont forget to mark as best answer so that others with the same question can see this!
Thanks and happy coding!
Kevin
Kevin Faust
15,353 PointsHey Alexander,
To be honest, I think it's best if you just experience the use of code blocks yourself. Go through the track and see how Craig uses code blocks. Whenever he uses one, try to analyze why he did that and the purpose of doing so.
You will see when following along, using methods and classes require code blocks. Over time, they will just become second nature. When you want to write code, you put your code inside code blocks. You will find that it is like 1+1 eventually. And you can see how explaining something like that is not too easy. Code blocks are the grammar of programming. Don't worry if your still confused, it will slowly make more sense.
I hope I helped you somewhat through this discussion
All the best,
Kevin
Alexander Nowak
498 PointsAlexander Nowak
498 PointsThanks for that Kevin! That makes sense. So how do I know when to start/end a code block when I'm using them in my code?