M1-IT 112 - Program Logic Formulation
M1-IT 112 - Program Logic Formulation
Introduction to Computer Program Now, try to map the situation with a computer program. The
above sequence of instructions is actually a Human
Before getting into computer programming, let us first Program written in English Language, which instructs on
understand computer programs and what they do. how to reach KFC from a given starting point. This same
sequence could have been given in Spanish, Hindi, Arabic,
A computer program is a sequence of instructions written
or any other human language, provided the person seeking
using a Computer Programming Language to perform a
direction knows any of these languages.
specified task by the computer.
The two important terms that we have used in the above
definition are − ACTIVITY 1: Try to give several steps to follow in cooking
scrambled egg. Write the steps inside the box.
Sequence of instructions
Computer Programming Language
To understand these terms, consider a situation when
someone asks you about how to go to a nearby KFC. What
exactly do you do to tell him the way to go to KFC?
You will use Human Language to tell the way to go to KFC,
something as follows −
First go straight, after half kilometer, take left from the red
light and then drive around one kilometer and you will find
KFC at the right.
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
2. Drive half kilometer
33
3. Take left
4. Drive around one kilometer
5. Search for KFC at your right side
1
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
ACTIVITY 2: Try to give several steps to follow to reach AVC. Computer program instructions are also called
Write the steps inside the box. 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).
2
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
3
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
We assume you are well aware of English Language, which We will explain all these elements in subsequent chapters
is a well-known Human Interface Language. English has a with examples using different programming languages. First,
predefined grammar, which needs to be followed to write we will try to understand the meaning of all these terms in
English statements in a correct way. Likewise, most of the general and then, we will see how these terms can be used
Human Interface Languages (Hindi, English, Spanish, in different programming languages.
French, etc.) are made of several elements like verbs, nouns,
This tutorial has been designed to give you an idea about the
adjectives, adverbs, propositions, and conjunctions, etc.
following most popular programming languages −
Similar to Human Interface Languages, Computer
Programming Languages are also made of several elements. C Programming
We will take you through the basics of those elements and Java Programming
make you comfortable to use them in various programming
Python Programming
languages. These basic elements include −
A major part of the tutorial has been explained by taking C as
Programming Environment programming language and then we have shown how similar
Basic Syntax concepts work in Java and Python. So after completion of
this tutorial, you will be quite familiar with these popular
Data Types programming languages.
Variables
Keywords
Basic Operators Computer Programming – ENVIRONMENT
Decision Making Though Environment Setup is not an element of any
Loops Programming Language, it is the first step to be followed
before setting on to write a program.
Numbers
Characters When we say Environment Setup, it simply implies a base on
top of which we can do our programming. Thus, we need to
Arrays
have the required software setup, i.e., installation on our PC
Strings which will be used to write computer programs, compile, and
Functions execute them. For example, if you need to browse Internet,
File I/O then you need the following setup on your machine −
4
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
5
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
You write your computer program using your favorite The following flow diagram gives an illustration of the process
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.
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
before you start programming.
Interpreter
6
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
programs line by line and execute them directly without any Computer Programming – BASIC SYNTAX
further conversion.
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.
If you are not able to set up any editor, compiler, or int main() {
interpreter on your machine, then tutorialspoint.com provides /* printf() function to write Hello,
a facility to compile and run almost all the programs online World! */
with an ease of a single click. printf( "Hello, World!" );
So do not worry and let's proceed further to have a thrilling }
experience to become a computer programmer in simple and which produces the following result −
easy steps.
Hello, World!
This little Hello World program will help us understand
various basic concepts related to C Programming.
7
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
8
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
9
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
Hello, World! called demo. Finally, we execute the produced binary demo
Hello, World! as follows −
Here, we are using a new line character \n in the first printf() $./demo
function to create a new line. Let us see what happens if we
which produces the following result −
do not use this new line character −
Hello, World!
Here, when we execute the binary a.out file, the computer
#include <stdio.h> enters inside the program starting from main() and
encounters a printf() statement. Keep a note that the line
int main() { inside /*....*/ is a comment and it is filtered at the time of
/* printf() function to write Hello, compilation. So printf() function instructs the computer to
World! */ print the given line at the computer screen. Finally, it
printf( "Hello, World!" ); encounters a right curly brace which indicates the end of
printf( "Hello, World!" ); main() function and exits the program.
}
This program will produce the following result − Syntax Error
Hello, World! Hello, World! If you do not follow the rules defined by the programing
We will learn identifiers and keywords in next few chapters. language, then at the time of compilation, you will get syntax
errors and the program will not be compiled. From syntax
Program Explanation point of view, even a single dot or comma or a single
Let us understand how the above C program works. First of semicolon matters and you should take care of such small
all, the above program is converted into a binary format using syntax as well. In the following example, we have skipped a
C compiler. So let’s put this code in test.c file and compile it semicolon, let's try to compile the program −
as follows − #include <stdio.h>
$gcc test.c -o demo
main() {
If there is any grammatical error (Syntax errors in computer printf("Hello, World!")
terminologies), then we fix it before converting it into binary }
format. If everything goes fine, then it produces a binary file
10
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1
This program will produce the following result − # print function to write Hello, World! */
main.c: In function 'main': print "Hello, World!"
main.c:7:1: error: expected ';' before '}' which produces the following result −
token
} Hello, World!
^ Hope you noted that for C and Java examples, first we are
So the bottom-line is that if you are not following proper compiling the programs and then executing the produced
syntax defined by the programming language in your binaries, but in Python program, we are directly executing it.
program, then you will get syntax errors. Before attempting As we explained in the previous chapter, Python is an
another compilation, you will need to fix them and then interpreted language and it does not need an intermediate
proceed. step called compilation.
Python does not require a semicolon (;) to terminate a
Hello World Program in Java statement, rather a new line always means termination of the
statement.
Following is the equivalent program written in Java. This
program will also produce the same result Hello, World!. ACTIVITY 3: Try to write a simple program which displays the
given output below. Write your code inside the box below.
public class HelloWorld {
public static void main(String []args) { Output:
/* println() function to write Hello,
World! */ My name is __________________.
System.out.println("Hello, World!");
I am ________________ years old.
}
} I came from __________________.
which produces the following result − I love programming.
Hello, World!
12