A simple and short quick start guide into programming for any beginners.

in #programming7 years ago (edited)

Since this is general information about programming we will not use one particular programming language but explain important concepts that apply to every language.

Your computer will execute the instructions you've programmed in a linear fashion from top to bottom with the ability to jump up and down. Since following instructions strictly from top to bottom without any jumps will limit your program's possibilities greatly there are a couple of constructs which are used to control the flow of your program.

But first we are going to explore the concept behind variables:

A variable stores information. While some programming languages are not as strict than others when it comes to what kind of information one can store in a certain variable keep in mind that a variable usually has a type that describes what kind of information it can store.

Common types are

  • int(integer) stores whole numbers positive or negative
  • char(character) stores a single character such as 'a' or '?'
  • stringstores a sequence of characters
  • floatstores a decimal number
  • bool(boolean) stores either true or false

Creating a variable and store information in it usually looks like this:

int Integer = 5
char Character = '!'
string String = "Hello"
float Float = 3.14
bool Boolean = true

So since a program manipulates data a variable is the most essential thing of programming.

Now we need to have a way of manipulating those variables. Variables have - depending on the type - certain operations they can perform. Since they are very intuitive just have a look at the example below:

int N = 4
int R = N + 3 // R now holds the value of 4+3 = 7
string S1 = "Hello"
string S2 = "World"
string S3 = S1 + S2 // S3 now holds "HelloWorld"

Now a couple of things to mention here.

First you probably have noticed by now but variables have names in order to address them. The names can usually contain any alphabetic character upper and lower case as well as underscores and numbers. The only exception is that they can not start with a number.

The second thing to mention are comments. In order to make the code easier to read you employ the use of comments which are usually introduced by '//' although this strongly depends on the language. While the double slash will make the rest of a line count as a comment there is also the option to block-quote:

/*
This is 
a block-quote!
*/

Comments are ignored in the execution of your program.

Third here's a list of common operations and their respective operators:

Integer + 3 // Addition
Integer - 3 // Subtraction
Integer * 3 // Multiplication
Integer / 3 // Division
!Boolean // Negating, true becomes false and the other way around
Integer = (Integer + 3) *3 - (Integer - 2) // you can use brackets
Integer = 4 // assigns the value on the right side to the left side

So now since we dove a bit into variables and their operations we will have a look at how to control the flow of your program. Your program will start at the entry-point which is different for each language. From there on you can make use of the following constructs to control the flow.

Conditions


If-Statements

bool Condition = false
int Integer = 4
if(Condition) // if Condition is true then the code in {} is executed
{
    Condition = !Condition // negates Condition and stores the result in Condition, basically just negating the variable
}
else if(Integer == 2) // else if is evaluated when all conditions above are false, check whether Integer is the same as 2
{
    Integer++ // adds 1 to Integer and stores the result in Integer
}
else if(Integer != 3) {} // checks if Integer is not equal to 3
else if(Integer < 3) {} // checks if Integer is smaller than 3
else if(Integer <= 3) {} // checks if Integer is smaller than or equal to 3
else if(Integer > 3) {} // checks if Integer is greater than 3
else if(!(Integer != 1)) {} // you can use brackets also, checks if Integer is not equal to 1 and then negates the result
else if(Condition && Integer == 3) {} // A && B will be true only if both A and B are true, false else
else if(Condition || Integer == 1) {} // A || B will be true if A or B or both are true, false when both are false
else // { Code } executed if all above conditions are false
{
   if(Integer == 5) // you can stack if-statements and don't need any else if or else block
   {
        Integer-- // subtracts 1 from Integer and stores the result in Integer
   } 
}

So there is a lot new information to take in. The comments should explain most of it. Some additional mentions:

  • Integer + 1 by itself will not do anything useful since the result is thrown away if not stored: Integer = Integer + 1
  • Integer = Integer + 1 can be shortened to Integer += 1, this works for other operators in the same manner
  • && (AND) and ||(OR) are binary operators which output a boolean
  • <, <=, >, >=, ==, !=are operators which compare two operands (left and right) and output a boolean

Switch-Statements

char Character // you don't need to assign a value when creating a variable
switch(Character) // variable that is compared to the cases 
{
   case 'A': Integer-- break // 'Integer-- break' is executed when Character is 'A' 
   case 'B': Integer++ break // break will break out of the switch statement
   case 'C': /* some code */ break // there can be any number of cases
   case 'D': /* some code */ // leaving out break would execute 'Integer = 1' even though Character is not E
   case 'E': Integer = 1 break 
   default: Integer = 0 // this will be executed if none of the cases were true
}
Integer -= 2 // executed after Switch-Statement is left

Some additional comments:

  • switch can take in several types of variables depending on the particular language
  • if there is nothing left to execute within {} the execution will continue after } (this applies to every pair of {})

That's it for the most common condition-constructs.

Before we continue with loops we will quickly introduce scopes.

Scope


A scope is a block of code in which knowledge about variables is contained. To 'see' certain variables they need to be in scope. Variables outside of scope can not be used within the scope. A scope is introduced naturally by for example an if-statement or can be created manually. A higher level scope A that contains scope B cannot see what's in scope B but scope B can see scope A. As a side note: before you use a variable it must be known first. You cannot use variable A in line 1 and create it in line 2 since A exists from line 2 and onward. In this article and in many programming languages you can spot a new scope by looking at curly braces. To illustrate that better here is some code:

{ // scope 1 contains every other scope in this example
    bool Condition // Condition is in scope 1
    if(Condition)
    { int A = 1 } // A is contained in scope 2 and can not be seen from scope 1  
    else
    { int B = 1 } // B is contained in scope 3 and can not be seen from scope 1
    { // scope 4
       int C = 1 // C is contained in scope 4 
       A++ // error: A can not be seen from this scope
       { // scope 5
          C++ // correct: C is in known in scope 4 which contains scope 5
       } // end: scope 5
    } // end: scope 4
} // end: scope 1

Now we've had a quick insight into scopes and we will continue with constructs used for flow-control.

Loops


A loop basically repeats a block of code as long as the loop condition is true. The loop-condition is evaluated after each iteration (complete execution of code contained within the loop).

While-Loop

while(Condition) // as long as Condition is true 'some code' is executed
{
    // some code
}

This is the most basic loop. You can use this loop to recreate the behavior of other loops. One of which comes next.

For-Loop


In order to explain how a for-loop works it's best to have a look at it.

for(<variable creation>; <Condition>; <Post-Instruction>) 
{ /* some code */ }

First you can create variables and assign values to them. If you create more than one variable you usually separate them with a comma. The variables created can be accessed in the loop-body. Second is the loop-condition which determines if the loop-body is executed or not. Third we have an instruction that is executed after each iteration (after the loop-body has finished executing). For-loops are usually used when you need to have a counter inside the loop. Here's an example of a for-loop that counts from 0 to 9 and then stops:

for(int i = 0; i < 10; i++) 
{ /* some code */ }

That concludes the most important loops you'll encounter. Next up are functions.

Functions


A function is a block of code which can receive input to produce some output. Functions can be 'called' by their name. Let's have a look at an example function:

float square(float x)
{
    return x*x
}

This function will take in a so-called parameter named x of type float and output a result of type float. It is named appropriately since you can see what it does just by looking at the name. The first float is the so-called return-type that is the output of this function. Then in round brackets there is one parameter of type float that is the input of this function. Within the functions body it calculates the square of x and then returns it.
Calling this function will look like this:

square(3) // call the function with 3 as x
float Result = square(2) // store the output of the function in Result

A function can return any type and an additional 'type' named void which means that it doesn't return anything. There can be no parameters or multiple. Here's another example:

void print1To(int x)
{
    for(int i = 1; i <= x; i++)
    {
        print(i)
    }
}

This function will print the numbers from 1 to x. We don't have to know the implementation of 'print' in order to use it. To call a function it needs to be in scope.

Now this concludes the most important topics a beginner should know. But this is not the end. We are quickly going into object-oriented programming since this is a topic of great importance even if your programming language is not object-oriented.

Object-Oriented Programming


Object-oriented programming employs the use of objects which are concrete instances of classes. A class can be seen as a blueprint and an object is made using this blueprint. Here's an example of how a class could look like:

class Cat // class is named 'Cat'
{
public: // everything in this category can be access from the outside
    Cat(string name) // constructor
    {
        this.name = name // assigns parameter to member variable
    }
    void meow()
    {
        print(name+": Meow!")
    }
private: // everything in this category is hidden from the outside
    string name // member variable
}

Now this is a class called 'Cat'. It is a blueprint for a very simple cat that can meow. A couple of mentions:

  • public and private dictate visibility to the outside (see below)
  • constructor is called upon construction of an object
  • a class can have any number of so-called member-functions and -variables.

This is how one would create and use an object of this class:

Cat KittyCat("Blacky") // creates object KittyCat
KittyCat.meow() // calls member-function meow
KittyCat.name // error: cannot access private members

And this concludes this quick introduction to object-oriented programming.

Now is the perfect time to decide on a programming language and get into actually writing code. Since you already know the basics it will be a very easy start. You'll first learn the language-specific ways of writing things down and then how to apply those concepts above to create actual programs.

Have fun coding!

Sort:  

good job thank you !!!

Thanks, I appreciate it!

Nice post about code, i programing in c# and Java good information for beginners.

Thanks, my favorites are C++ and Javascript!

Wow, didn't know about Promise, what a great name!

A promise can be: fulfilled, rejected, pending, settled.

I'm nostalgic for the days when an introduction to coding meant learning BASIC. It's not very relevant anymore, but I think it would ease new folks into the steep learning curve a bit more gently.

I'm not sure if BASIC will help beginners, although it's in the name. It's a bit outdated I guess.

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 60309.00
ETH 2342.16
USDT 1.00
SBD 2.55