Introduction To Computer Programming
Introduction To Computer Programming
OVERVIEW
The two important terms that we have used in the above definition are:
• Sequence of instructions
Here, you have used English Language to give several steps to be taken to reach KFC.
If they are followed in the following sequence, then you will reach KFC:
1. Go straight
3. Take left
Now, try to map the situation with a computer program. The above sequence of
instructions is actually a Human Program written in English Language, which
instructs on how to reach KFC from a given starting point. This same sequence could
have been given in Spanish, Hindi, Arabic, or any other human language, provided
the person seeking direction knows any of these languages.
Now, let's go back and try to understand a computer program, which is a sequence
of instructions written in a Computer Language to perform a specified task by the
computer. Following is a simple program written in Python programming Language:
The above computer program instructs the computer to print "Hello, World!" on the
computer screen.
• Computer program instructions are also called program source code and
computer programming is also called program coding.
If you understood what a computer program is, then we will say: the act of
writing computer programs is called computer programming.
• Java
• C
• C++
• Python
• PHP
• Perl
• Ruby
Uses of Computer Programs
Today computer programs are being used in almost every field, household,
agriculture, medical, entertainment, defense, communication, etc. Listed below are a
few applications of computer programs:
• Computer programs are being used to develop graphics and special effects in
movie making.
• Computer programs are being used to perform Ultrasounds, X-Rays, and other
medical examinations.
• Computer programs are being used in our mobile phones for SMS, Chat, and
voice communication.
Computer Programmer
Someone who can write computer programs or in other words, someone who can do
computer programming is called a Computer Programmer.
• C Programmer
• C++ Programmer
• Java Programmer
• Python Programmer
• PHP Programmer
• Perl Programmer
• Ruby Programmer
Algorithm
Thus, a computer programmer lists down all the steps required to resolve a problem
before writing the actual code. Following is a simple example of an algorithm to find
out the largest number from a given list of numbers:
The above algorithm has been written in a crude way to help beginners understand
the concept. You will come across more standardized ways of writing computer
algorithms as you move on to advanced levels of computer programming.
2. BASICS
We assume you are well aware of English Language, which is a well-known Human
Interface Language. English has a predefined grammar, which needs to be followed
to write English statements in a correct way. Likewise, most of the Human Interface
Languages (Hindi, English, Spanish, French, etc.) are made of several elements like
verbs, nouns, adjectives, adverbs, propositions, and conjunctions, etc.
• Programming Environment
• Basic Syntax
• Data Types
• Variables
• Keywords
• Basic Operators
• Decision Making
• Loops
• Numbers
• Characters
• Arrays
• Strings
• Functions
• File I/O
We will explain all these elements in subsequent chapters with examples using
different programming languages. First, we will try to understand the meaning of all
these terms in general and then, we will see how these terms can be used in different
programming languages.
This tutorial has been designed to give you an idea about the following most popular
programming languages:
• C Programming
• Java Programming
• Python Programming
A major part of the tutorial has been explained by taking C as programming language
and then we have shown how similar concepts work in Java and Python. So after
completion of this tutorial, you will be quite familiar with these popular programming
languages.
3. ENVIORNMENT
When we say Environment Setup, it simply implies a base on top of which we can do
our programming. Thus, we need to have the required software setup, i.e.,
installation on our PC which will be used to write computer programs, compile, and
execute them. For example, if you need to browse Internet, then you need the
following setup on your machine:
Similarly, you will need the following setup to start with programming using any
programming language.
A text editor is a software that is used to write computer programs. Your Windows
machine must have a Notepad, which can be used to type programs. You can launch
it by following these steps:
If you are a Mac user, then you will have TextEdit or you can install some other
commercial editor like BBEdit to start with.
Compiler
You write your computer program using your favorite programming language and
save it in a text file called the program file.
Now let us try to get a little more detail on how the computer understands a program
written by you using a programming language. Actually, the computer cannot
understand your program directly given in the text format, so we need to convert
this program in a binary format, which can be understood by the computer.
The conversion from text program to binary file is done by another software called
Compiler and this process of conversion from text formatted program to binary format
file is called program compilation. Finally, you can execute binary file to perform the
programmed task.
We are not going into the details of a compiler and the different phases of compilation.
Interpreter
We just discussed about compilers and the compilation process. Compilers are
required in case you are going to write your program in a programming language that
needs to be compiled into binary format before its execution.
There are other programming languages such as Python, PHP, and Perl, which do not
need any compilation into binary format, rather an interpreter can be used to read
such programs line by line and execute them directly without any further conversion.
So, if you are going to write your programs in PHP, Python, Perl, Ruby, etc., then you
will need to install their interpreters before you start programming.
4. BASIC SYNTAX
Online Compilation
If you are not able to set up any editor, compiler, or interpreter on your machine,
then tutorialspoint.com provides a facility to compile and run almost all the programs
online with an ease of a single click.
So do not worry and let’s proceed further to have a thrilling experience to become a
computer programmer in simple and easy steps.
Let’s start with a little coding, which will really make you a computer programmer.
We are going to write a single-line computer program to write Hello, World! on your
screen. Let’s see how it can be written using different programming languages.
Try the following example using our online compiler option available at
codelearning4.wordpress.com.
For most of the examples given in this tutorial, you will find a Try it option in our
website code sections at the top right corner that will take you to the online compiler.
Try to change the content inside printf(), i.e., type anything in place of Hello World!
and then check its result. It just prints whatever you keep inside the two double
quotes.
#include <stdio.h>
main()
{
/* printf() function to write Hello, World! */
printf( "Hello, World!" );
}
This little Hello World program will help us understand various basic concepts related
to C Programming.
Program Entry Point
For now, just forget about the #include <stdio.h> statement, but keep a note that
you have to put this statement at the top of a C program.
Every C program starts with main(), which is called the main function, and then it is
followed by a left curly brace. The rest of the program instruction is written in between
and finally a right curly brace ends the program.
The coding part inside these two curly braces is called the program body. The left
curly brace can be in the same line as main(){ or in the next line like it has been
mentioned in the above program.
Functions
Functions are small units of programs and they are used to carry out a specific task.
For example, the above program makes use of two functions: main() and printf().
Here, the function main() provides the entry point for the program execution and the
other function printf() is being used to print an information on the computer screen.
You can write your own functions which we will see in a separate chapter, but C
programming itself provides various built-in functions like main(), printf(), etc., which
we can use in our programs based on our requirement.
Some of the programming languages use the word sub-routine instead of function,
but their functionality is more or less the same.
Comments
A C program can have statements enclosed inside /*.....*/. Such statements are
called comments and these comments are used to make the programs user friendly
and easy to understand. The good thing about comments is that they are completely
ignored by compilers and interpreters. So you can use whatever language you want
to write your comments.
Whitespaces
When we write a program using any programming language, we use various printable
characters to prepare programming statements. These printable characters are a, b,
c,......z, A, B, C,.....Z, 1, 2, 3,...... 0, !, @, #, $, %, ^, &, *, (, ), -, _, +, =, \, |,
{, }, [, ], :, ;, <, >, ?, /, \, ~. `. ", '. Hope I'm not missing any printable characters
from your keyboard.
Apart from these characters, there are some characters which we use very frequently
but they are invisible in your program and these characters are spaces, tabs (\t), new
lines(\n). These characters are called whitespaces.
These three important whitespace characters are common in all the programming
languages and they remain invisible in your text document:
A line containing only whitespace, possibly with a comment, is known as a blank line,
and a C compiler totally ignores it.