kids encyclopedia robot

Computer programming facts for kids

Kids Encyclopedia Facts

Computer programming is like giving a computer a set of instructions to follow. These instructions are called programs. A person who writes these instructions is known as a programmer.

Computers understand instructions in different languages, which are called programming languages. There are many programming languages available. Programmers often use special software tools, like integrated development environments (IDEs), to help them type and edit their programs. IDEs have helpful features, including a text editor.

How Computers Understand Code

Computers understand instructions best when they are in a special format called machine code. This code uses characters that are hard for humans to read. Writing programs directly in machine code is very difficult and takes a long time.

So, programmers write instructions in languages that are easier for people to use. Then, the computer converts these instructions into machine code. The simplest of these "human-friendly" languages is assembly language. Programs written in languages that look more like English are changed into machine code by a tool called a compiler. Some languages use an interpreter instead of a compiler.

Storing Your Programs

Once a program is changed into machine code by a compiler, these instructions are saved in a file. This file contains numbers that the computer can understand. These files are usually called "executables."

When you open an executable file, the computer loads these machine instructions into its computer memory. This allows the CPU (the computer's brain) to run the program.

Using Variables in Code

A variable is a special placeholder in a program that can hold different pieces of information. Think of it like a box where you can store different things. Variables can hold information that a user types in, or they can store answers from calculations the program does.

Making Decisions with Conditionals

Conditionals are parts of a program that only run if a certain condition is true. The program checks if something is true or false. If it's true, the program does something. If it's false, it doesn't. This is often done using an "If Statement."

The "If Statement"

Here is an example of an if statement in the Perl programming language. It checks if the name variable is "Bill." If it is, the program will print "Hi Bill!".

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

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

Adding an "Else" Option

Sometimes, a programmer wants the if statement to do something different if the first condition is not true. This is called an "else block." Here is an example in Perl:

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

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

Multiple Choices with "Else If"

Programmers might want an if statement to have many options. They can use "else if" blocks. These blocks run if the first "if" condition isn't true, but their own condition is true. In Perl, "else if" is spelled "elsif." Other languages might use "else if."

An if statement can have as many "else if" statements as needed. If the main "if" block and none of the "else if" blocks are true, then the simple "else" statement will be used.

#!/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!";
}

Explaining Code with Comments

In a program, a comment is text that is only for people to read. It's not code that the computer will run. Comments have a special symbol in front of them. This symbol tells the computer to ignore that line.

Comments help explain how parts of a program work. This is very useful when many people are working on the same program. If a programmer leaves comments, others can quickly understand what's happening in the code.

Here is an example of comments in the C programming language. In C, two forward slashes "//" are used for comments.

#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, programmers might want to temporarily remove a line of code without deleting it. They can turn it into a comment. The computer will ignore it, but the programmer can still see and read it.

Here's an example in the Perl programming language. In Perl, the "#" symbol is used for comments.

$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.

Finding and Fixing Mistakes: Debugging

Computer programmers often make mistakes when writing code. These mistakes are called bugs. Bugs can make a program do the wrong things. Debugging is the process of finding and fixing these mistakes.

There are many ways to debug code. Software tools like text editors and IDEs can help find certain mistakes before a program even runs. Programmers also use special programs called debuggers. A debugger can run a program step by step. It can also track how the values of variables change. This helps programmers find exactly where a mistake happened in their code.

Images for kids

See also

Kids robot.svg In Spanish: Programación para niños

kids search engine
Computer programming Facts for Kids. Kiddle Encyclopedia.