Introduction To Computer Programming
Introduction To Computer Programming
Before getting into computer programming, let us first understand computer programs and what they do.
A computer program is a sequence of instructions written using a Computer Programming Language to perform a specified task
by the computer.
The two important terms that we have used in the above definition are −
Sequence of instructions
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.
A computer program is also called a computer software, which can range from two lines to millions of lines of
instructions.
Computer program instructions are also called program source code and computer programming is also called
program coding.
A computer without a computer program is just a dump box; it is programs that make computers active.
As we have developed so many languages to communicate among ourselves, computer scientists have developed several
computer-programming languages to provide instructions to the computer (i.e., to write computer programs). We will see
several computer programming languages in the subsequent chapters.
If you understood what a computer program is, then we will say: the act of writing computer programs is called computer
programming.
As we mentioned earlier, there are hundreds of programming languages, which can be used to write computer programs and
following are a few of them –
Language Uses
Middle-Level Languages: C, C++ (balance between low-level control and high-level abstraction)
High-Level Languages: Python, Java, JavaScript (easier for humans to read and write)
Machine Language
Machine language is the lowest-level programming language, consisting entirely of binary code (0s and 1s) that a
computer's CPU can execute directly. It is hardware-dependent and difficult for humans to read or write.
Assembly language is a low-level programming language that uses symbolic representations (mnemonics) instead of
binary code, making it slightly easier to write than machine language. It requires an assembler to translate it into
machine code.
Algorithm
From programming point of view, an algorithm is a step-by-step procedure to resolve any problem. An algorithm is an
effective method expressed as a finite set of well-defined instructions.
Thus, a computer programmer lists down all the steps required to resolve a problem before writing the actual code.
Flowcharts
Flowcharts are the visual representations of an algorithm or a process. Flowcharts use symbols/shapes like arrows,
rectangles, and diamonds to properly explain the sequence of steps involved in the algorithm or process. Flowcharts
have their use cases in various fields such as software development, business process modeling, and engineering.
Terminal/Terminator
2. Input/Output
A parallelogram denotes any function of input/output type. Program instructions that take input from input devices
and display output on output devices are indicated with parallelogram in a flowchart.
Input/Output
3. Action/Process
A box represents arithmetic instructions, specific action or operation that occurs as a part of the process. All arithmetic
processes such as adding, subtracting, multiplication and division are indicated by action/process symbol.
Action/Process
4. Decision
Diamond symbol represents a decision point. Decision based operations such as yes/no question or true/false are
indicated by diamond in flowchart.
Decision
7. Flow lines
Flow lines indicate the exact sequence in which instructions are executed. Arrows represent the direction of flow of
control and relationship among different symbols of flowchart.
Flow lines
Example of a Flowchart
Draw a flowchart to input two numbers from the user and display the largest of two numbers.
Example Flowchart
Though Environment Setup is not an element of any Programming Language, it is the first step to be followed before setting on
to write a program.
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.
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.
So, if you are going to write your program in any such language, which needs compilation like C, C++, Java and Pascal, etc., then
you will need to install their compilers.
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.
We are going to write a single-line computer program to write Hello, World! on your screen.
#include <stdio.h>
int main() {
Hello, World!
This little Hello World program will help us understand various basic concepts related to C Programming.
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.
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 −
Whitespace Explanation Representation
A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline characters, and comments. So you can write printf("Hello,
World!" ); as shown below. Here all the created spaces around "Hello, World!" are useless and the compiler will ignore them at
the time of compilation.
#include <stdio.h>
int main() {
Hello, World!
If we make all these whitespace characters visible, then the above program will look like this and you will not be able to
compile it −
#include <stdio.h>\n
\n
int main()\n
\n
\n
\tprintf(\t"Hello, World!"\t);\n
\n
}\n
Semicolons
Every individual statement in a C Program must be ended with a semicolon (;), for example, if you want to write "Hello, World!"
twice, then it will be written as follows −
#include <stdio.h>
int main() {
Hello, World!
Hello, World!
Here, we are using a new line character \n in the first printf() function to create a new line. Let us see what happens if we do
not use this new line character −
#include <stdio.h>
int main() {
Syntax Error
If you do not follow the rules defined by the programing language, then at the time of compilation, you will get syntax errors
and the program will not be compiled. From syntax point of view, even a single dot or comma or a single semicolon matters and
you should take care of such small syntax as well. In the following example, we have skipped a semicolon, let's try to compile
the program −
#include <stdio.h>
main() {
printf("Hello, World!")