Programming Without Codes
Programming Without Codes
Hitesh Choudhary
Published at Smashwords
1|Page
About the Author
Author of many books, articles, and online courses on the technical aspects of the Internet and the World
Wide Web, Mr. Hitesh has built a reputation for his ability to grasp complex technical topics and explain
them in clear, concise terms.
2|Page
Table of Contents
Preface 5
3|Page
________________________________________________________________ Table of Contents
4|Page
.Net languages: C# visual basic .NET 68
Ruby
Preface
The greatest and miraculous change profoundly marked its appearance was with the sky-rocketing boom
in the world of computers. And undoubtedly, the real power of magic relies in the handful lines of codes,
which we hear every day as ‘programming’. Everybody in this world should learn how to program a
computer because the actual impact of the coding falls over you; as it teaches you not just how to think
but how to think wide.
Most people don’t realize just how important programming is. Just about every aspect of our modern lives
is influenced directly by code. Today almost medium-to-large sized company either has a team of
developers or has to hire developers to do jobs for them. From web applications to mobile applications, to
DVRs and more; our lives run on code. The real benefit to learn programming is being able to automate
simple tasks and find new ways of solving problems. Learning to program trains your mind to think about
problems in a different way and breaks down the complex problems into smaller parts. And the biggest
hot-spot more than ever is the number of development jobs increasing to a point where it is impossible to
fill the demand. It’s pretty unlikely that demand for programmers will ever decrease and it is very likely
that it will only increase over the coming years. So, if you are looking for a great career field, with high
earning potential and great future prospects; you might strongly want to consider learning to code. It’s an
immensely creative activity which is replete with fun and excitement where you’re free to do anything
and everything.
This book will help you out understanding each and every concepts related to programming without
codes. This is an essential read for all the early beginners as well as the experienced programmers as this
book deals with concepts starting from a lower level of difficulty to the higher one and that too in a very
simple and a reader friendly environment. All those who start up with a mood of complex and puzzling
appearance of programming will certainly wipe out from mind and the reader will develop a special
interest in comprehending the basics behind the code.
Chapter 2, Introduction to variables and data types, focuses on the variables and data types that are
used in programming. It is very important to understand the usage and characteristics. Complete
elaboration on strong, weak and duck-type languages have been given in detail. This chapter also give a
brief about the backdrop of numbers in computers. Combinations of various operators have been
elucidated.
5|Page
Chapter 3, Basic elements of programming, introduces all the frequently used statements like ‘if’ and
‘else’, switch statement along with sufficient number of examples. This chapter gives you a very easy
understanding.
Chapter 4, Breaking the code apart, talks about functions used in programming. Variable scope is
another important thing to be taken care of while working with functions in most modern programming
languages. Here we also talk about splitting the code into different files.
Chapter 5, Introduction to Iteration, opens up with explains each and every aspect of Iteration with its
meaning. Usage of Iteration has also been detailed with simple examples. ‘for’ loop in programming
includes some examples which fulfills the sole idea of conception.
Chapter 6, Introduction to string concatenation, helps you out in the usage of string in programming
with proper and easy examples. We have also included a short analysis on an incident which will enhance
you understanding concerning the subject. This chapter gives a brief about global property and pattern in
string. Detailed introduction of regular expressions have been given.
Chapter 7, Working with arrays, discusses about the complete concept of array and its implementation
in programming. Cases have been segregated for different steps while forming an array. A very smart
example keeping tract on its analysis has also been included. This teaches you to understand the
behaviour of n array.
Chapter 8, Style in Programming languages, opens up with giving you a brief about what style means
in programming. How style varies in different languages is detailed in this chapter. A very basic and
interested concept of pseudocode makes thing clear to you. As in other chapters here also we have taken
certain cases for reader’s easier understanding.
Chapter 9, Input/Output and persistence, deals with the inputs and outputs in modern and traditional
programs. Event driven programming is detailed in this chapter along with sufficient examples. Other
languages have been taken into account for input/output files.
Chapter 10, Introduction to debugging, opens up with a very interesting topic of this book. It tells you
about the general debugging logical errors in programming. Gradually it takes to you the syntax to
understand it in a complete package.
Chapter 11, Introduction to Object Orientation, talks about the reason of using object orientation. This
chapter brings up the concepts from the ground level discussing about what object is actually referred in
programming. A short analysis on real world objects has also been done.
Chapter 12, Memory Management across languages, talks about what memory is in terms of computer
and how wisely it is managed in different languages. How memory is allocated to the object is elucidated
in this chapter. Memory management in low & high-level languages are also included. It also introduces a
brief idea about bubble sort along with short programs.
Chapter 13, Most renowned languages, makes the reader to dwell slightly into where C languages can
be employed and very briefly what functions are used in it. Then, it includes basic concepts of Java, .NET
6|Page
(C# and visual basic.NET), Python and Ruby with simple examples. Short resemblance between these
languages has also been introduced and succinctly explained.
7|Page
1
Programming without codes
What is programming ?
What does it strike to your mind when you hear of programming? You might have come across such
question many a times. In simple terms, a computer program is a set of instructions. But this might sound
is bit problematic because when you hear of complex program like Photoshop or Flash or some software
playing high-definition video or a 3D game. Of course, that can’t be simply a set of instructions. But it
revolves around the some part of instructions and codes. There stands no doubt that every computer
program we talk about is a sequence of instructions. There are separate small commands following in a
sequence. There is no fixation or limit over the number of instructions. It can be either five or 5,000 or
may be it can reach to 5 lakhs as well. Every instruction indicates towards the performance of small but
specific task. The knack lying behind programming is: when you are given a lengthy task to perform then
the systematic way to break it apart into smaller sections and consider it as individual steps. The
surprising element here is that we all come across such situations in our daily lives and resolve them in
the similar way.
The thing which plays the vital role here is: sequence. If you’re directions get jumbled up then you’ll
receive false results. You might confuse your friend if you’re instructions aren’t clear and easy to
understand. Ambiguity in the sequence in the instructions can take you long hours or might still not reach
8|Page
the real destination. All your need to be specific is with your sequence and also the particular landmarks
that you mentioned would be of great help for your friend.
Now extracting out the core part from this example is that while programming we’re directing the
computer with specific and sequenced instructions.
Conclusion:
The whole story encircles the trying to break apart a complex task into smallest individual sections and
making use of programming language in writing those instructions. Undoubtedly, if have never been too
much into programming then, you would probably not known to those instructions and how they are
framed up. Of course, while working on your computer you can’t give instructions like ‘turn left or right’.
Now the question arises what can be those basic fundamental instructions to be given to a computer. In
fact, they are very basic ones. Tasks like addition of two numbers or changing the color of the individual
pixel or displaying a letter on the screen. It’s important to give enough instructions to the computer to get
your task done.
Take an example:
You can compare it with the example of a friend that we mentioned above. If his car has only two speeds:
0 and 5000 miles an hour. Your wrong instructions can make him drive down to some wage place and
just result into a complete wastage of time. The same applies to computers. They exactly what you tell
them, so the instructions you give should make sense in order to get the task done flawlessly.
9|Page
must have noticed some programming languages end up with a semicolon after the completion of each
statement, same as we end a sentence in English with a period.
There are some languages which make use of all uppercase, some can make use of all lowercase
characters, though there are some which follow their own set of instructions. To understand the rules of
programming languages to need to understand and at least memorize the syntax they have. It is necessary
to know how to write those pieces of instructions in the programming language that you’re making use of.
Writing the statements in the right order and using the right syntax hold a vital importance.
In simpler terms, programming is the ability to deal with the idea coming out of your mind and
then taking it apart into its individual pieces.
The changes in the software world are gradually come into shape. Once you’re clear about the basics of a
particular programming language; it becomes easier to pick up the other additional languages.
Consider the CPU, the chip, the central processing unit which is the brain of any computer, desktop,
laptop, server, phone, game console. All of these do not understand the programming languages that we
set to perform specific tasks on the computer. Many a times we might hear somebody saying that he’s
writing some codes for the computer, but it’s an amiss. The thing to mark out here is that the chip only
understands machine code or machine language. These are the actual instructions that run directly on the
computer hardware. It is not possible to just write machine code despite of tricky programming
languages. It reason behind this is that these are numerical operations which perform small instructions
inside the computer memory. Such instructions are only readable by a machine and are not meant for a
human being. There are different machine codes for different models of CPU.
10 | P a g e
required to have a detailed knowledge of the hardware. This came to be known as low-level languages.
To overcome the drawbacks of low-level languages a new language was introduced; which came to be
known as high-level languages. It is quite easier working in high-level languages and can be shared across
different platforms. The only limitation with these languages is that they run slower. High-level languages
hold a lot of importance and are widely used now-a-days. The thing to keep in mind is that it is important
to convert high-level languages to machine code.
Before dwelling into the core part of programming it is necessary to understand three basic things.
• how to write
• how the source code gets converted to machine code
• how to execute the program
Writing statements & source codes
Let’s see how to write statements or source codes. Programming language’s source code is written in
plain text. The following points will help you out in understanding easily.
• Open up a simple text editor that comes with your operating system (Notepad on the
PC or TextEdit on the Mac).
• The file extension is very important. Example: .js for JavaScript and .groovy for
Groovy files or .txt for text files.
• If you're writing code in a program where you see a formatting bar with bold and
italic options, then that probably not the place for writing codes.
Programming language source code does not need to be bolded or underlined or italicized or
justified. When you’re using a program like TextEdit on a Mac, it has to be a plain text.
There are some one statement programs which are written in a language called ALGOL 68. It was aired in
1968 giving it the mane ALGOL 68. The other very renowned languages are Python and Lua. The
important thing to note here is that though the statements in these languages are same but that doesn’t
mean that are identical. There are some languages which require a little more than one single statement to
be considered as a full program. There are many other languages which are given explicit starting and
ending points. An older version of ALGOL, ALGOL 60, was written in all caps and required the words
BEGIN an END to mark out the program.
ALGOL 60
BEGIN
DISPLAY(“Hello,world!”);
11 | P a g e
END
Languages like C and other languages which are based on C like C#, C++ and Java requires curly braces
and weird esoteric keywords.
C
#include<stdio.h>
int main(void)
print(“Hello,world\n”);
return 0;
C#
using System;
class Example
Console.WriteLine("Hello, world!");
Java
public class HelloWorld
System.out.println("Hello World!");
Text editor’s are most widely used among programmers as it provides a user-friendly environment
comprising of many features. Some of the typical features are line numbers, the most widely used Find
and Replace. They often have color-coding. Remember that it is not the same as formatting. Color coding
happens automatically and it helps you to read and recognize different parts of the language. They have
syntax checking as well. This is very much similar to spell checking in a word processor. Syntax checking
12 | P a g e
will help you out in rectifying if there’s something wrong in the code that you’ve typed in. Sometimes
these text editors are oriented towards a particular language only.
Integrated Development Environments or IDEs
An analysis on web development brings out that the chosen web development application is also a usable
programmer's text editor. The very important term which comes out of this is the Integrated Development
Environments or IDEs. These are large programs which include a good programmer's text editor but
usually they are incorporated for professional development. Some examples of IDEs are Apple's Xcode
on the Mac or Microsoft's Visual Studio or Eclipse which is a cross-platform. You are not needed to
concentrate on IEDs at this particular stage; they are mostly used in specific area of development. At this
particular stage you need to learn how to convert your coding into machine code so that it can run on the
computer itself.
Keeping in view the usage of the compiled language, you’re writing the source code. You have a program
called a compiler that will go through that source code and create a separate file that contains the machine
code. These files are sometimes referred to as executable files as you’re utilizing them to execute the
program. Now you gave me that file to run it on my computer. I’m unknown with the source code; it’s
safely kept with you. On the other hand, the other situation is with an interpreted language you don't
compile the source code before sending a copy to me. My machine will be required to interpret the source
code whenever I want to run your program.
One thing is clear that interpreter is different from a compiler. Interpreter will have to go through your
source code line by line and processing it at the same instant. It does not save it as a separate machine
code file. You might wonder that you’ve used interpreted languages even if you don't know it.
13 | P a g e
• Once it is compiled, it is immediately ready to run and you can send it to 100 or
1,000 or 100,000 different people.
• It can be optimized for a CPU. This makes it much faster to work.
• You need not to send your source code to everybody, which might be a good thing.
Darker sides of the complied code are as follows:
• If you’ve compiled it on a PC, the executable file won't work on a Mac. It is required
to be compiled separately for different kinds of CPUs even when you’re working on
the same platform.
• Every time you're writing the code; to compile is an extra step that you need to take
to test your program.
Benefits of interpreted code are as follows:
• You need not to care for the kind of machine because you don't have to provide
machine code. Your work ends after sending the source code.
• It is portable and flexible across platforms.
• It is easier to work with. While testing it is just required to write the source code and
then run it.
• No worry about conversion of source code because that the load of the interpreter.
• There is no in-between compile step.
• It can be easier to debug because when things go wrong you always have access to all
the source code.
Drawbacks of interpreted code which over-shadow the benefits:
• Everyone who needs to run that program on their machine has to have an interpreter
for that language on their machines.
• It can be sometimes slower because you have to interpret it every time the program is
required to run.
• The source code is public because you're sending it to everyone who needs to run that
program.
Just-in-time or JIT:
Analyzing the benefits of both the complied and interpreted language we can extract out the advantageous
part from it. We’ve noticed that in the complied model all the work is done upfront but that’s a bit
inflexible. Secondly, switching over to the interpreted language, all the work is done on the receiving end
but that is a little slower. Considering the upfront where we compile is called an intermediate language.
This can be treated as machine code while still being portable across different platforms.
It can be sent to the people who need to run it. Each person who runs it takes it the last step from where it
is taken to the machine code on their computers. This is sometimes referred to as Just-In-Time or JIT
compilation. This intermediate language is also known by the name bytecode.
14 | P a g e
According to theoretical analysis all computer languages can use any of these methods. C, C++ and
Objective-C are found as compiled languages and they require a compiler. The compiler can be
downloaded free of charge but they're mostly built into integrated development environment applications.
Languages like PHP and JavaScript; in fact most of the languages with the word script at the end are
usually interpreted. Renowned languages like Java, C#, VB.NET and Python make use of this
intermediate hybrid approach.
It does not make much difference to know whether the language is compiled or interpreted but the thing
to take into account is the language that they have chosen. If the priority of your program is absolute
maximum speed running on one single platform then you'll probably switch over to a compiled language.
On the other hand, if you're required to get your code accessed across multiple platforms then you should
quickly move down to an interpreted one.
If you want to build iPhone apps or Windows desktop apps or some dynamic website,
you need to firstly drive your attention towards the language choice and the after, the
language choice will determine whether you are needed to be compiled or interpreted.
15 | P a g e
2
Introduction to variables and data types
Whatever we try to perform or execute on the computer, it goes to the computer memory and occupies a
part of it. A name has to be given to it while the program is running. This occupied space is given a name
and then a value is stored into it. The stored value can be any fictitious thing like an email address or a
date or a number and any valid data. This data can be changed whenever required. As variables can vary,
thus it is named so. While programming it is needed to create a variable like this. The name of the
variable is up to us but it advised that it should be relevant to the piece of data that it going to hold.
Take an example:
Let’s consider a variable named year or let it be customerEmail or todaysdate. Many a times you’ll notice
non-relevant words as well; may be sometimes just a single letter like a. While we’re creating a variable,
we’re carving out a small area of the computer’s memory to hold a value.
int year;
var x; ($)
16 | P a g e
• There are no spaces allowed in between the word.
• It can be made of letters, numbers, the dollar sign and the underscore.
• No other characters are allowed but it can't begin with a number.
17 | P a g e
Undefined variables:
Suppose you’re writing a line of code for a variable to exist. Though it has a name ‘year’ but it doesn't
have a value right now. Such variables are regarded as undefined and undefined variables have a special
significance in programming. But there is no point in keeping a variable that stays undefined. Thus, we
need to define or set the initial value of the variable at the time we create it. It can be done by two
statements. Firstly, you need to declare the variable then after put or store some value to initialize the
variable like year =2011.
var year;
year = 2011;
This statement puts the value in the variable or we can say we’ve initialized the variable. This is actually
a command instructing to place the value 2011 in the variable called year. Both of these statements can
also be combined into one statement; simply like defining or declaring the variable by creating it and at
the same time initializing it to some value.
The’ = ‘sign sets the variable to the initialized value.
x = 200;
X = 210;
Similarly if you're creating multiple variables and assigning them separate initial values; it still can be
combined onto one line. This makes it easier to read and shorter to write. As a programmer, once you've
created a specific variable you can store any value into it. It could be numbers, text or dates.
int year;
int month;
int day;
OR
int year, month, day;
OR
18 | P a g e
int year = 2011, month = 10, day = 31;
Data types:
Most of the programming languages have their own sets of principles to be followed while writing a
program. While creating a variable, you don't just give it a name but it is also necessary to tell what type
of information is going to be stored in that variable. It must be decided beforehand if you’re going to store
an integer which means a whole number having no value after the decimal point. It can also be a floating-
point number which can have a value after the decimal point or may be just a single character. When
you’re storing multiple characters in a variable, it is called as a string. If it is a Boolean value then it can
have a value that can be either true or false. Hence, it is important to define the data type before storing
the value in the variable.
Strongly-typed language:
Once you have made a choice among various data types, it is not allowed to change it. This is known as a
strongly typed language. Many variables can be created in a particular program but it is necessary that
each variable must be of one particular data type. It can crash your program and produce many flaws if a
wrong data type is stored in the variable that wasn't designed for it.
Weakly-typed language:
There are some weakly-typed languages as well. It is only required to create variables for integers and
strings. We just create a generic variable or you can say an empty container. It can be simply used to put
any type of value. If you create this variable without assigning it any value then it would starts off as an
undefined variable. However, you can use the name of the variable and an assignment operator to set the
value of x = 200.
x = 200;
x = “Hello World”;
x = ‘Hello World’;
x = True;
Many of the programming languages make use of either single or double quotes to
mark the start and the end of a string.
Boolean values can also be stored which says either true or false. The word true or false does not need the
quotes around them. They are considered as a part of the language. Thus, an undefined variable can be
stored with a number or a string or a Boolean value.
19 | P a g e
Weakly-typed languages are easier to work with because they are user-friendly and give a liberty to put
anything anywhere. The only problem with weakly-typed language is that there is no guarantee whether
the variable declared contains the right kind of data. Many-a-times while programming, there is no need
to change the type of data that a variable stores.
Take an example:
If a variable is defined as high-score then it is understood that it would contain a number in it and not the
word apple or the value true. Another example can be a variable called firstName then it is itself
implicated that it should be a word and cannot contain the value say 25.8. The advantageous factor
working with strongly-typed language is that internal rules shun a lot of common errors from occurring.
Conclusion:
The analysis of the above examples made one thing clear that before making use of any variable, it is
important to know to write them.
The representation of the numbers can be seen in almost every task we do in our daily lives. Such as
dealing with bank balances in dollars and cents, the position of a spaceship on the screen, the height and
width of a web browser window, the coordinates of a device using geo-location, the amount of
milliseconds an MP3 song has been played. Even colors are represented with numeric ranges of red,
yellow and green.
Significance of numbers:
You need to be very conscious of what type of number you’re using: integer floating-point, positive or
negative. Let initialize the variable x to the number 5. If you’re new to programming, lines like x = 5 can
look queer to you because may be the last time you have seen this was equations in school studies. But
you need to mark out a point here that the equals sign here is just not a description but a command. It
simply depicts that place the value 5 in the variable called x. It does not matter what it contained earlier
but after execution of this line, 5 gets stored into the variable x. This is what the equals sign does. It
assigns a value and so it is known by the name assignment operator. You must have noticed that there are
no quotes around the number 5. The number 5 is referred to as a numeric literal. It represents the fixed
20 | P a g e
value 5. The variable part of this line can change as our program runs, but the literal does not. 5 have a
same meaning every time. If you want to change the number by a ten thousand then you can easily do
this. But make sure you do not put any commas in it. Such as x=1000000. Just make use of the number
itself. Numbers are regarded as positive by default though they can be negative as well.
X = 5;
X = 1000000;
We know that while placing strings in your statements, double-quotes are used to enclose the text or the
sentence. On the other hand, single quotes can also be used. But both of them cannot be used at the same
time. The situation when you require both of the formats can arise when quotes inside quotes are to be
placed.
X = “Hello World”;
X = ‘Hello World’;
Conclusion:
The solution which can be employed here is to use double-quotes along with a single-quote contained
inside them. This would be perfectly fine. Nevertheless, it may get a bit difficult when we’re dealing with
more complex sentences.
21 | P a g e
X = “Don’t mix your quotes”;
Conclusion:
Strings can considered as the holding place for some characters. We can put questions inside the strings
as well. It can be even converted to either upper or lowercase. One thing to always remember is while
defining the name of the variable; it is suggested to define it in such a way so that it reflects the
information inside the string. Variables give us a lot of information about the contents and regarding what
they're holding.
balance=500;
Let’s find out few more such operators which are widely used. Most common among all is arithmetic
operators such as addition, subtraction, multiplication and division. Asterisk is used for multiplication and
the forward slash is used for division. Many a times, they are used in combinations as well giving it a
special meaning. Considering an example of a variable a = 100. Whereas variable b is set to 50. We can
make use of the plus sign on the right-hand side of the assignment operator to perform an addition
operation. Adding up the two values in two different variables and saving the result in a new variable
called result. The result comes out to be 150.
a = 100;
b = 50;
The plus operator could be changed to a minus sign and perform subtraction operation. In the same
manner, we can easily apply asterisk for multiplication and the forward slash for division. The
significance of assignment operator is that it assigns the value present at theleft-hand side of the equals to
sign to the variable at the right-hand side. Multiple operators can be used together butsome of theoperator
symbols are treated to be more important than others.
Combinations of different operators:
Suppose you have two values in two different variables. Applying addition operation makes it 5+5 i.e. 10
and the multiplication operation would make it 10*10 i.e. 100. But if the operators are applied in different
positions it can yield very different results. If both the operations are employed simultaneously then again
the result would come out to be different as multiplication holds a higher priority and would be performed
earlier than addition.
22 | P a g e
Now suppose if there are same variable names on both sides of the equals sign then the current value of
score would be taken into consideration. Later on, the result would be stored in the variable score. If the
score is 100, it will now turn out to be 110. This idea to add a value to an existing variable rather than
creating a new variable is very common and so there is a shorthand for it which is +=. Score += 10
implies that whatever the value of the score variable is; add 10 to it. Here the + and the = have to be
written as one.There is no space in between them. This is considered as one operator. Similarly, we have
-=, *= and a /= for subtracting a value or multiplying a value or dividing a value respectively.
If we need to just add 1 to an existing variable, then we could evaluate it either by writing a = a + 1 or by
using a shorthand a += 1. But the usage of an increment by 1 to a variable is so common that it build up
its own shorthand which is ++.a++ implies add 1 to the variable a. This is how the language C++ got its
name from. It's the impression of C+1 depicts the next version of C. Falling on to the other sides, the
same idea we utilised while subtracting 1 from a variable by writing it long hand as a = a - 1 and later on
build up a short-hand for it as a -= 1 or just a--. The ++ is called the increment operator whereas, -- is
called the decrement operator. Thus, it is clear that these symbols have equal importance within a
programming language as the words themselves.
a = a + 1; a = a - 1;
a += 1; a -= 1;
a++; a--;
Whitespaces in programming
Most of the modern programming languages do not get affected by whitespace. Whitespace are simply
spaces or tabs or line breaks. These are considered as insignificant and can be avoided. It does not matter
if the line breaks are placed in between the lines whether at the start, at the end or in the middle. It does
not hamper the result if you decide to put some tabs or few spaces before staring the statement. The
whitespaces are ignored and the characters are being read. The ignorance of whitespaces is possible
because of semicolon which implicates the end of the statement in programming.
Usage of whitespaces:
Whitespaces are useful when multiple statements are combined onto one line. It does not pose any
problem writing it this way. But it is suggested to make use of one statement for one line. In fact, the
whitespaces are avoided inside the statements. You can put multiple spaces between different elements of
same statement. It is advisable not to create a mess out of it by using multiple statements or multiple
spaces in the same line.
While writing in C-based languages, the programmer intends to immediately close with the semicolon as
soon as possible. It is similar to the way we use the period at the end of a sentence in English. You may
use white spaces for readability. Using too many elements in the same statement can be hazy to read.
Whitespaces are used to clear it out.
Whitespaces in operators:
23 | P a g e
There are few customs that you may run into. Let’s suppose we’re using the ++ operator. There are two
places where whitespaces does matter. Firstly, while making use of an operator that has multiple
characters like the ++ operator. You can put a space between the variable name and the ++ and the other
way is you may put a space between the ++ and the closing semicolon. You cannot put a space in between
the two. Don’t get confused; this is not two addition operators. It is one ++ operator which is also called
as the increment operator. It has an ability to add one to a variable. There’s no space in the middle of that.
Similarly, the same thing goes for operators like --, += or -=.
a = 5;
b = 10;
c = 20;
x = true;
z = false;
a++;
b--;
c+= 10;
Whitespaces in strings:
Secondly, whitespaces matter a lot inside a string. Whitespaces are placed between the words that are
there in the string. Whitespaces will hold a lot of importance when you’re required to make a line break
inside a string. But you can’t just simply place a whitespace to split up in two lines because in the end
you’re winding up the entire statement with a semicolon. The only solution to make this happen is to
make use of backslash. This backslash is sometimes referred to as escape character. Those special
charters are \n; this simply implies a new line and used as a string literal.
Representation of comments:
They can be easily applied by simply writing a line that begins with two forward slashes. Anything after
these two forward slashes is avoided making it a single line comment. Soon after leaving that line the
comment is over and the rest of the code is unaffected. The other way of applying the two backslashes is
after the existing line. But is mostly advisable to place it before the code it is meant to be applying for.
// This is a comment
24 | P a g e
Sometimes a blank line is also observed before the comment because it makes it more visible. Multi-line
comments can also be created. Despite of using multiple lines with two forward slashes, the other way is
to use a /* when beginning a comment and ends up after the appearance of */. Multiple lines of comments
are mostly used for large lumps of coding. Though often two backslashes are used for commenting but
some programming languages have their own syntax of writing comments for codes. Thus, it is always
advisable to check out the syntax of the particular programming language you’re writing the code in.
25 | P a g e
3
Basic elements of Programming
Take an example:
Let’s imagine you’re writing a bank balance program. But during programming there are few questions
which need to be a part of your programming. If the bank balance is positive, calculate interest. If it
comes out to be negative, charge a penalty. The other condition could be: if they check the checkbox on
the form, add them to the mailing list and if they haven’t then don’t. There can be many other conditions
like if the position of the missile image is exactly similar to the position of the spaceship image, then
flaunt the explosion image.
There is a need of such a programming which deals with the conditions as well as a part of the code while
executing. In other way round it can be said that a code that sometimes runs and sometimes doesn’t with
respect to the conditions of the program at that time. Such a code is conditional code. And so, we
introduced if statement which exist in every programming language.
The word ‘if’ is used in the statement to declare the conditions. Along with the word ‘if’, there is
a pair of parentheses comprising of an opening and closing round bracket which contains the
condition part to be followed. Then after, an opening and closing curly brace which contains the
code part to be executed.
26 | P a g e
If you’re new to programming then you need to bear in mind that in programming languages, the symbols
used are very specific and holds a special and different meaning. For example parentheses indicate simple
round brackets and braces indicate curly braces.
if ( )
// ...}
Let’s imagine a condition where you want to check out the equality. Checking out whether something is
equal to something else, we cannot use the single equals sign. It will be required to use double equals sign
having no spaces in between. This is known as comparison operator. The condition can be whether ‘c’ is
equal to 999. Again we’re just focused in finding out if the condition comes out to be true or false. This
comparison operator is used many renowned languages like C, C++, C#, Java, JavaScript and many
others. Don’t get perplexed with the single equals sign which is known as assignment operator. Both have
very different meaning. Assignment operator is used as a command. It sets a value, not checks a value.
The condition to check inequality simply means something is not equal to something else. This can be
written as != . This is one operator. Let’s see the usage of it with the help of an example. The condition to
check out is if ‘d’ is not equal to 100. We’re looking for whether it is true or is that false. On finding it to
be true we move on to the code block. It is grouping a section of code together and executes after finding
the condition to be true. Code blocks can also be nested inside each other if it is required. It seems like the
code inside a code block is indented.
27 | P a g e
Working with complex conditions
Let’s begin with creating a condition and then adding some more thought to it. We’ve created a variable
called balance and settled its value equal to 2000. Let’s apply ‘if’ statement to it. The condition to be
applied is if balance is greater than 0. Now we can easily see that it is greater than 0. Of course, code
block will also be required quickly jumping to the opening curly brace and then running down through a
couple of lines to the closing curly brace. In the end, the code part prints that balance is positive on
finding the condition true.
There is a very important point to be noticed. If the condition comes out to be true i.e. greater than zero
gives us the output as balance is positive. But think about when it is equal to 0. It will give us the output
as the balance is negative. There might arise some problem with this because we haven’t specified any
condition regarding this. Computer only does what we have specified in the code. It has no understanding
or assuming things of its own. In the case of equal to 0, we need to introduce another condition. Such
problems arise when we dealing with positive and negative numbers. It is advised to be as exclusive as
possible about what you're checking against. The condition should be if the balance is greater than or
equal to 0. As zero is not considered as negative so, the output will come out as the balance is positive.
You can also make use of an operator >=. This is regarded as a single operator.
More conditions like if balance is greater than 10000; can also added by introducing another ‘if’
statement and making it more complex.
balance = 5000;
if (balance > 0 )
else
28 | P a g e
To understand the usage of comparison operators let's example. We’re writing code by using ‘if’
statements and want to check whether one variable is equal to another variable. The comparison operator
is written as the double equals sign, also called as the equality operator. According to case, we’ll check if
the contents of the variable ‘a’ are equal to the contents of the variable ‘b’. If it is found out to be equal
then the execution of the statements present inside the opening and closing curly braces i.e. the code
block would be executed. The thing to mark out and remember forever is the comparison operator is
double equals sign and not the single assignment operator.
if (a == b) {
a = 5;
b = 10;
if ( a = b ) {
//always true!
In JavaScript and a couple of other languages like PHP, the equality operator is represented as the three
equals sign or triple equals sign. This is also called as strict equality operator.
= assignment
== equality
=== strict equality
29 | P a g e
grade = “Premium”;
print(“this is this”)
print(“this is that”)
print(“this is Something”)
In other languages, switch statement is called the select or select case statement. It has the ability
of listing several situations or cases in one place.
Format of ‘switch’ statement:
Now employ the same example mentioned above using switch statement. It is required to check if the
variable named day is Monday, Tuesday or Wednesday. It has a similar format to that of the ‘if’
statement. As in the ‘if’ statement we made use of the word ‘if’ along opening and closing parentheses for
the condition and the opening and closing curly braces for the code block. In case of switch statement,
we’ll make use of the word ‘switch’ (all lowercase). You should know what the switch is looking for. In
this particular case, it is the variable day. You need to put ‘day’ inside the parentheses. Neither it is
greater nor is it lesser than something. It implies that we’re looking for the variable called grade. The
code block inside the body of the switch statement describes the different situations or the different cases
that we're looking for as per the requirement of our program. The format goes like: Use the word case and
describe the first option. In this case, the day is equal to Monday. Following this, in the similar manner
describe other cases. There is no need to use equals sign or the double equals sign because we already
know about the variable that we’re looking at i.e. grade.
grade = “Premium”;
switch (grade)
Write "Premium" followed by a colon. Then after you need to describe what has to be done after the
selection of the particular case. If you’re checking out a variable that contains a string then the double-
quotes will be required. On the other hand, if you’re checking a variable that is a number then cases
would be like: case 1, case 2 and case 3 without putting them in double-quotes.
grade = “Premium”;
switch ( grade ) {
case “Regular”:
print(“this is this”);
30 | P a g e
break;
case “Premium”:
print(“this is that”);
break;
case “Diesel”:
print(“this is something”);
break;
default:
Make sure you do not get confused with the semicolon; we are using colon here and not semicolon. As
this format is quite readable, it becomes easy to scan and figure out what we're actually looking for and
what has to be done then after.
Benefits of ‘switch’ statement:
The major benefit of using the switch statement is that we can finish off the program easily. There can be
a situation when no option is matched among the cases mentioned. The word ‘default’ can be used when
no case is found. This is not in double-quotes. Do not get confused we're not checking the variable for the
default value.
Going through each of the statements describing cases, let’s suppose we’re looking for the variable day
which is equal to Tuesday. Moving on to the first case, we came across Monday which is not the right
match. Next, we move down to the second case and we got the match. It’s Tuesday. This will indicate an
alert sign. But because of the format set in the switch statement called fall-through will immediately jump
down and find the next statement to be executed. To prevent fall-through in our cases we’ll add the word
‘break’. This implies that we're done with this case. So at the end before the next case we write ‘break’.
Break keyword simply indicates to jump out of the particular switch statement. It is not required
to put the break keyword after the default statement because it will by default set to jump out of
the program.
31 | P a g e
4
Breaking the code apart
Introduction to functions:
Addition of more and more code makes the program quite messy, harder to read and harder to follow.
Most of the programming languages break apart the large amounts of code into smaller, reusable and
modular pieces. These are turned into smaller programs and have different names in different
programming languages. They are called modules or subroutines or subprograms or may be sometimes as
just routines or methods. There are few programming languages which call them as functions.
A function is simply the idea of extracting out a block of code from a large chunk of program. These
subroutines or functions can be of one line or five line or even more than that. It is a block of code
wrapped up together and given a name. These functions can be called later in any program and treated as
the one single function. These functions follow the verb-noun format such as calculateArea,
createMessage and animateImage. All of these functions encapsulates a block of code, no matter even if it
is just a single line and performs a specific task. These functions can be called anytime in any program.
Functions are small programs comprising of a block of data extracted out from large chunks of
code of a program specifying a particular task to be performed.
Format of functions:
The code is created surrounded by the curly braces depicting where the function starts and ends. The code
written in the block is called the body of the function. Collectively they can be called as functions but
since there are numerous such functions, it is required to give them a unique name. As was the case in
variables, giving names to the functions is up to us. Few examples of functions are createMessage,
hideMenu and there are many more. For easy understanding you may consider it as myFunction.
After naming the function, the opening and closing parentheses are required. If there is some data or
parameters or arguments to be passed into the function; then it is placed inside these parentheses. Empty
parentheses simply imply that there are no such parameters or arguments to be passed. It can be called in
a program by using the name of the function followed by the empty parentheses and a semicolon. This is
how the body of the function is called and all the code inside it is executed. There is no bondage on using
the function more than once in a particular program. It can be called many a times.
32 | P a g e
Functions in programming:
The important thing to remember is once you’ve created a function it won't be called automatically. It will
be required to call it. If you have one or more functions in many programming languages it doesn't make
any difference officially where the function was written. It can be written in any order. It is required to
call the function and then define it. That is why almost all programming languages quickly scan the code
and figure out the function existing behind that code before it tries to run or execute it. It is important to
define the functions before calling any of them. Define all the functions up at the top of the code file. This
is considered as one of the rules to be followed while programming.
function ( ) {
// ….
//sometime later
myFunction ( );
myFunction ( );
myFunction ( );
Either termed as functions, modules, subroutines or methods are callable units inside a larger program and
comprising of small block of code is a core feature of all programming languages. This makes the
programs easier to write and understand. Also much of the memory and time is saved. You can
understand it by comparing with a live example where different programmers are given different parts to
write and thus splitting the work making more organized and less complex. There are some complex
functions as well. They not just perform an operation but also return values or can also accept data.
myFunction ( );
function myFunction ( ) {
// lots of code
myOtherFunction();
function myOtherFunction ( ) {
// lots of code
33 | P a g e
Understanding variable scope
There is another important thing to be taken care of while working with functions in most modern
programming languages. Let’s imagine we’ve created a function and defined a variable inside that
function. This variable only exists inside that function. This has led to the introduction of variable scope.
Every variable has a scope. This depicts the location or the visibility of the variable. After jumping into
the function, we’ll create the variable ‘x’ and print the contents of that variable, let’s say it 500. Now after
coming out of the function if we try to print up the content of x again, it’s not going to show any result.
This is regarded as undefined. It is unknown to the x variable because x variable does not exist outside
that simple function.
Variables declared inside the functions are referred to as local variables. They have local scope
and are only available within the opening and closing curly braces of that function.
There is a keyword which helps out if we are required to make the contents or the local variables visible
outside that function. The value can be returned by using the Return keyword. Another way to access the
local variables of a function is to again declare and create the variable outside the function. The variable x
created outside all the functions is referred to as a global variable.
Global variable is that variable which can be used anywhere, anytime regardless of whether inside
function or outside the function.
The thing to keep in mind is not to use the word ‘x’ to declare the variable again. Inside that simple
function where we initialized the variable ‘x’ to 500; if we called the variable inside the function it would
be x=500. And outside the function it would be 500 as well. You need to be cautious about where the
variables are defined.
x ; (global variable )
function simpleFunction () {
// lots of code
// lots of code
print (x) ;
simpleFunction () ;
print (x) ;
34 | P a g e
programming language. Of course, it cannot be used anywhere randomly. It cannot be done like keeping
half a function in one file and half in the other. Several functions can be grouped together in one file and
several more in another. This makes it easier for multiple people to work on the same project by working
on separate files.
You might come across some complex applications built by software developers where they might have
hundreds or thousands of separate code files for one project. There’s nothing unusual in that.
35 | P a g e
5
Introduction to Iteration
What is Iteration?
Iteration can be treated as the programming catchphrase for a loop. Let’s find out the idea lying behind it.
You begin writing some code. You want to introduce some statements in your code implicating to look
into all the MP3 files in a folder and changing the position of an image on the screen. It is required to
write some statements to let it happen simultaneously. Now suppose you are needed to repeat it. Probably
you might think of writing it again and again as per the need. But all this will turn out to be quite tiresome
if imagine you need to repeat it several hundred times. This led to the introduction of such a function
wherein we write the statements once and then just say "Repeat this". Repetition can occur any number of
times or it can be set to repeat until told to stop or can be told to go through all the fields on a web page.
Thus, repetition of the statements is up to us.
Repetition of the particular statements can be done by creating loops and this procedure came to be
known as iteration. Creating loops is very easy. The important thing to be taken care of is not when to
begin any loop but when to stop. The loop can be prolonged to even 5,000 times or can be until told to
stop; it has to have something to keep a track of it. Therefore, all the loops will have conditions that
control how long the loop should proceed.
Usage of Iteration:
Most of the languages follow the same format in programming. We’ve seen in the ‘if’ statement if the
condition turned out to be true, execute the code in the block. Let’s take an example. There is a variable a
= 1. The condition is if a < 10 then print up value of ‘a’. Undoubtedly we can make use of the ‘if’
statement here. The way could be simply replacing the word ‘if’ by ‘while’ and this gives us a loop. This
implies while this condition is true, execute the body of this code rather than if this condition is true.
Regardless of how many lines of code is there in the body of the loop, it gets executed for each iteration.
Every time the loop goes around the code would be executed.
Infinite loop:
When variable is set to 1, the value gets stored in memory. Then after we move down to the while just
like it was there in the ‘if’ statement. According to this particular case, it evaluates as true. As ‘a’ is less
than 10; we quickly jump into the body of the loop inside the curly braces and executes the code out
there. Here, it is just popping up an alert. When we move down to the end of the loop, it is marked by the
36 | P a g e
closing curly brace. The control again goes back to the start of the loop and checks the condition again.
This happens automatically until a < 10.
37 | P a g e
Problem with infinite loop:
But there can arise a problem if the value of isn’t changed then it will keep on revolving making it an
infinite loop. Infinite loop is not advisable as it makes us stuck in the loop forever. At that time the only
option would be either turn off the computer or force the program to terminate. It will be required to keep
track on how many times we want the loop to be repeated. It is required to add a line to increment the
variable ‘a’. If we make use of an increment operator such as a++ and making it a part of the code; again
we get into the loop and check for the same condition. It prints the value on finding the condition true or
executes whatever code is there. If we write a++ just before the body of the loop closes, it will keep
incrementing the value of ‘a’ by one.
The value of ‘a’ will be now 2. This is how the loop will keep going, incrementing the value of ‘a’ and
checking out the condition. At some point the value of ‘a’ will reach 10. Checking it out we’ll find out
that it is not less than 10. As the condition comes out to be false, we’ll quickly jump out of the loop
without executing any of that code.
38 | P a g e
loop is executed. Just before the closing curly brace we'll increment the index. This will check the
condition again and keeps on looping until that condition turns out to be false.
// do stuff
// do stuff
// do stuff
i++;
The benefit of it is that it makes the things go in an organized and readable format. Everything about the
loop is written at the top. It is not needed to look outside the loop for finding the index being set up nor is
it needed to scan inside the loop for the increment of the index to make sure if it’s in the right place or
not. It’s quite clear and to the point way of setting up a loop. Such type of coding is very common and
frequently used online and in books as well. ‘while’ and for loops are often used in most of the manual
looping. These are same across all C-based languages.
Other loops in programming:
Other than ‘while’ and ‘for’ loop, the other loops which are used in programming are ‘do’ or ‘do’/‘while’
statement. The basic difference between these and the ‘while’ loop is that in the ‘do’/’while’ loop the
condition is moved to the end of the block. The format of ‘do’/‘while’ is completely different. It doesn’t
match to the formats we’ve seen yet. But the elements are still in the same place.
Take an example:
Let’s set up an index a = 1. We’re using keyword ‘do’. The code will be written in the opening and
closing braces just after ‘do’ keyword. The code block also contains the increment operator of a++. But
the condition that we’re going to check is at the end in the ‘while’ loop i.e. a < 10.
a = 1;
while (a < 10 ) {
print(a);
a++;
Same as in ‘while’ statement we need a semicolon at the end of the ‘do’/’while’ statement. The contrary
part between the two is that whatever is there in the code block will be executed once before we have
gone through the condition. Even if the condition was false when ‘a’ started off at 100; the code in the
block would be executed before the condition is checked.
Mostly ‘while’ and ‘for’ loops are much more common than ‘do’/’while’ loops. It requires checking the
condition before entering the loop. This makes it more readable to have the condition at the top of the
loop than at the bottom.
39 | P a g e
6
Introduction to string concatenation
Take an example:
Let’s take an example to understand the difference. There are two variables named as foo and bar.
Suppose the variable day is initialized to5 and amount is initialized to 5 without double quotes. These are
numeric values. If you want to print the result after adding them up, you’ll make use of addition operator.
The outcome will be 10.
foo = 5 ;
bar = 5 ;
foo = “5” ;
bar = “5” ;
Conclusion:
The important thing to mark out here is the values stored in the variables were numeric values which were
easily understood by the addition operator. It adds them together.
40 | P a g e
foo = 5 ;
bar = “5” ;
foo = 5 ;
bar = “b” ;
Conclusion:
This time the result would be different. The result would be concatenation and not addition. It will put the
two together i.e. beside each other and give the output as 55. Such a result is the behaviour of the
concatenation of string. Even though the value stored was numeric, but due to the double quotes it was
treated as string and not a numeric value.
You might face few situations where you try to store such values in the variables that just do not make
sense. There are two variables boxes=5 and we create other variable packets equal to the letter A. We try
to print boxes times packets and then boxes multiplied by packets. Concatenation it doesn't work here.
The result might come out as five times A i.e. A then A then A till five times. Such a result may be or
may be sensible that you were expecting.
41 | P a g e
which are commonly known as functions. We've already studied about functions which are called by
using parentheses. Methods are almost similar to functions that belong to a string.
Again create a variable called phrase and use the dot operator phrase dot to access all the built-in methods
like UpperCase. Just like a function it can be called by using the parentheses. As a result, the string will
get converted to uppercase. Similarly there are many such behaviour that string has. These characteristics
or behaviour of the strings are changed from one programming language to other. Strings are quite
powerful and have various built-in behaviours like converting to uppercase or converting to lowercase.
This allows us to certain pieces of methods within them. The thing to understand about strings is that they
are a bit complex than numbers because we can have strings using different cases.
42 | P a g e
Analyzing different cases of string:
Case 1:
Create two variables. One of the variables contains the word “Hello” with an uppercase H and the other
variable containing the word “hello” with a lowercase h. Now find out if they are equal to each other. Of
course, they are not because uppercase H and lowercase h cannot be treated the same way.
Case 2:
Suppose you have no consideration about whether it is uppercase or lowercase and want to have a case
insensitive comparison. You just want to find out if it contains the same word. You cannot check it in a
very simple way. You need to make use of the methods of strings to do the comparison between the two.
It is required to convert both the strings to the same case and then do the comparison. What you actually
have to do is like: convert str1 toLowerCase and str2 toLowerCase and now ask whether they are equal.
The outcome now will be like: Yes, they are equal. Such abilities of the string help us to perform many
such tasks as per our need.
str1 = "hello";
str2 = "hello";
// str1 != str2
if ( str1.toLowerCase( ) == str2.toLowerCase( ) ) {
Case 3:
We can also find certain words that exist inside the strings. This is often employed in strings. Let’s again
create a variable called sentence and use the indexOf method of the string. Now make use of the same dot
operator and then indexOf. You can clearly see that it is a lowercase ‘I’ and an uppercase ‘O’. Let’s pass
another string into it. In this case, word is Elegant. You want to know the index of elegant in the string.
This actually means that if the word Elegant exists in the string where it begins from. Now the output will
be like if that word exists in the first string, it will return the position of the word and that's zero based.
The first letter of the string which is the uppercase W and will be positioned 0. Elegant will begin at
position 10. If that word is not found in the string, it will return -1. Taking another example if the certain
phrase or word did not exist, you could call sentence. If passing in a DDDD and then applying indexOf
method. We know that does not occur. If the result is -1, you could print a statement that says, ‘No, this
word does not exist in the string.
postion = phrase.indexOf("groovy"); // 10
43 | P a g e
// it returns -1 if the term is not found
if ( phrase.indexOf("DDDD") == -1) {
44 | P a g e
Other possible cases:
There are dozens of ways that you can combine these different kinds of operations to find out if the word
exists in a string. There is a method called lastIndexOf. Dot lastindexOf gives the last position of the
word in a string. Whereas, dot indexOf gives the first occurrence of the word in the larger string. If you’re
working with larger amounts of text, you can also break a string apart. There are several methods to do
this. One way to do this is to use slice method. Create a variable called phrase and give it some basic text.
Next, create another new variable called segment which is based on the part of phrase. Use phrase dot
slice and slice expects two arguments. In this particular case, pass in 6,11. There is a significance of this
numeric value. The first number is the position that we are going to start at in the phrase. It starts from 0.
Therefore, it becomes 0123456. 6 is the position we’re going to start and 11 would be the position we are
going to stop.
segment = phrase.slice(6,11);
Ultimately, we are getting 6, 7, 8, 9, 10. This is how we can extract out those letters and store that result
in the second variable. It can be simply visualized as pulling out a section out of another string. There are
other methods like dot substring() or dot substr(). It’s one and the same thing. It gives us a couple of other
options as well. Other than slice, there are other options as well to perform this task.
This can be seen in all modern programming languages. Other than comparison and equality check; it can
be used for greater than and less than. Let’s take an example to understand this. We have got two strings
here: aardark and beluga. Write an ‘if’ statement to check out whether if (str1 < str2). It will begin
comparing the first letters. According to this case, you intend to find out if a less than b. This is almost
like doing a phonebook comparison. The result is true here i.e. first string is lesser than the second string.
str1 = "aardvark";
str1 = "beluga";
str1 = "aardvark";
str1 = “Beluga";
45 | P a g e
case you want to change this or mistakenly you have written uppercase B, again you will have to use the
ability of the string to be converted and calling toLowercase on both of these before comparison.
To wind up with this, it is apparent that strings have a lot of built-in behaviour which allows us to deal
with different conditions.
Take an example:
You are required to know whether the string is an email address or you may say if it is an URL. The other
cases can be you want to check if a password has a mixture of uppercase and lowercase letters or may be
the existence of some special symbols. You might be required to verify whether a credit card number has
the correct amount of digits. These are just few examples where you will have to make use regular
expressions built into many programming languages.
Regular expressions are sequences of characters that describe and have the ability to match
patterns and strings.
Although nobody finds ‘regular expressions’ comfortable to begin with. But they are very useful and it is
important to know about them. There is always two parts to work with regular expressions. First, create
the expression that describes the pattern you're looking for and next, try and apply it to something and
find out if it matches.
Another way of employing regular expression is almost the same as before with a slight difference.
Despite of myRE; now we make use of a new word RegExp which is regular expression and store the
string hello into it. In either of the cases we are actually creating a new regular expression object. Most of
the object oriented language employs regular expressions. The second word new implies creating a new
object. The word hello is simply a pattern which is up to you. You may take some word as well. It is now
required to look for the word hello if it exists in another string. Once the regular expression is created, we
will have to test it. Create a new variable called myString. We’ll test the regular expression against this
new variable. Therefore, the ‘if’ statement will be if myRE dot test. Dot test is a built-in method. It is
treated as a function that belongs to this regular expression.
myRE = /hello/;
46 | P a g e
// or
if ( myRE.test(myString) ) {
print("Yes")
You have passed in the variable myString and with the help of the above function you’re willing to check
whether it contains the word hello in it. The output will be a Boolean response. By calling the above
function to test will return either true or false. In this particular case, if the word hello exists in myString,
we will write a code to get it printed and say, yes.
Complex patterns in regular expressions:
There are some regular expression objects which have other methods that can return different data. The
special characters are used for creating complex patterns. Let’s take an example. Create a new regular
expression variable and use the shorthand format which uses the two forwards slashes, one at the start and
the other one at the end. Whatever is being written in between them is the pattern that we are going to
match. The point to mark here is that the forward slashes themselves are not part of the pattern. As we
have already seen the match on the word hello, so let’s move onto some different examples. Prefix this by
the caret character. It actually means hello has to be at the start. If instead of this, the pattern was hello$
before the forward slash, it implies that hello has to be at the end.
myRE = /^hello/; // ^ at the start
hel*o means that it would be a 0 or more. Hence, it would match all the previous ones and also match
heo. Here l doesn't appear at all. There is a question mark for 0 or 1 match. It would match heo or helo,
but would not match more than that. The vertical bar symbol means it is matching either for hello or
goodbye. The decimal point is the period which means any character.
If you’re using backslash w inside the pattern, it is going to match an alphanumeric and underscore. The
other case is if you use backslash b inside the pattern. It would mean you are looking for a word boundary
like a space or a new line. The square brackets can also be used to provide a range of characters to match
on. In this case, if you are looking for the ‘ope’, then it would match on these words: cope, rope, lope and
dope. These are among the very few patterns that can be form complex patterns in strings.
/hello|goodbye/; // either|or
/\wello/; // \w alphanumeric or _
47 | P a g e
/\bhello/; // \b word boundary
There are many patterns for regular expressions such as if you’re willing to match a date or a password or
credit card format. One of the regular expressions which are still on process is to match on an email
address. It’s a complex operation and people are still discussing and debating from many years on what
the perfect regular expression for an email address is. Sometimes you might notice there is a kind of a
repetition of the same kind of thing. The reason lying behind is that an email address can follow many
complicated rules. The regular expressions for email addresses might be not perfect but this is what has
come across the most. There can be complex patterns in regular expressions where you are not looking
for a perfect match but something which is good enough to fit in well. There is no need of memorizing the
formats even if you’re new to programming. You just need to set your focus on the fact that they do exist,
for what purposes they can be used for and how they're used. Most of the times, you will probably notice
that the syntax of regular expressions are built-in to most modern programming languages.
48 | P a g e
7
Working with arrays
What is array?
An array is an organized collection of similar type of data. The data or the values are encapsulated one
above the other and given a name. We have already seen how to create one variable at a time. After
creation of the variable and giving it a name; we can make use of the equal sign to assign that variable a
value. The value could be a letter or a number or a string or may be it’s a Boolean value. But the thing is
that it will be one value. There raised a situation where one wants to store multiple values in a variable.
This led to the introduction of array. An array is the notion behind having multiple values all contained in
a variable which can be treated as data and collectively forming an array.
Format of an array:
Create a variable named as myArray containing multiple values in it. It's a great way of keeping the data
together that belongs to each other or is kept for the same purpose. Now, there is no bondage on the
number of values to be stored in the array. It can be dozens, hundreds or even thousands of pieces of
information stored together. There is no need to give names to every single individual piece as an
individual variable.
49 | P a g e
On the contrary, there are few languages like the older versions of Visual Basic which used 1-based
arrays. But still 0-based arrays are more common and often seen in programming languages. Index
number is used to either set that value after the other or to get to that value. Hence, to access any one part
of the array, it is necessary to make use of both the name of the array and the index of the element. Let’s
understand this by making an array. The creation of the variable will be same as we used to do it earlier.
But it is necessary to indicate to the programming language that we are forming an array.
Now the question arises how to store the individual elements in the array. You will again have to use the
square brackets to mark out the position where you want to place your values or elements. If you want to
feed multiple values in the array; you need to again make use of the square brackets. The numeric value
written inside the square brackets is the index.
multipleValues[0] = 50;
multipleValues[1] = 60;
multipleValues[2] = “Hello”;
print(multipleValues[2] ) ;
The same format can be used to get the contents of the array. If you want to write a message “Hello” at
the second position, it can be easily done by using square brackets and we’ve seen this already. Though
this is an easier way of forming an array but there can be even easier and faster ways for creating and
accessing the elements of array.
Case 2: setting and accessing elements of an array in one line
50 | P a g e
Instead of wasting time in writing several statements like the way we’ve done in the above case, we can
use the square brackets and combine all the elements onto one line. We can just simply load this array up
with the initial values. Let’s take an example: when you write 100,200, "Hello" inside the square brackets,
it will automatically create an array and put them at the position 0, position 1 and position 2. Further you
can come up with adding more positions like to position 3, position 4 and so on. But it will always begin
from a 0-based index.
Arrays are found in every programming language and there are also other ways to group values together.
There can be a possibility where you do not want a 0-based index but a letter-based index. There are other
ways also to access the elements of the array. Such ways can also be employed but as seen in most of the
cases, they follow 0-based index. A collection in a programming language is simply multiple values that
are grouped together in some way and thus, forms an array. There can be slightly different ways of
creating them across various programming languages but the overall concept remains the same.
Behaviour of array
Arrays are often considered as objects. The word ‘objects’ sounds very common but it has a very special
meaning in programming. We will dwell more into objects but before that it would be enough to reckon
them as the containers of data. Arrays are very useful and tell us information about them. Because of their
behaviour, they can perform any task that we ask them to.
print ( myString.length );
myArrey = [10,20,"test",True,99];
print ( myArray.length );
It gets to know that its length is 5. This is the length of the entire array containing five elements in it.
Although the elements are positioned at 0, 1, 2, 3, 4; but it showed up 4 as the highest index. When you
call dot length on a string, it automatically understands that the programmer is asking for the number of
characters in the string. Similarly, when we call dot length on an array, it gives us the length of the array.
51 | P a g e
Methods in arrays:
We have already seen the usage of functions. We use the name of the function along with the opening and
closing parentheses. We can create functions ourselves and can also use the built-in functions like alert. In
this case, you’re calling a function and then passing in some information that also returns a value.
Functions are used in every programming language. We have seen a bit about methods which were very
similar to functions. A method is a function that belongs to an object. Instead of calling a method directly,
we use the name of the object followed by a dot and then the name of the method.
SomeFunction ( );
// to call a function
print("Hello");
result = calculateValues(5,73);
myString.toUpperCase( );
If you call a method by using the opening and closing parentheses, it would be like calling a function. But
it is important to specify for what purpose the method is going to apply. If it’s a string variable, we can
use the dot toUpperCase method to convert the string to uppercase. And there are many more methods
that we can apply. You can create multiple arrays and the different methods can be called on each array.
A shorthand format can be used here.
Take an example:
Create a new array and store some random values into it. There are five elements in the array. Now write
myArray dot reverse along with the opening and closing parentheses without passing any value into it. It
will reverse all the elements present in the array. 50 had been at position 4. It will now come at position 0
and so on. There are other methods like dot sort. It will sort the elements in the array according to the
rules of sorting. In case, if the elements in the array contain all alphabetical or numerical values or may be
both; we can make use of dot join. It will take all the elements of the array and combine them with
commas and then after return them as a string. As per the behaviour of the array, it has all the
information.
myArrey .reverse( );
.sort( );
.join( );
While programming you might be thinking whether you have to memorize the things. The answer is no,
at least not initially. You can’t memorize every single method on every single object as there are so many.
You can find a reference guide in this case.
52 | P a g e
You can refer to the reference guide for the language that you're working with and explore the
different methods available for that language as per your need. There is no need to memorize
everything. Just try to know where you can find the reference of the syntax.
All we have to do is to set up the index. We have already seen the importance of beginning with the 0-
based index and while working on the basic array; it is always advised to start the index from 0. You need
to check the condition now and you’ll notice something different this time. But at this stage you need to
know the length of the array which you can find out with the help of dot length property. You may write
the condition as while i < 5 (i is the index), but it is much easier to write while i <myArray dot length.
This will save your line of codes and make your program look short. As we’ve seen earlier also, since it’s
a loop we‘ll be required to increment the index at the end of the body of the code block.
i = 0;
//…
//…
//…
i++;
The thing to keep in mind is every time we iterate through or loop through we can make use of the index.
The value of i helps us to access the current element.
Taking an example:
To make it more clear to you; let’s try and understand the difference in the usage of i in this example. If
you create a variable i, go through the code and execute it. Here the value of index is 0. Moving on to the
next line, you see the condition if i is less than myArray dot length. Here you’ll have to first of all find out
53 | P a g e
the length by using myArray.length which will come out to be 5 in this case. Now you’ll begin checking
out the condition. There are five elements in the array. This means myArray.length is 5. Now we need to
know the value of i to check on the condition. You might remember that dot.length property gives the
highest number of index i.e. 4 in this case. The condition comes out to be true. Therefore, we jump into
the body of code block and start executing the code. Now there can be either some alert message or might
be you want to access the first element of array. We'll write myArray and the value of i which is 0 for the
first element. Moving down to the further processing; we’ll get to the end of the loop and increment the
index.
We'll move back and check the while statement again. Now the value of i is 1 after increment. Now we
need to check whether 1 < 5. The output is yes, it is. This is how we’ll keep on moving through the loop
while adding 1 to the index, accessing the element at that particular position and executing the code. At
one point we’ll come across 5 while keep on incrementing the index. At this point the condition becomes
5<5 which is not true. Hence, the outcome will be ‘No, it isn't’. As the condition was to check while i is
less than 5. So, after we encounter 5; all the other values will be of course, greater than 5. Therefore, the
program will terminate here. While we're checking the condition we're actually checking against the
length of the array and the value of index access the current element.
While dealing with different languages, you need to choose if you want an immutable array or a mutable
one. You might be thinking that flexibility of array is much more comfortable to work with. But there are
times where we do not require doing any kind of addition or subtraction. Let’s see through a couple of
examples. When you’re dealing with days of the week or the months of the year; we do not require to do
any kind of alterations. There is no need to eliminate any element mid of the program.
54 | P a g e
One of the strong advantages of immutable arrays is that they are faster to work with. All one has to do is
just allocate an area of memory for that array which it knows is not going to change. On the other side,
working with mutable or changeable objects, one thing will keep troubling your head that this array may
grow or shrink. We know that an array is an organized list of items. We begin from the position 0 and
keep incrementing the index one by one. But there can be other kinds of collections as well. Now imagine
instead of the built-in index of 0, 1, 2, we might want a, b, c as the indexes.
Take an example:
You have a list of states. The collection is very useful but we are using the abbreviation for these states.
Despite of using indexes as 0, 1, 2; we’ll use MO, TU, WE. Many languages allow this kind of an array
and it can be given a few different names. Such kind of array is called as an associative array. This can
also be considered as a dictionary or map or a table. It can be associated with databases. But it’s not a
database. It has nothing to do with a database. These are variables in memory and allow us to access each
individual piece of data. So whenever working with any language it is advised to know about the built-in
support for different kinds of collections.
55 | P a g e
8
Style in programming languages
What is style?
While working with different programming languages; you will come across a new factor called style.
You need not to worry about the rules or the syntax or how to write the code rather style tells us how you
should and shouldn't write it. It actually deals with what names should be given to the variables and what
is the right place to put the curly braces or how should you call the functions and how should you applied
in the code. All the programming languages have developed a set of style guidelines.
The major point of style guidelines is that you need to focus on making your code readable, consistent.
You will also need to keep an eye over how other programmers are writing their codes because that will
help you out in making your code look easier to read. Also you can refer some books to rectify whether
the code you're looking at is written well or not.
Create a variable named as ‘a’. You can choose any other letter also. Sometimes variables are even given
meaningless words like ‘coo’ or ‘shu’. But the information represented by these programming variables is
very useful. In case of an image, the information could be a name, a date, the height and width of an
image. Now-a-days, naming conventions hold the dominant part. Variables and functions are the two
whose names are decided by us and are mostly written in CamelCase format. This actually implies that
name of the variable starts with a lowercase. But in case if there is more than one word, it will capitalize
the first letter of each subsequent word. Just like the hump of a camel and so the name given.
variable score;
variable highScore;
variable everHigherScore;
CamelCase format is the dominant style in modern world. It is recommended by the Yahoo and
Google style guidelines.
The problem with brace style is where to put the opening and closing curly braces and how much to
indent your code. While using ‘if’ statement or a ‘while’ loop, the curly brace gets opened up on the same
line as the keyword. The code is indented inside the block and the closing curly brace closes on a line by
itself matching up with the keyword. You must have noticed while using ‘if’ and ‘else’ statements that
‘else’ is written on one line with both the closing of the previous block and the opening of the next code
block. The other styles like Pascal or Allman style have the opening brace on its own line and then it lines
up with the closing brace. This is very much common in languages like C#. But the C based languages,
for ‘while’ and ‘if’ statements and its makes use of the opening curly brace on the same line as the
keyword. You should be consistent over the way you write the code.
• You should make use CamelCase format for variables, functions, and methods.
• Open curly braces on the same line.
• Define functions before calling them.
• Use semicolons to end a statement.
• Always use proper variable declaration.
What is pseudocode?
While you start writing a program, you might come across a blank text file, the syntax, case sensitivity
and the curly braces. There can be many other things to keep in mind while writing a program. Now
despite of remembering all these things and there are thousands of things associated with it. So it is not
practically possible to make your mind engrossed in memorizing the stuff. Instead you can jump off from
your computer and either find a piece of paper or look for a whiteboard and then begin writing what's
called pseudocode. Make one thing very clear that pseudocode is not a language.
Pseudocode is the term for writing the computer instructions in plain English. They are written to
the point which makes it readable to anyone who comprehends the problem; no matter whether
they have a pretty good command over codes or languages.
Case 1:
57 | P a g e
Let’s go through few examples. Suppose you’re asking the user for email addresses and pens it down on
either a whiteboard or a piece of a paper. If the email address is found to be matched to the accepted
pattern, add them up to the email list or else show up the error message. The basic thing about
pseudocode is that there are no fixed formal rules, but some of the rules are very common to see.
Conclusion:
The way you deal with things and go down to follow the steps matters a lot. Despite of simply asking the
user for email address, you could have also asked to prompt for email address. To make it more clear and
understandable, it is always advisable to break the things apart and makes it readable. Create an email
variable, ask the user for email address and store user's email addresses in the email variable. This might
be a bit lengthy. What are we trying to do here is defining the structure of the code. You will now come
across programming keywords like if and else and while and for. Pseudocode is only indented to make
the structure to be followed easily.
Different people have different ways of writing the pseudocode. The longer you will write a language or
the lengthier you steps would be; the more the pseudocode will have a tendency to inculcate the style of
that language. Sometimes you might fall upon pseudocodes written partway in uppercase. Many a times
while writing pseudocodes, people overtly mention where an if or a loop ends by using a phrase like END
IF or END LOOP. This exactly serves the same purpose as a closing curly brace in any C-based language.
The only thing which makes them fall on opposite sides is that there are no rules in psedocode. The only
focus is on clarity and understanding.
Case 2:
The thing you should clear in your mindsets is your pseudocode should make sense. Let’s see an example
where you’re going through a list of numbers and adding them together. We will start off with setting
total to zero, getting a list of numbers, looping through each number in the list and finally adding each
number to the total. You need to remark where the loop ends.
Conclusion:
If the number is more than zero, it will print a positive message, else it will print that it’s zero or last
message, end if. All that matters is it should make sense whether you can program or not. There is no
syntax of writing pseudocode. It’s a place where you don't have to worry about braces, parentheses,
brackets and naming conventions. It is not necessary that one line of pseudocode will break down into
one line of real code. Sometimes one line of pseudocode like send email can split up into several lines of
real code. The best thing in pseudocode is it explores and let you know the problem without necessarily
knowing exactly how to code it. In case you have a problem in writing a code; it exposes where you need
to go and learn about it. Even when don’t have any knowledge about the language; you can still write
psedocode and identity the problems.
Case 3:
Let’s suppose as if you’re building a game and you know how to work with images. You have some idea
about such programming as you have done some of it already. You start writing a pseudocode code like
58 | P a g e
this. If a missile-image touches the spaceship -image, you’re going to replace this spaceship-image with
the explosion-image. An explosion sound will be played and then you need to check if the remaining-
lives counter is zero. If none of the lives are left, you will show the "game over" message. In case it is not
zero, you will subtract one from their remaining lives. You will show the begin message. You will reset
the spaceship to the starting position. This is not necessarily the perfect pseudocode for a game, but you're
following the steps here. Even if you do not know how to play a sound in the language; you can still build
your pseudocode. You can make your own personal task list and pen down all the problems you want to
look up for. You can pen down like: "research how to play short sound effect." After you’ve figured it
out; you can code this.
Conclusion:
It must have cleared to you now that pseudocode is not just for beginners. Many very experienced
software developers also work out a lot of their problems in pseudocode; so that they can have a better
understanding of it without worrying about syntax. It's a very useful habit to get into to start sketching out
your programming ideas in plain English before you write code.
59 | P a g e
9
Input/output and persistence
Such types of programs are designed to run and finish as fast as possible. But later on such programs were
designed that run with graphical user interfaces. Programs like a web browser that not just start and run
all the way through as quickly as possible.
On the off side these programs can also stay up in between the process. We can interact with these
continuously and erratically over hours or even days or even weeks. But here the input and output has a
different meaning. A mouse-click over something or something typed in a webpage is the input whereas;
change of images on the screen is the output. This is not the same as saving and loading files because
sometimes we are required to save documents to the hard drive or load files from the hard drive or save
information to the web.
Introduction to persistence:
Describing everything as input and output is very common. When you are saving some information and
then running a program. Finally you want to save information either to your document folders or to a
database or to the web or to a cloud. All this simply implies that it will come out of the currently running
program. But that data would still be on the drive or in the database or in the cloud. This is termed as
persistence. It is referred to save the state of a program. If you won’t decide where to persist the data it
will just disappear.
60 | P a g e
All the variables we've worked with so far get stored in the computer's volatile memory in RAM while the
program is in process. On the other hand, when we close the program everything is disappeared if you
don’t decide where to store it. But the major problem is that different programming languages support
different ways of persistence. Languages which are mostly used for desktop development, mobile
development or mainframe development have built-in ability to write a code to save or load files to the
hard drive.
Examples of Events:
The ability to employ event-driven programming is built into most of the languages now. Unsurprisingly,
we are cognizant about it that such events are already happening around. When the page is loaded, that's
considered as an event. When the user clicks somewhere on the screen, this is another event. And when
we say when they move their mouse falls under a whole bunch of events. Scrolling the screen up and
down or clicking into a text field or typing the keys on the keyboard are all events. Even when they leave
the form, it's another event. To wind up all this, we can say events are going on all the time.
Take an example:
You have one event called multiple functions or multiple events. You’re free to give any name. Though
there are several ways to handle an event but here we are concerning on one for a basic understanding.
While working with event-driven programming, there are two pieces of information that you require.
First, what is the event and second, what do you want to do with it. Now keep in mind that these events
can happen on different pieces of the page or may be the different pieces of your user interface. It's just an
61 | P a g e
idea of a click event. But this click event must be happening over something. It can be either on a button
or an image or some other part of the screen.
myelement.onclick = function () {
//...
//...
The idea of the word function followed by parentheses, and then followed by the opening and closing
curly braces is the general format that we’ve seen earlier also. Usually, the word function is associated
with a name. If it’s not given any name, it’s called an anonymous function. It means a function without a
name. We know that the function is just a bunch of code wrapped up. When we use a function it is
actually indicating towards an occurrence of an event and then after the code inside the block is executed.
Usually whenever you declare a function; you make use of the opening and the closing curly braces.
While following this format, you might come across a semicolon after the closing brace. This is
something contrary to what we have seen in ‘if’ and ‘while’ statement. They don’t use a semicolon just
after the closing curly brace. The reason of putting a semicolon here in this case is because the entire line
is a statement and not a function. So the format becomes: myelement dot onclick = the function and
whatever is inside it.
62 | P a g e
• Open file at path myfile.txt. This allows you to grab hold of it.
• Read the contents of the file into a string variable.
• Close the file.
There is always a rule or you can say a procedure to close the file when you're done with it. There can be
many problems that you might face while closing it. It could be like if that path has no file or some other
program has that file already open or if they have already grabbed it or if that file is too huge that it
cannot be read into a string variable. So there can be hundreds of possibilities of problem to occur. While
dealing with file input/output, you will come across is a lot more conditional code going on to check out
the things and that could possibly go wrong. No need to worry about it too much. All you want to do is to
read in a small text file that you know exists.
63 | P a g e
Case 2: Using Ruby
Let’s take an example using the programming language Ruby. As told earlier also; do not worry too much
about the syntax. We are only focusing on the overall approach here. It's not about whether you can scan
this code and find out the file path or look where you had opened and close the file. You just have to keep
an eye over the general approach here.
We’ll open the file called simpleexample.txt. Because you haven’t given any path information here, so
this file should be in the same folder as the program itself. The next line will tell you which file is to be
opened here. Write file dot open using that filename. The “r” in quotes have a significance here. It means
open this file in read-only mode. If you want to open this file where you could write to it; you will have to
use “w” in quotes and for both reading and writing; you can write “r+”.
filename = 'simpleexample.txt'
f = File.Open(filename, 'r‘ )
f.each_line { |line|
puts line
f.close
Different programming languages support opening files in different modes. One mode is for reading,
other mode for writing and another mode for reading and writing. There might be an overwrite mode if
the file exists already or another mode to append if the file exists already. These types of modes are very
common and useful. Opening a file in read-only mode has less impact on the system and it allows other
users to open it too.
We've got a couple of curly braces being used. It appears to be some kind of container. We are actually
reading each line and using puts, p-u-t-s. This puts a string in Ruby language to display each individual
line. In the last line we're writing f dot close. This is closing the file that we have opened on line 2.
Let’s write a string out to a file in Ruby. We begin by creating a string called message. The same thing
will go with the filename = 'myoutput.txt'. Then after we call File dot open. We’ll be using a slightly
different format here. We have got the “w” in quotes which mean to open in write mode. The last line
allows us to have an automatic close to the file that you opened a couple of lines beforehand. Again the
approach is important here rather than the syntax. You are required to know the path to open the file, to
write the contents and in the end close the file. Ruby is one of the simplest languages for reading and
writing files.
filename = 'myoutput.txt'
64 | P a g e
f.write message
end
We’ll again go through the loop enquiring whether it still has lines left. The output here is positive.
Similarly we’ll go through another line, display that line and this is how we go piece by piece through the
entire file. Finally, we'll close the file. If the file is opened it needs to be closed.
Concept of streams:
Many higher-level languages like C#, Java and Objective-C, you can end up working with files by using
what is called streams. The concept of a stream is very simple. It means a stream of bytes like a conveyor
belt of bytes. It is not as complex as it sounds. While working with streams, you need to take them all as
one piece at a time. It’s easier working with streams because you need not to worry where this data lies.
You even don’t have to enquire if that file is on your hard drive or not. It doesn’t matter whether you’re
reading this data online or offline. It is a stream of data that you need to deal with. The only problem that
a programmer who is new to any language might face is finding out how that language works with file
input and output. You'll figure out yourselves that most languages deal with the same concepts in slightly
different ways. They all have substantial built-in functionality for reading and writing different kinds of
files.
65 | P a g e
10
Introduction to debugging
Cause of debugging:
If you’re completely new to programming then it would be difficult for you to write even a single line of
code that works correctly. Programming languages are very specific and it's very likely to get mistaken in
the special characters that it uses. Like using a colon instead of a semicolon, missing a closing quote,
using the wrong operator or the wrong name. Once you get beyond programs like “hello, world” and
begin writing programs which are important. It becomes difficult to write more than a few instructions
without tripping up over your own logic. You expect your code to work and it's quite possible to get upset
when it doesn't. There are a few things that experienced programmers know and non-programmers don't.
Don’t worry about mistakes. They are likely to happen. Every time you fall into some mistakes; it will be
a plus one to your knowledge in programming. This is how you learn to debug the code. Keep rectifying
what went wrong with your code and go back and fix it. You might come across some situations when
you’ll find your program was working finely but you noticed some odd behaviour in the result. You will
notice this odd behaviour in the errors in your code. This breaks down into two main categories.
The first and the foremost are syntax errors and logic errors. Let's find out the difference. Most of the
times, the problem is found in the syntax. This means that something is wrong with the actual format of
what you wrote. Further possible errors could be misspelled keyword with wrong case letter or usage of a
colon instead of a semicolon or forgetting to close a quote on a string. It’s not that easy to debug such
errors when you've got thousands lines of code.
66 | P a g e
Even experienced programmers are likely to commit such errors and from time to time they have to go
through letter by letter figuring out what they have missed out. It's very common to count the opening and
closing curly braces to make sure that they match up. One of the benefits of using programmer's text
editors is that they highlight the syntax. While you’re working in an editor and green means a comment.
Sometimes you can make a mistake like when you’ve accidentally opened a multi-line comment. This
made everything after this an actual comment, whereas what was required was a single line comment.
Color coding can help you a lot when you're just scanning your code to find syntax problems. These kinds
of problems become less occurring when you get familiar with any language. Might be you still make
them but it would be lesser. One of the benefits of using a compiled language rather than an interpreted
language is these syntax problems are found before you even try to run the program. The very useful
thing in compiled language is the complier as your code needs to go through the compiler before
execution. But with an interpreted language, you often have to attempt to run it and find out what's wrong.
if (isNaN(minutes)) {
return;
document.getElementById("inputArea").style.display = "none";
The problem is if you have fall upon a return statement and a function, you'll straight away jump out of
the function into whoever called it. This implies that any line after the return statement would never be
executed. The problem with such an error is that they can be extremely difficult to find. There are some
logic errors that even fall into their own category like problems with arithmetic. Such types of errors are
also correct in syntax but they are meaningless.
function myFunction( ) {
67 | P a g e
//do stuff
return 0;
//do stuff
//do stuff
//do stuff
If you write some code having a variable named as ‘a’ and set it equal to 50 and the other variable b = 0
and storing the output to a variable called result. The task assigned to result is result = a/b. The syntax
here is correct: the right case, the right operators and even the semicolons at the right place. The problem
is this doesn't make sense. a/b here is 50/0. Dividing by 0 is not allowed. It is logically meaningless, so
the computer can't perform this. You will notice in many programming languages, the line that tries to
divide by 0 will cause your program to crash completely. These are just a few examples of the errors that
are committed very easily.
a = 100;
b = 0;
result = a / b ;
print(result);
The logic errors will be the most significant problem you'll face have as a programmer. In any debugging
situation, try to reproduce the problem. Hopefully it will help you out to figure out the cause of the error
easily and where it has occurred. You need to figure out if your program is actually running the way you
thought it to be.
68 | P a g e
11
Introduction to object-orientation
These are objects are not just randomly created. They represent the way you talk and how you tackle the
problem which you are trying to solve. They can represent things like employees, images, shapes, bank
accounts, player objects, students, spaceships, asteroids, video segment, audio files and whatever that
exists in your program. Object-orientation can be referred to as a programming paradigm. It's not a
language itself but a set of ideas that is supported by many languages.
There are also other alternatives to Object-oriented programming. Other than just procedural
languages like straight C, you might also work with logic programming languages like Prolog if
you pursue a university computer science course. Functional programming languages like Haskell
tend to be more popular in the academic world or for very specialized uses like computational
linguistics. If you want to work in the pragmatic world of creating web applications, mobile apps,
desktop applications, game development, it is advised to use Object- Oriented programming
languages.
What is an object?
Always remember one thing that the introduction of Object-Orientation and computing was intended to
dwell more into programming and bring the real world closure to it. When you refer to an object in a
computer program, you should know what is that object in the real world. It’s difficult to describe an
object before you know something about it other than just taking it as a thing. So here we are looking for
the objects which even belong to real world like you can say like apple, mug, fruits and there are more
thousands of etc. These are all objects or you may say these are all things.
69 | P a g e
Behaviour of objects:
Objects are separated from one another. They have their own characteristics and existence. Their unique
identity makes them independently different from other objects. If there are two mugs placed before you.
It is not necessary they are same. Their unique characteristics, attributes and behaviour can be entirely
different from each other. This makes them two different objects. There should be no complexity in
dealing with objects. An apple is an object, so is an aircraft, so is an iPhone or flowers or glasses and the
list is endless. All are objects. But what makes them very different from each other is the way they behave
and their individual attributes. One object might contain other objects as well.
There is no point in getting perplexed among these objects because these are all real objects and we have
already known a lot about their characteristics which gives an elucidate understanding in their
separateness. They have inherent properties that describe them.
Take an example:
A mug can be full or empty. An apple can be green or red. A tree can be short or tall. A lamp can be off
or on. These are called the attributes of any object, things like color, weight, and size. They describe the
current state of an object and the state of one object is independent of another. When you turn one lamp
off, it does not mean that all the lamps in the world will be turned off.
Most objects have multiple attributes. A mug can be full or empty or the other state could be it is half
empty or half full. At the same time it could be black or white or blue or some other color. It could be
small or large. The behaviour of the real world objects are varying. A telephone can ring and an airplane
can fly. The behaviour is specific to the type of object it is. An apple cannot ring and a telephone cannot
fly.
Conclusion:
The point to remember here is the three things: identity, attributes and behaviour. They are one and the
same thing that describe an object in an object-oriented programming language. Objects in a computer
program are self-contained. Their identities separate them from other objects. They also have their own
attributes. This information describes their current state or the things they are efficient enough to do.
70 | P a g e
also open it or close it. It can be concluded from this that though they are not physical items but still can
be described from their behaviour. Similarly, objects in a computer program are just the things which are
having a visual appearance on the computer screen.
Undoubtedly, buttons, images and spaceships can all be objects but invisible things like a timer or an
array can also be treated as objects. So don't just focus purely on physical items or visible items when
you are working with objects in a computer program. If you're new to Object-Oriented Design, it can be a
little challenging for you to figure out if something in your application is a new potential object.
It’s quite easy when your application needs something like car, employee or document. But it’s not that
easy when you're building an event management application. Can an event be an object? One way to find
out is to ask if it’s a noun. We have read in primary classes that nouns aren't just physical things, but
people, places, and ideas or concepts. The other way could be put the word "the" in front of it. The mug,
the apple, the car or the television; no doubt they are objects. But also the bank account, the time, the date
or the event; these also work.
Object-oriented programming has been such a successful idea that all the languages except one employ it.
C is not an object-oriented language rather a procedural-oriented language. It predates the time of object-
oriented programming came into existence. But the advanced version of C is C++; C++ is object-oriented.
It has the original name of C with classes.
Objective-C is also just C with additions to support classes and objects. C# and Java are both languages
that extract a lot from C and add object orientation. On the weaker sides of things, Perl was not originally
an object-oriented language but it support some features. We’ve already studied; JavaScript is object-
oriented loosely and the same is PHP and Python. Ruby is the purest object-oriented language here.
Whenever you begin to program in C, the thing to bear in mind is to get familiar with the type of
language and start figuring out what built-in classes exist in it.
The best thing working in object-oriented programming with a lot of existing classes is that somebody
else has already written the behaviour that you don't have to. In JavaScript we've gone through some
examples having classes that depict simple things like arrays and dates. While working with languages
like Objective-C, Java, and C #; classes are there to represent text boxes, images and buttons. Moreover,
these can be used for files, video players and cryptography functionality. In fact almost for anything else
that you can think of.
71 | P a g e
12
Memory management across languages
What is memory?
The thing we haven’t discussed yet in detail is memory. Usually after creating the variables and objects
and using them up; we forget about them. But actually this is not the case always. In many programming
languages, you can't just create an object, use it and forget then after. It is important to manage the
memory that the object uses because that manage the lifetime of that object.
At the same time you should also be aware of what all things are no longer used. If that memory is no
longer used to you then you should free that memory. It’s not that difficult as it might seem to. When you
start passing through these objects, you start calling functions and finally have a long lived program. This
can get a bit difficult. A very common mistake noticed is you allocate some memory to the object you
created then after using you forget it forever. The reason behind this is that you don't need that object
anymore. The point to mark here is that you should always remember to free the memory. Sometimes you
think you have made the memory free but it has not probably. This gives you a memory leak. It’s not a
big problem if you have done it once. But if next time you create a long-lived program that have
thousands of objects and when you run the program, it's going to get slower and slower and take up more
and more memory. This can even crash your program. The memory leak isn't the only problem you can
face.
Take an example:
Let’s go back to the first object whose memory you have already freed up. There might be a possibility
where a variable points to that area of memory. This is called a pointer. If you try and use that variable
because you think the object still exists but actually it doesn't. This actually called a dangling pointer.
This can make your program to crash. That is why memory management is important and a bit difficult
72 | P a g e
code to write. The difficulty level has risen because with more high-level languages are evolving; there
are different methods for dealing with it.
As you start to get more into modern high-level languages, there are several techniques for dealing with
it. Objective-C and C++ can choose to use something called reference counting. It is preferable to manual
memory management and allows keeping a counter on who is using the object. Rather than manually
freeing the memory, you can just allow the system to free it up when it realizes that counter has reached
0. When you switch over to languages like Java, C# and VB.NET; there is a memory management
technique called garbage collection.
Rather than keeping track of individual objects, the system itself keeps the track of how much memory
your application is using. It also keeps scanning through all your objects and find out which ones are still
relevant and which ones are still being used up. Suppose if it sweeps through a thousand objects, clearing
up 500 of them and finally leaving 500 of them around. This is regarded as garbage collection because it
fritters away the unused objects and saves the memory. But it doesn't happen all the time. There is no
surety when it is going to happen. As a programmer when you’re dealing with these languages,
sometimes you can suggest to the garbage collector that it is a good time to go and clear up areas of
memory. But it is not required to work on the individual level of the individual objects. Further when you
get into more scripting languages, it becomes completely automatic. You have no control over when
memory could be reclaimed. But the issue is the more automatic it is, it becomes slower and less efficient
it is.
Writing manual memory management code is tiresome and difficult. If you are able to do it absolutely the
right way then it will lead to the most efficient use of memory. It’s your primary focus then you’ll have to
look for languages that allow you to do manual memory management. The importance of memory
management was a prior concern a couple of decades ago as compared to the present world. Therefore,
years back the higher level languages employing memory management features like garbage collection
holds a lot of importance and could efficiently create code and thus programs worked faster.
Introduction to algorithms
You often come across a term in programming called an algorithm. It sounds similar to a program itself.
Algorithms are sequential steps to accomplish some task. But in terms of programming, it is considered as
a small procedure or a part of the program rather than the whole program itself. Perhaps, it is just a
function. It performs a specific task. There are many different ways to accomplish the same task so there
are many different algorithms.
73 | P a g e
Take an example: understanding concept of bubble sort
Suppose you’ve a series of numbers which you need to sort. It could be 5 or 5000. There are several ways
of accomplishing this task. Let’s see through one of them. Let’s take you want to compare two numbers at
the same time. Take the first two numbers and compare them. If the first one is greater than the second,
then flip them. Moving down the series, compare the next two. Again, if the first number is greater than
the second, flip them again. In this particular case you’ll have to move up again and compare the next
two. Check out whether the first is greater than the second. Now you find out that it is greater. Scour
through each number in the series step-by-step means that you have to scroll through the numbers
individually. You can take the biggest number on the first pass and movie down and then compare it to
the number first passed. When you get to the end of this first pass, you reach the highest number is at the
end. But still they are not in order. You need to make another pass, go back to the start and compare those
two numbers. If the first one is higher than the second, you will flip them. This time you need not to go all
the way to the end because you’ve already bubbled up the highest number. You will notice that you have
reached the last number. Quickly you can return to the start and compare the numbers. But you don't need
to flip those two. After multiple passes, you won't need to flip anymore. They're all in order. This is
referred to as a bubble sort. This is a simple sorting algorithm.
You can make multiple passes and bubble the larger numbers up to the top of the list. Let’s now
understand this by the help of a pseudo code. You can write a bubble sort in just a few lines of code. It
doesn’t matter whether you feed five numbers into it or 5000. It's a simple straightforward algorithm but
unfortunately, bubble sorts are not so efficient enough. If you have to work with large lists of data, then
bubble sort doesn’t produce an efficient result.
repeat
swapped = false
for i = 0 to (list.length - 1 )
swapped = true
end if
end for
Now you might be thinking if there are other ways to perform this task. To some extent it also depends on
how long the list is. There are many sorts like bubble sorts, heap sorts, quick sorts, insertion sort, merge,
selection, permutation sorts and these are just some of the best-known and most prominent sorting
74 | P a g e
algorithms. There are many more than this. The best thing now-a-days is that in most languages you’ll
find sorting functions are already built into the language. While working with arrays or other collections,
you can expect them to have built-in sort functionality to them.
It’s not necessary that the simplest algorithms which you can write well are the most efficient one. The
most efficient algorithms can be very difficult to write, hard to understand and maintain. This is one
reason why computer programming has often been called an art rather than a science. If the same problem
is given to ten experienced programmers then it is likely to get 10 completely different solutions. The
point is there is not just one way to solve a problem. There can always be ways to betterment and
improvising the older one. Many a times while programming you might fall upon problems to decide
between ease of use, development and speed.
Introduction to multithreading
You must have seen the operating system running multiple programs at the same time. This is referred to
as multitasking. The complete concept of performing multiple things inside one program is called
multithreading. You must have come across multithreading many times while working on your computer.
It keeps your program responsive; specifically in desktop or mobile applications with a graphical user
interface.
75 | P a g e
13
Most renowned languages
Introducing C language:
The most influential language C was invented in the early `70s at Bell Labs and it still in fabulous use
around the world. C has influenced many renowned languages like C++, C# and Objective-C. Also
languages like Perl and PHP owe a remarkable amount to C. C is a pure structured language; also known
as non-object oriented language.
It's neither about classes nor objects. It is a purely low level language that works with manual memory
management. It is a compiled language which strongly typed in the sense that you just don't create generic
variables rather you create variables of type integer or float or Boolean values. But getting started with C
language is not that difficult as it seems unless you’re getting too detailed with the language.
If you want to build desktop apps or mobile app or web applications, where you're looking for a
high productivity environment, then C language can be a good choice.
If you’re looking for an environment which makes your program run fast and keeps it compact; then C
language can be a good catch. You just have to. But you should be able to recognize the basic structure of
it. It uses the curly braces to surround code blocks. It uses the semicolon at the end of the line. It uses the
two forward slash for the comment. When you create variables in C, you write it like int X not var X.
This is because you’re creating an integer variable or a character variable, so you need to be specific
about the kind of data that you’re working with.
Functions in C:
Functions are also employed in C. You don't use the function as a keyword. In actual fact, the word
‘main’ here indicates that this is the single most important function in a C program and in fact, in almost
76 | P a g e
all C-based languages. ‘main’ here is the name of a function. It's the function where the program begins.
This is how C and most C-based languages know where to start. There's a block of code with the word
‘main’ attached to it and a very common thing that you'll notice in a C program is a statement like
‘include’.
#include <stdio.h>
// this is a comment
int x;
x = calculateSomeValue ( ) ;
printf( "Hello\n“ ) ;
As you’ll start exploring more languages, you'll figure out several things like that. If you talk about
general purpose languages it’s very common to link to external libraries. Basically what happens here is
this file encapsulates a whole bunch of code that somebody else wrote. These functions are very useful
and you can use them in your program to get it link to it. As you get more detailed into the most general
purpose languages, you’ll start noticing that it's less about the language itself and more about all these
external files also called libraries of code that you can get your program to link up.
How to start up in C?
Let’s have a look at it and see how to get started in C. C is one of the standard languages which do not ask
for many things. You need a text editor and a compiler. You'll often find C compilers built into
programmers' text editors and other IDEs. This is because C predates the web. There are many websites to
fetch the information about it. Box:
The classic compiler is called as GCC. It's an Open Source compiler used by a lot of different IDEs or
Integrated Development Environments.
You can refer to gcc.gnu.org and this one of the best place to find some central authoritative
information.
77 | P a g e
Java is a language that uses the hybrid compilation model. Neither it is compiled nor interpreted. It
compiles and sends it to what is called bytecode. It is a very cross-platform language. There was adage
given by Sun Microsystems was "write once, run anywhere". As it is a partially interpreted language the
users have to have the interpreter for Java on their machine. And this came to be called as the Java Virtual
Machine or JVM. You must have noticed that some applications on your machine require to install Java.
That is why it is famous for desktop applications. You can write a desktop application in Java and then
run it on a Mac or on a Windows PC or on a Linux box.
Of late, one of the biggest endorsements in Java is for developing mobile applications that run on
the Android platform. So it's a C-based language.
Take an example:
The thing which pops up again is the word ‘main’. This is how you tell a Java program where to begin.
Let’s see through an example. You have intmyInt = 55 line. Here you’re using the word int. rather than a
general variable, it’s an integer variable. The line that begins System.out.println is how you output the
words “Hello World!” on the screen. But because this is an object- oriented language and in Java
everything is in a class. That’s why you have to surround the code that you want in this class
HelloWorldApp.
class HelloWorldApp {
int myInt = 55 ;
It might seem to be a bit strange but remember most of the development environment you use, provides
this framework for you. You just have to get in and fill in the gaps. And as it was mentioned before,
although there is a passing similarity because of the curly braces and semicolons; Java is not JavaScript.
The languages have very little to do with each other.
If you're developing an interest to get started with this language then you're probably looking to get a full
IDE, a full development environment for Java.
The two most popular among IDEs are Eclipse and NetBeans. This is because this language is
owned by Sun and thus developed java.sun.com as the most authoritative website to go to. Sun is
now owned by Oracle. You can easily direct it to find a lot of information by tutorials available
giving information on how to get started and the basics of starting to work with Java.
78 | P a g e
.NET languages: C# and Visual Basic .NET
Let’s talk about two languages: C# and Visual Basic.NET. These are the two flagship languages if you're
developing them on the Microsoft platform. We’re taking a look at both the languages at the same time
because although they do look different but their usage and approach are almost identical. These two
languages were launched in 2003 by Microsoft. But Visual Basic has a longer history than that. It was
reinvented at that time when Microsoft released the .NET framework.
79 | P a g e
Basics of C# and Visual Basic .NET
The vast library of prewritten code contains a huge amount of functionality that you can tap into if you
use either of these two languages. Both of them are object oriented. They make use of classes and objects
and both of them share the same characteristics. They are high-level and strongly typed languages. They
use garbage collection so you don't have to worry about memory management and use the hybrid
compilation model like Java does.
Similarities to Java:
The important thing to mark here is that neither they strictly compiled nor strictly interpreted. They go
halfway. When you compile a C# or a VB.NET application, it will compile to what is called Intermediate
Language or Microsoft Intermediate Language. This means that it can be distributed across a variety of
different machines with different CPUs. Now each machine will take it the finishing step to move down
to the machine code. Just the same way as Java requires the Java Virtual Machine on every computer
which runs a Java program; .NET languages require .NET Runtime to be installed on every machine.
Resemblance to ASP.NET:
Unsurprisingly, they are closely associated with developing on the Microsoft platform, no matter whether
that is Windows server or a Windows desktop system or may be a Windows phone. All this can be done
with the help of .NET languages. They both have the same proficiencies for building desktop
applications. You can also build dynamic websites using these languages. When you think of building a
website on the Microsoft platform, it is generally fall under the term ASP.NET.
What actually ASP.NET denotes is that you're building a dynamic, smart, interactive website using
Microsoft technology. When you’re doing it, you're actually employing either C# or VB.NET. These
languages can be used for developing mobile applications that work on the Windows phone operating
system. So you can say these languages are closely associated with the Microsoft world. You shouldn’t
wonder when you hear that C# is not surprisingly a C- based language. Probably it seems to be closer to
Java as well. It makes use of the curly braces, the semicolons to end the statement and the word ‘main’.
This is how you inform the .NET Runtime where your program begins.
Resemblance to Java:
You might be wondering how to execute the first line. In this particular case, its System dot Console dot
WriteLine. You need not to worry about the syntax. This is just a simple way to get the text Hello World
out to the console by flaunting up a message.
Take an example:
As you’ve seen in Java, the whole thing is enfolded with this phrase public class Hello1. The reason
behind this is C# is a thoroughly object oriented language and everything inside it falls under a class.
// Hello1.cs
80 | P a g e
public class Hello1
System.Console.WriteLine(“Hello, World!”);
Resemblance to Pascal:
To create even the simplest application, you just have to have a class and then call ‘main’. This is the line
you’ll have to execute. You might have noticed that the use of curly braces is a bit different here. This is
because in C#, the common usage of them is to line them up so that each on their own line matches the
opening and closing curly braces. This is referred to as Pascal or Allman style. It has become the accepted
style in the C# world. C# is whitespace insensitive same as in C-based languages.
Particularly, in this case you can't mark out where blocks of code begin and end with curly braces. You
will have to make use of ‘if’ and ‘end if’. If there is a Sub also known as subroutine like a function, you
can make use of an End Sub or a Module. You also have this System dot Console dot WriteLine. This
line is exactly the same as you’ve seen in C# except for the semicolon at the end of the line that is
required in C#. Though the overall format of the language is different but the statements that you execute
is in a .NET language. This is identical to each other as you’ve the same libraries and functionality
available to you.
Module Module1
Sub Main ( )
System.Console.WriteLine("Hello, World!")
End Sub
End Module
If you’re able to code well in C#, you can easily read VB.NET. The point here is how to get started.
Microsoft has made it fairly easy to do this. Go and download Visual Studio. This is their flagship
integrated development environment and you'll notice that there are several different versions of which
include versions that are completely free. This is referred to as the Visual Studio Express editions. Visual
Studio includes a text editor, a compiler, a debugging tool and all the things that you need to get started
with any of these languages.
81 | P a g e
There are versions of Visual Studio that are commercial and cost several thousand bucks. This is
how you can start up with these languages, experiment and play around. You can even create
websites and desktop applications without spending anything. If you're keen to know more about
it, take a look at microsoft.com/express.
Ruby
Let's have a look at one of the newer languages in the software world. It was developed in Japan in the
mid of 1990s. Many people consider it as a very pure object oriented language. Working in Ruby, you’ll
find everything as an object, even variables that might be considered primitives in other languages like
Booleans or integers. Everything is an object. It's a high-level garbage collected language. You don't need
to worry about memory management and it's completely interpreted. You don't need a compiler for Ruby
rather you need an interpreter.
It doesn't compile to intermediate language like Java or the .NET languages. Most people find it very easy
to approach. You don’t require a lot of prerequisite knowledge to get started with Ruby. It's fairly friendly
for beginners. Now the question arises for what it is used. We know that it is a cross-platform language.
There are installers and interpreters available on every operating system. You'll also find it built into the
last few versions of Mac OS X. It's has probably become the most famous for its use in what's called
Ruby on Rails.
This is a well-designed web framework that uses the Ruby language. Along with this ruby has come up as
a great language for writing small utilities and text processing applications on the desktop. You might be
wondering how it appears. Obviously, it’s not a C based language. You don't even need a main section.
Take an example:
Let’s take an example creating a program in Ruby. Put the string as "Hello, world!”. No need to put any
semicolon at the end of the line, but this doesn't really tell us too much about it. Add a few more lines
here. Now define a function here. You can use comments but not with the two forward slashes; rather you
can use the pound sign or hash sign. After creating this function or method, use the word ‘def’ for defines.
You’re calling this welcome saying it takes a parameter of name and then it’s going to output hello
comma, whatever the name is. You don't use curly braces to mark the end of the function. You can use
the word ‘end’ instead and then below it write x = bob.
# this is a comment
end
x = "bob"
welcome ( x )
82 | P a g e
This is how you can create a variable which is a string and then you can call this function or method
saying welcome and passing in x. Again, you don't have to worry about memorizing syntax. The point
here is that you should focus on the overall structure. Ruby is a fairly compact language to get started
with easily.
If you're working on a Mac you should have the Ruby interpreter. There are several different
editors that you might use like TextMate and RubyMine on the Mac, Aptana is cross-platform.
You can refer to the website rubyonrails.org, which is all about that web based framework that
uses the Ruby language. Another website is ruby-lang.org. Both of these have fantastic resources
to get started with Ruby; either the Ruby language by itself or as a web based framework.
Python
Next, let's talk a little bit about Python. This is a language that's often regarded same as Ruby as it's a very
clean and concise object oriented language. You can easily figure out that it's another language that comes
with a really large library. In fact, this is one of the benefits of this language. There is a whole bunch of
prewritten functionality that you can just type into. Again, same as in Ruby, it is an interpreted language.
It has a garbage collection property as well. You don't have to worry about compilers and neither about
memory management.
Usage of Python:
One of the great things about Python is that it’s a totally cross-platform language. There are Python
interpreters on every platform that are well supported. It's a very simple and easy language to build web
applications with. It can be tied perfectly with the Apache web server and it has lots of uses as an
embedded scripting language inside other applications. It uses a great deal for building applications and
web apps by companies like Google and Yahoo!
# this is a comment
83 | P a g e
def sayhello( name ):
sayhello(‘bob')
Dissimilarities to Ruby:
Unlike Ruby, this is a very concise readable language that's easy to get started with. There are several
IDEs and editors that support Python. Two common ones are the Eclipse IDE with the extension PyDev
that helps for Python development and Komodo as well. Although you'll find many programmers' text
editors also supports Python.
Since python has found a great use as per information security folks, you have already created a
course on Python with Eclipse and InfoSec. Infosec stands for information security.
Python interpreters are available on every platform and actually built into recent additions of Mac
OS X also. The single best resource for getting started is through the website python.org. This is
the official Python website and has a great amount of installers, help, and tutorials which helps
you out to deal with this language.
Objective-C
Objective-C is one of the older languages on the list. It was invented in the early 80s as an object oriented
version of C. It is really the C language with some extra stuff added to it. That extra stuff makes it object-
oriented. One of the benefits of using Objective-C is, it comes with a large library referred to as Cocoa. It
is a compiled language and not interpreted. It doesn’t uses intermediate bytecode.
84 | P a g e
old history lesson. Objective-C was used by the company NeXTstep to build their operating system. Later
on it was bought by Apple and since then it extracted out a lot of things from it. When you see examples
of programs written in Objective-C, you will come across this NS letter all over the place.
#import <Foundation/Foundation.h>
[pool drain] ;
return 0 ;
Another odd feature that is very common to come across is the @ sign in front of a string. That's how you
tell Objective-C that you are denoting a string object in that language. Same as C you also have a lot of
import statements. One of the powers of using Objective-C is that you can link to a bunch of big libraries
full of interesting code that you can get access to. Objective-C has been used so long by Apple that
they've built huge amounts of pre-written code. These can be used to build Macintosh desktop
applications, iPhone applications and these are enfolded under the general name of Cocoa. If you're
interested in writing applications that run natively on Apple hardware, then this is the language you
should look for.
If you're interested to get more detailed about the language, there is one key editor or IDE and that
is XCode. This is Apple's own integrated development environment for building desktop
applications or iPad or iPhone applications or even just simple command line applications. You
can refer to this key website developer.apple.com. Here you’ll find downloads, reference guides,
tutorials and a lot of information about getting started with Objective-C.
85 | P a g e
With Objective-C, you have Cocoa or Cocoa Touch depending on if you're focused on the desktop or on
the iPhone and iPod. Python also have the standard library. Another thing that programmers know and
non-programmers don't is not about the language. It's about the libraries. When you are learning C#, you
might wonder if there was something that can help you to connect with database. It was the .NET
framework.
While experimenting with Objective-C, you might have come across a built-in video player class
for the iPhone. It's already been written by Apple and even there in the Cocoa Touch framework.
You can link to it and use it.
If you're using Python and your program needs to be save in a file and wants to unzip that file up.
You’ll find that in the Python Standard Library.
As a beginner when you fall upon hundreds or even thousands of prewritten classes available to the
language, you shouldn’t worry because you need to memorize them. It's like being in a real library. The
point is not that you must read every book. The point is to navigate through the stacks and find whether
what you're looking for exists or not and if so, how do you get to it. You can refer to reference guides
anytime usually on websites whenever you face any problem.
Python Standard Library and the iOS Developer Library have their own set of classes. That’s why
learning more about the language is afr more important than focusing on the syntax.
86 | P a g e