When using your favorite apps or scrolling through a website, do you ever wonder how they were made? Or, maybe you have a brilliant idea for an app or website but feel overwhelmed by the thought of building it from scratch. Learning to code can seem daunting, but it can be both an exciting and rewarding journey that unlocks endless possibilities for creativity and innovation.
We'll dive into the world of coding and explore the basics. We'll talk about the challenges beginners often face, including frustrating syntax errors and seemingly endless debugging. But fear not! We'll also share tips and tricks for overcoming these roadblocks and staying motivated. So, buckle up, grab a cup of your favorite beverage, and let's explore the exciting world of coding together!
Choosing a Language
Congratulations! You've decided to embark on a challenging journey
that will unlock endless opportunities for you to use your imagination.
Learning to code may seem intimidating at first, but by taking small steps and
learning the fundamentals, you will find you will have traveled further than you thought.
So, where do you begin? The first step is to choose a language to learn. There are many options out there, from popular languages like Python and JavaScript to more niche languages like Swift for iOS development. It can be tempting to jump into the deep end and tackle the most complex language you can find, but I recommend starting with a language that is beginner friendly.
I would suggest an interpreted language, like JavaScript or Python. So, what is an interpreted language? Good question, in simple terms it means the program can be run without being compiled first. Wait, now what’s a compiler? Some languages, like Java and C++, require human-readable code to go through a compiler. The compiler then takes the human-readable code and converts it to machine-readable code.
I personally chose JavaScript to learn, but you are free to choose any language that appeals to you and your goals.
Resources
Once you've chosen your language, it's time to find resources to help you learn. There are countless online courses, tutorials, and books available, so it's important to find the right ones for you. Some people prefer structured courses with assignments and deadlines, while others prefer the flexibility of self-paced learning. Don't be afraid to try out different resources until you find what works best for your learning style.
Below I will talk about resources I have personally found useful. I don't have an affiliation with any of them, and receive nothing if you click on the links. This is just for your reference.
I’ve personally used the free option of Codecademy. The learning user interface is fun and interactive. It also has practice exercises along the way. Of course, you can pay for the Pro version and get access to projects and even more learning modules.
Another great source for learning material is Udemy. There are some free courses available, but the vast majority of the courses are paid courses. If you choose to go the paid route, wait for the courses to go on sale, which they do, and often, you can get them at prices nicer to your wallet.
If you haven’t heard of freeCodeCamp, you should give them a look. They have an interactive user interface as well that allows you to write code as you learn. They have a blog and a YouTube channel that offers courses on different languages and projects. The great thing about freeCodeCamp is everything they do is offered to you for free.
A similar option to freeCodeCamp is The Odin Project. It’s
not as broad in the knowledge base, it sticks to Ruby on Rails and JavaScript,
but it is a solid way to learn to code. While they produce some of the course
material, they also make use of the material available on other websites. It’s
not a ding against them, it exposes you to various learning materials, and
maybe even more resources for you to explore.
Basic Coding Concepts
At this point, I'm going to assume you haven't had time to explore the resources yet and are jumping at the bit to get some coding knowledge. This section is just about exposing you to concepts. Don’t worry about the code just yet. The great thing about basic concepts is it doesn’t matter what language you use. The syntax, or the specific requirements for the language, may be different; but the concept is the same.
Variables
First off, let's talk about variables. Variables are like containers that hold information, such as numbers or words. Think of them as labeled boxes that you can store things in. For example, you might have a variable called "name" that holds the value "John". Later on in your code, you can use the variable "name" to reference that value.
Below is an example in JavaScript. The variable "name" is holding the value "John". When "name" is called in the print statement, console.log, instead of printing "name" it prints the value of the "name". In this case "John".
1 2 3 4 5 | const name = "John"; console.log("Hello " + name + "!"); // Hello John! |
Functions
Next up, we have functions. Functions are like mini-programs that can perform specific tasks. They're great for breaking down complex tasks into smaller, more manageable pieces. For example, you might have a function that calculates the average of a set of numbers. Whenever you need to calculate an average, you can simply call that function instead of writing the code from scratch every time.
Functions help you keep your code DRY. What is DRY? It stands for “Don’t Repeat Yourself”. If it is at all possible to write a function to prevent repetitive coding you should do it. If you have copied and pasted code throughout your program, then chances are it isn’t DRY. If there is a mistake in one of your pasted sections, it is probably in all your pasted sections. Now you need to go through your code and find all of those copied and pasted sections and fix the mistake. However, if you had written a function, you may find that all you had to do was correct the function and the rest of your code would be okay.
A function also has parts. In the JavaScript example below the name of the function is “hello”. Inside the parenthesis, we have a parameter. In our case, the parameter is “name”. We can use the parameter inside the function. When we call on the function we will call it with an argument in parenthesis, in this case, “John”. This is what will become the value for the parameter.
1 2 3 4 5 6 7 | const hello = (name) => { console.log("Hello " + name + "!"); }; hello("John"); // Hello John! |
Conditional Statements
Another important concept is conditional statements. These are basically "if-then" statements that allow your code to make decisions based on certain conditions. For example, you might have a conditional statement that says "if the user is logged in, show them their account information. If they're not logged in, prompt them to log in." This helps your code make intelligent decisions based on what's happening in the program.
If statements have conditions. In its most basic form it usually follows the format:
1 2 3 | if (condition) { // instructions if condition is true } |
In the below JavaScript example, we are using a function to determine if the person's name is John. If their name is John, we will say, “Hello John!”. If the person is not John, then we will say, “You’re not John!”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const hello = (name) => { if (name === "John") { console.log("Hello " + name + "!"); } else { console.log("You're not John."); } }; hello("John"); // Hello John! hello("Jake"); // You're not John. |
Note: When programming it’s good to remember everything is case-sensitive. For instance, the above code would not have recognized “john” or “JoHn” for “John”.
Loops
Finally, let's talk about loops. Loops are used to repeat a set of instructions multiple times. This is great for tasks that need to be done over and over again, such as iterating through a list of items. For example, you might have a loop that prints out all the numbers from 1 to 10.
For Loops
There are different types of loops. The first one we will talk about is the for loop. This loop allows you to run code over and over for a limited number of times until a condition is met.
The below example shows the usual format for a for loop.
1 2 3 4 5 | for (initialization statement; loop condition; increment statement){ // instructions to repeat } |
Say we wanted to print that list we mentioned earlier for numbers 1 to 10. Instead of printing the numbers manually one by one, we can use a loop. In the below JavaScript example, we can see a for loop in action. In the initialization statement, we set the variable i to 1. Then we set a loop condition, so long as the value, i is less than or equal to 10 follow the instructions, in our case print i. For the increment statement, we are increasing the variable i by 1 after the instructions are followed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | for (let i = 1; i <= 10; ++i) { console.log(i); } // 1 // 2 // 3 // 4 // 5 // 6 // 7 // 8 // 9 // 10 |
While Loops
While loops continue to run until a condition is met. Unlike the for loop, there isn’t a limit on how many times the loop can run. It will just continue to run until the stop condition is met.
While loops are useful in situations where the number of iterations is unknown or depends on some external factor that can change during the course of the program’s execution.
The usual format for a while loop is:
1 2 3 4 5 | while (loop condition) { // instructions to repeat } |
In the below JavaScript example, we are making the same list as earlier, but this time with a while loop. The loop condition is as long as i is less than or equal to 10. In the instructions we print i and have the incrementing step of ++i, this is so we don’t have an infinite loop. An infinite loop is a loop that runs indefinitely. This happens when the loop condition always evaluates to true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | let i = 1; while (i <= 10) { console.log(i); i++; // same as i += 1 or i = i + 1 } // 1 // 2 // 3 // 4 // 5 // 6 // 7 // 8 // 9 // 10 |
Do...While Loops
A do-while loop is similar to a while loop, but with one important difference: the instructions inside the loop are always executed at least once, regardless of whether the loop condition is initially true or false.
The usual format for a do-while loop is:
1 2 3 4 5 6 | do { // instructions to repeat } while (loop condition); |
In the JavaScript example below, I purposely made the condition false so you could see that the loop would run at least once. The loop will perform the do first, in this case, print i. Then, it will check to see if the condition is true. Since it evaluates to false, it only runs once.
1 2 3 4 5 6 7 8 | let i = 11; do { console.log(i); } while (i <= 10); // 11 |
Additional Resources
Understanding these basic coding concepts will give you a solid foundation to build upon as you continue on your coding journey.
Now that you understand the basics, you can try your hand at solving some problems. Codewars, CodeSignal, and LeetCode are fun ways to try your hand at solving programming tasks. You progress through the levels by solving problems. It may seem overwhelming at first, but make sure to break the problem down into smaller parts so you understand what is being asked of you.
Remember, coding can be challenging at times, but it's also incredibly rewarding. So, keep at it, and don't be afraid to ask for help when you need it! There are forums and discords set up for learners to reach out to more experienced programmers.
Top photo by Adi Goldstein on Unsplash
No comments:
Post a Comment