A computer language is a language that can be understood by the computer. A computer programming language is any computer language which can be used by programmers while interacting with the computer or writing the instructions for the computer.
1.1. Examples of programming languages.
* Nuts
* Html
* CSS
* JavaScript
* C++
* Angular
* Basic
* VBS
* VBA
* Kibol
* PHP
* C#
* Lua
* FORTRAN
* Perl
* Java
* Python
* C
* Rust
* Aml
* COBOL
There exist thousands of programming languages you’ll only learn what you need. You’ll find that some programming languages are almost the same, where learning one will open your mind like you’ll find it easy for you to learn more.
1.2. Variables
Variables are containers for storing the values.
For example;
num1=3
name=“john”
In the example above, ‘num1’ and ‘name’ are variables whereas ‘3’ and ‘john’ are values. If X is a number, then that number is a value for a variable called X.
Variables can be divided into alphabets, numbers and Booleans. The value for Boolean variables are true and false. For the alphabets, there are strings and characters. Character variables hold single character while string variables can hold multiple characters. Not all character values are letters but characters are among alphabets because of their behaviours. For if 1 and 2 were values of two character variables, then adding up those variables will give 12 not 3. In such case, twelve is a string not a number. A single character can be assigned to a string variable; the only difference is strings are able to hold multiple characters at once. In languages like C++, character variables are surrounded by single quote while strings are surrounded by double quotes. For numbers, there are; integers, floats and doubles. The values for integer variables are only numbers without decimals while the values for both floats and doubles are numbers with decimals. The difference is doubles are longer floats. For example, float variables can hold values like 13.14 while 13.1428 is an example of a double value.
While coding in languages like C++ and Java, you’ll always have to specify the type of the variable to create any. There are some other languages you won’t need to specify the variable type, in this case its detected automatically. Before making any calculations, if the default type is string, you’ll need to convert from string to integer, float or double.
1.3. Operators
Arithmetic operators
+ - * / % | Addition Subtraction Multiplication Division Modulus |
Assignment operators
= += -= *= /= | Equal Plus assignment Minus assignment Times assignment Divide assignment |
Relationship operators
== > < >= <= | Equal Greater than Less that Greater than or equal Less than or equal |
Membership operators
In
Not in
Member of
Not member of
Logical operators
! | Not |
|| | Or |
&& | And |
These are the common operators used in computer programming. The modulus operator divides two numbers and gives the remainder. Both the logical operators and the relationship operators can be used to test statem0065nts or compare. The relationship operators and logical operators give the boolean values either true or false.
The NOT operator tests statement to give true if the statement is false and false if the statement is true. The OR operator tests two statements and gives trueif any of the statements is true and falseif both statements are false. The AND operator tests two statements to give true if both statements are true and false if any of the statements is false.
1.4. The if statement
The if command is used for conditional operators. In most of the programming language, the if command can only work with the relationship operators. But there are some programming languages that support the membership and logical operators for the if command.
The below is an example of the if command usage;
if (“text or num” == “text or num”)
{
}
The above codes can work in languages like C++, Perl, C and Javascript. At execution time, the boolean values; true and false are treated as ‘1’ for true and ‘0’ for false. Now, remember the operators including membership operators and logical operators give boolean values as they execute. This is true for correct statement and false for wrong statements.
Now, how does the command work? The if statement possess the condition and a block of codes that get execute if the condition is true or does not give out ‘0’. If the condition gives a false boolean that block of codes is ignored. In most of the programming languages, the condition is to be put into paranthesis and the block of codes is to be put between the two braces.
The if command may also possess another part called the else statement. Normally, the if command is used to form the if statement. If the else statement is added, this is now called the if...else statement. The if statement possess a block of codes that get execute if the condition is true or does not give ‘0’ while the else statement possess a block of codes that get execute if the condition is false or ‘0’. The below is an example;
if (“text or num” == “text or num”)
{
}
else
{
}
According to our example, the if...else statement possess both the positive and the negative part.
if (“text or num” == “text or num”)
{
positive part(1)
}
else
{
negative part(0)
}
Now, you’ll start using the if command very soon. The if command will help you make programs and websites limited by access, options or possibilities. You’ll be able to make programs and websites with security check up and priveleged access. If you like it, you’ll one day be able to make intelligent programs including calcutors.
1.5. functions
Functions are user-defined commands. They may even be called subroutines, procedures, callables or commmads. In different programming languages, there are different ways of creating and calling function. Though so, don’t panic; in this section, we will learn the most cmmon ways of creating and calling the funcions.
function hello()
msgbox “Hello World”
end function
The above codes can be used while creatimg a function called ‘hello’ which displays the message ‘Hello World’ in a dialog box once it is called. The above are vbscript codes and the below a JavaScript codes that can do the same.
function hello() {
alert(“Hello World”);
}
For vbscript, to call a function called ‘hello’ you’ll have to write ‘call hello()’ while in javascript, you’ll only have to write ‘hello()’.
public void hello() {
system.out.println(“Hello World”);
}
The above codes can be used to create a function called ‘hello’ which displays the message ‘Hello World’ once it is called. It works in java and to call it, you have to write ‘hello()’. Now, the bellow codes show how one can do the same in C++.
Void hello() {
Cout << “Hello World”;
}
To call the above function, you’ll have to write ‘hello()’. Now, let’s look at the codes above. The word ‘hello’ is the name of the function. The opening and the closing parenthesises indicate it a function and the brace specifies where a block of codes begins and ends. Very soon, you’ll know why void is necessary and other keyword that may replace void. The semicolon specifies where a line of codes ends.
1.6. Loops
In programming, a loop is a sequence of codes that get execute until a certain condition is no longer true. However, in computer programs, there are some tasks that require loops. While coding, programmers use loops to save time and to avoid making heavily coded source files.
loop-name (condition) {
block of codes
}
The above show the general structure of a loop. The loop possess a block of codes that get execute whenever the condition is true.
1.6.1. The while loop
The while loop is an example of a loop. In this section, you’ll learn more about the while loop. Let’s begin;
while (remain < 2) {
remain++;
}
The above example shows the codes for the while statement. The codes below shows an example of a loop that counts from 0 to 20 without displaying anything.
int remain;
while (remain < 20) {
remain++;
}
The above codes can work in both C++ and Java. The statement ‘int remain’ creates a variable called remain whose value is ‘0’. The ‘remain++’ statement changes the value of remain by 1 and as it is within the while loop, this happens repeatedly until the value of remain is 20.
Now, the codes below show how to do the same in Perl.
$remain=0;
while ($remain < 20) {
$remain++;
}
As you can see, there’s no big difference. Only that variables don’t look alike. For the ‘$remain=0’ statement, the dollar sign ‘$’ indicates that remain is a variable. In this statement, the value ‘0’ is assigned to the remain variable. assigning ‘0’ or any other integer to remain indicates that remain is an integer variable. The ‘$remain++’ statement changes the value of remain by 1.
int remain;
while (remain < 20) {
remain++;
cout << remain << endl;
}
Now, the above are codes of a loop that count from 0 to 20 while displaying the progress on the screen. These codes can only work in C++.
1.6.2. The do-while loop
There is no big difference between the while loop and the do while loop. Now, let’s look at how it work.
do {
remain++;
} while (remain < 2);
The above are codes of a do-while loop that can work both in C++ and in Java. Just as you can see, there is no big difference between the while loop and the do while loop. For the do-while loop, its instructions are executed atleast once; even when the condition evaluates to a false Boolean. The instructions are executed once before checking the condition. Now, let’s look at how this work in Perl.
do {
$remain++;
} while ($remain < 2);
1.6.3. The for loop
In this section, we are going to see what the for loop I like in the computer program codes. We are now going to look at examples of computer program codes for displaying the ‘Hello’ two times, using the for loop.
In C++
for (int remain; remain < 2; remain++) {
cout << “Hello” << endl;
}
In python
for remain in range(2):
print(“Hello\n”);
Now, as you can see; the way the for loop works changes by programming languages. However, the way this loop works in C++ is the same way it works in languages like Java, JavaScript and all other C languages. The for loop is what is used for loops the get execute a fixed number of times.
© Mihigo ER Anaja
Write to me for more;
0 Comments