ADC - Basics of Programming Languages
ADC - Basics of Programming Languages
https://alreflectionsdc.blogspot.com
Membership operators
In Member of
Not in 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
true if any of the statements is true and false if 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()’.
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;
https://www.facebook.com/mihigoanaja
https://iblink.ning.com/members/mihigoeranaja