0% found this document useful (0 votes)
12 views9 pages

Introduction To Computer Programming

Uploaded by

marymacharia2044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Introduction To Computer Programming

Uploaded by

marymacharia2044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MODULE 1: INTRODUCTION OF COMPUTER PROGRAMMNG

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

 Computer Programming Language

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 −

print "Hello, World!"

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.

Introduction to Computer Programming

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 –

Examples of programming languages and their uses:

Language Uses

C System programming, Embedded systems

Java Enterprise applications, Android development

Python Data science, AI, Web development

JavaScript Web development, Frontend scripting

PHP Web backend development

Swift iOS app development


Importance of Programming:

 Automates repetitive tasks.

 Enhances problem-solving skills.

 Provides career opportunities in technology.

 Enables software development for various applications.

Levels of Programming Languages

Programming languages are categorized into:

 Low-Level Languages: Machine language, Assembly language

 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.

Algorithm: Add Two Numbers


1. Start
2. Input two numbers (num1 and num2)
3. Compute the sum: sum = num1 + num2
4. Output the sum
5. End

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.

Why use Flowcharts?


Flowcharts are used due to the numerous amount of benefits they provide. Below are some of the important reasons
to use flowcharts:
 They provide clarity and simplification to the complex processes and algorithms, which in turn helps other people to
understand them easily.
 Flowcharts provide a universal visual language that can be understood by anyone across different teams and helps
reduce miscommunications.
 They are an optimal solution for documenting standard operating procedures, workflows, or business processes. This
makes it easier to train new employees.
 Flowcharts help in increasing the visualization of the problem being solved which enables more informed and data-
driven choices.

Symbols used in Flowchart Designs


1. Terminal/Terminator
The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A pause/halt is generally used in a program
logic under some error conditions. Terminal is the first and last symbols in the flowchart.

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

Rules For Creating a Flowchart


A flowchart is a graphical representation of an algorithm. It should follow some rules while creating a flowchart
 Rule 1: Flowchart opening statement must be ‘start’ keyword.
 Rule 2: Flowchart ending statement must be ‘end’ keyword.
 Rule 3: All symbols in the flowchart must be connected with an arrow line.
 Rule 4: Each decision point should have two or more distinct outcomes.
 Rule 5: Flow should generally move from top to bottom or left to right.

Example of a Flowchart
Draw a flowchart to input two numbers from the user and display the largest of two numbers.

Example Flowchart

Below is the explanation of the above flowchart:


 Start: The process begins with the Start symbol, indicating the start of the program.
 Input num1: The first number, represented as num1, is entered.
 Input num2: The second number, represented as num2, is entered.
 Decision (num1 > num2): A decision point checks if num1 is greater than num2.
o If True, the process moves to the next step where num1 will be displayed.
o If False, the process moves to display num2.
 Stop: The process ends with the Stop symbol, signaling the conclusion of the program.

Advantages of using a Flowchart

 Flowcharts are a better way of communicating the logic of the system.


 Flowcharts act as a guide for blueprint during program designed.
 Flowcharts help in debugging process.
 With the help of flowcharts programs can be easily analyzed.
 They provide better documentation.
 Flowcharts serve as a good proper documentation.

Disadvantages of using a Flowchart

 It is difficult to draw flowcharts for large and complex programs.


 There is no standard to determine the amount of detail.
 It is very difficult to modify the Flowchart.
 king a flowchart is costly.
Computer Programming – Environment

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 −

 A working Internet connection to connect to the Internet


 A Web browser such as Internet Explorer, Chrome, Safari, etc.

Similarly, you will need the following setup to start with programming using any programming language.

 A text editor to create computer programs.


 A compiler to compile the programs into binary format.
 An interpreter to execute the programs directly.

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.

Hello World Program in C

#include <stdio.h>

int main() {

/* printf() function to write Hello, World! */

printf( "Hello, World!" );

which produces the following result −

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.

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

New Line To create a new line \n

Tab To create a tab. \t

Space To create a space. empty space

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() {

/* printf() function to write Hello, World! */

printf( "Hello, World!" );

which produces the following result −

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

\t/* printf() function to write Hello, World! */

\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() {

/* printf() function to write Hello, World! */

printf( "Hello, World!\n" );

printf( "Hello, World!" );

This program will produce the following result −

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() {

/* printf() function to write Hello, World! */

printf( "Hello, World!" );

printf( "Hello, World!" );

This program will produce the following result −

Hello, World! Hello, World!

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!")

This program will produce the following result −

main.c: In function 'main':

main.c:7:1: error: expected ';' before '}' token

You might also like