Jump to content

Computer programming

From Simple English Wikipedia, the free encyclopedia

Computer programming is the process of telling a computer to do certain things by giving it instructions. These instructions are called programs. A person who writes instructions is a computer programmer. The instructions come in different programming languages, like C++ or Java. Sometimes, programmers use special software, such as integrated development environments (IDEs), which have many special parts, including a text editor, to help them to type and edit programs.[source?]

Computers can understand instructions if those instructions are written in machine code, meaning long patterns of ones and zeroes. Writing a whole program in machine code would take a long time, so instructions are written in special programming languages that are easier for people to understand. The computer converts that into "computer form" instructions (in other words, machine code) so the computer can follow them. The instructions can also be written in an assembly language, which is almost the same as machine code but a little easier to understand.[source?]

Converting a program from its original programming language to machine code is called "compiling" the program. Not all languages need to be compiled. Some languages, called interpreted languages, use interpreters instead. An interpreter is a program specially written to read a programming language and execute its instructions right away. For example, Python and JavaScript use interpreters.[source?]

Programming Concepts

[change | change source]

Variables

[change | change source]

A variable is what programmers call a little piece of data, like a number or someone's username. Programming languages usually have a few different built-in types of variables.

Conditionals

[change | change source]

Conditionals are parts of the program that work if something the program can check to see whether it is true. If that part is not true, then the program won't make it happen. A conditional is often done with an "If Statement".

Here is an example of an if statement in the Perl programming language. What it does is it checks to see if the name variable is Bill. If the name variable is Bill, then it will print out the words "Hi Bill!".

#!/usr/local/bin/perl
$name = "Bill";

if ($name eq "Bill") {
    print "Hi Bill!";
}

Sometimes, a programmer might want to have the if statement do something else if the first part of it is not true. This is known as an else block. Here is an example of an else block in the Perl programming language.

#!/usr/local/bin/perl
$name = "Ted";

if ($name eq "Bill") {
    print "Hi Bill!";
}
else {
    print "Hi person who is not Bill!";
}

And sometimes, the programmer might want to have multiple things for the if statement to do. For example, they may have the if block run if something is true, but will have parts of the if statement known as else if blocks that will run if the first part doesn't work, but if it works somewhere else. In the Perl programming language, else if is spelled like this, "elsif." Other languages might have it spelled like "else if" however. But for Perl, it is spelled like "elsif." Else If blocks will only run if their condition is true, just like the first if block. An if statement can have as many else if statements as the programmer needs. If the if block, and none of the else if blocks are true, then the plain "else" statement will be used by the program.

#!/usr/local/bin/perl
$name = "Ted";

if ($name eq "Bill") {
    print "Hi Bill!";
}
elsif ($name eq "Ted") {
    print "Hi Ted!";
}
elsif ($name eq "Alex"){
    print "Hi Alex!";
}
else {
    print "Hi other person!";
}

In programming, loops are used to run some code repeatedly. The simplest kind of loop is a so-called while-loop, which repeats until its condition is not true anymore. Here is an example in C.

while (condition) {
    printf("Hi! ");
    condition = checkSomething();
}

A while loop always checks first if a condition is fulfilled before the code in it is ever run. There also exists something similar, called a do-while-loop, which is guaranteed to run its code at least once and only checks its condition after the code in it has run to decide if it should run again:

do {
    printf("Hi! ");
    condition = checkSomething();
} while (condition)

A more versatile kind of loop is the for-loop, which in many languages (for example in C) takes three expressions. In C these are seperated by semicolons (;):

for (int i = 0; i < 10; i++) {
    printf("Hi! ");
}

The first expression is a piece of code that is run before the loop first runs any code or checks its condition. Here, it "initializes" a new variable i with the "type" int, which is an integer (whole number). It is set to 0. The second expression is the condition the loop checks before it runs its code. If this condition is not met, the loop has ended. The third expression is a piece of code which is run after every iteration of the loop, that means every time after the code in the loop has run. i++ is just another way to write i = i + 1, which increases the value of i by 1. To sum it up, the for-loop above can also be written as:

int i = 0;
while (i < 10) {
    printf("Hi! ");
    i = i + 1;
}

This will display the message "Hi!" ten times.

Comments

[change | change source]

In the program, a comment is information that is meant to be read by people who are reading the program. Comments have a special symbol in front of them that tells the computer that they are comments and should not be read as code.

Comments are used to explain how a certain part of a program works. This is helpful when multiple people are working on the same program, and if they need to work on a section where someone else was working on. If the programmer that was working on it first left behind comments for any other programmer that works on it later, it will help them know faster what is going on in the program.

Here is an example of programming comments in the C programming language. In C, the two slash symbols "//" known as a forward slashes, are used. With the comments, a person can read the code and know what is going on.

#include <stdio.h>
// This is a comment, ignored by the computer
int main(void) { // Here the starting point of the program is defined
    printf("Hello world!\n"); // Actual process
    return EXIT_SUCCESS; // Tell everyone that we had success
}

Sometimes, a programmer may need to remove something from the code, but for many different reasons, they may not want to just simply delete it. An easy solution is to use the comment symbol. The computer will think that the code is just comments, rather than actual code, but the programmer will still be able to see it and read it.

Here is an example of that in the Perl programming language. In Perl, the "#" symbol is used for comments, instead of forward slashes "//" like in the C (programming language).

$name = "Sam"; #we set the name variable to be Sam
$age = 14; #We set the age to be 14
# $country = "France";  because of the special comment symbol at the beginning of the line, this line is now a comment.

Debugging

[change | change source]

Computer programmers make mistakes when writing codes. The mistakes are called bugs and cause the program to follow the wrong instructions. Debugging is the process of finding and fixing the mistakes. To debug code is to find such mistakes.

There are many debugging methods. Software such as text editors and IDEs have tools that can detect specific mistakes in the codes before the program is executed. Programmers can also use programs called debuggers. A debugger can run a program step by step and track how values of specific variables change when the program is running. Programmers can use a debugger to find where the mistake happened in the code.

References

[change | change source]