0% found this document useful (0 votes)
2 views

M1-IT 112 - Program Logic Formulation

PLF1

Uploaded by

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

M1-IT 112 - Program Logic Formulation

PLF1

Uploaded by

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

IT 112 – PROGRAM LOGIC FORMULATION MODULE 1

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

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.
Now, let's go back and try to understand a computer
program, which is a sequence of instructions written in a As we mentioned earlier, there are hundreds of programming
Computer Language to perform a specified task by the languages, which can be used to write computer programs
computer. Following is a simple program written and following are a few of them −
in Python programming Language −
 Java
print "Hello, World!"
 C
 C++
The above computer program instructs the computer to print  Python
"Hello, World!" on the computer screen.
 PHP
 A computer program is also called a computer  Perl
software, which can range from two lines to millions
 Ruby
of lines of instructions.

2
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1

Uses of Computer Programs  Ruby Programmer

Today computer programs are being used in almost every Algorithm


field, household, agriculture, medical, entertainment,
defense, communication, etc. Listed below are a few From programming point of view, an algorithm is a step-by-
applications of computer programs − step procedure to resolve any problem. An algorithm is an
effective method expressed as a finite set of well-defined
 MS Word, MS Excel, Adobe Photoshop, Internet
instructions.
Explorer, Chrome, etc., are examples of computer
programs. Thus, a computer programmer lists down all the steps
 Computer programs are being used to develop required to resolve a problem before writing the actual code.
graphics and special effects in movie making. Following is a simple example of an algorithm to find out the
 Computer programs are being used to perform largest number from a given list of numbers −
Ultrasounds, X-Rays, and other medical examinations.
1. Get a list of numbers L1, L2, L3....LN
 Computer programs are being used in our mobile
2. Assume L1 is the largest, Largest = L1
phones for SMS, Chat, and voice communication. 3. Take next number Li from the list and do
the following
Computer Programmer 4. If Largest is less than Li
5. Largest = Li
Someone who can write computer programs or in other 6. If Li is last number from the list then
words, someone who can do computer programming is 7. Print value stored in Largest and come out
called a Computer Programmer. 8. Else repeat same process starting from
Based on computer programming language expertise, we step 3
can name a computer programmers as follows − The above algorithm has been written in a crude way to help
beginners understand the concept. You will come across
 C Programmer more standardized ways of writing computer algorithms as
 C++ Programmer you move on to advanced levels of computer programming.
 Java Programmer
 Python Programmer
Computer Programming – BASICS
 PHP Programmer
 Perl Programmer

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

 A working Internet connection to connect to the Internet Text Editor


 A Web browser such as Internet Explorer, Chrome,
Safari, etc. A text editor is a software that is used to write computer
programs. Your Windows machine must have a Notepad,
If you are a PC user, then you will recognize the following which can be used to type programs. You can launch it by
screenshot, which we have taken from the Internet Explorer following these steps −
while browsing tutorialspoint.com.
Start Icon → All Programs → Accessories →
Notepad → Mouse Click on Notepad
It will launch Notepad with the following window −

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.
You can use this software to type your computer program
 An interpreter to execute the programs directly. and save it in a file at any location. You can download and
In case you don’t have sufficient exposure to computers, you install other good editors like Notepad++, which is freely
will not be able to set up either of these software. So, we available.
suggest you take the help from any technical person around
If you are a Mac user, then you will have TextEdit or you can
you to set up the programming environment on your machine
install some other commercial editor like BBEdit to start with.
from where you can start. But for you, it is important to
understand what these items are.
Compiler?

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

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

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.

Hello World Program in C

Try the following example using our online compiler option


available at www.compileonline.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.
So, if you are going to write your programs in PHP, Python,
Perl, Ruby, etc., then you will need to install their interpreters Try to change the content inside printf(), i.e., type anything in
before you start programming. place of Hello World! and then check its result. It just prints
whatever you keep inside the two double quotes.
Online Compilation #include <stdio.h>

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

Program Entry Point Comments


For now, just forget about the #include <stdio.h> statement, A C program can have statements enclosed inside /*.....*/.
but keep a note that you have to put this statement at the top Such statements are called comments and these comments
of a C program. are used to make the programs user friendly and easy to
understand. The good thing about comments is that they are
Every C program starts with main(), which is called the main
completely ignored by compilers and interpreters. So you can
function, and then it is followed by a left curly brace. The rest
use whatever language you want to write your comments.
of the program instruction is written in between and finally a
right curly brace ends the program. Whitespaces
The coding part inside these two curly braces is called the When we write a program using any programming language,
program body. The left curly brace can be in the same line as we use various printable characters to prepare programming
main(){ or in the next line like it has been mentioned in the statements. These printable characters are a, b, c,......z, A,
above program. B, C,.....Z, 1, 2, 3,...... 0, !, @, #, $, %, ^, &, *, (, ), -, _, +, =, \,
|, {, }, [, ], :, ;, <, >, ?, /, \, ~. `. ", ' . Hope I'm not missing any
Functions
printable characters from your keyboard.
Functions are small units of programs and they are used to
Apart from these characters, there are some characters
carry out a specific task. For example, the above program
which we use very frequently but they are invisible in your
makes use of two functions: main() and printf(). Here, the
program and these characters are spaces, tabs (\t), new
function main() provides the entry point for the program
lines(\n). These characters are called whitespaces.
execution and the other function printf() is being used to print
an information on the computer screen. These three important whitespace characters are common in
all the programming languages and they remain invisible in
You can write your own functions which we will see in a
your text document –
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.

8
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1

Whitespace Explanation Representation Hello, World!


If we make all these whitespace characters visible, then the
New Line To create a new line \n above program will look like this and you will not be able to
compile it −
#include <stdio.h>\n
Tab To create a tab. \t \n
int main()\n
{
Space To create a space. empty space \n
\t/* printf() function to write Hello,
World! */
\n
A line containing only whitespace, possibly with a comment, \tprintf(\t"Hello, World!"\t);\n
is known as a blank line, and a C compiler totally ignores it. \n
Whitespace is the term used in C to describe blanks, tabs, }\n
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 Semicolons
compiler will ignore them at the time of compilation. Every individual statement in a C Program must be ended
#include <stdio.h> with a semicolon (;), for example, if you want to write "Hello,
World!" twice, then it will be written as follows −
int main() {
#include <stdio.h>
/* printf() function to write Hello,
int main() {
World! */
/* printf() function to write Hello,
printf( "Hello, World!" ); World! */
printf( "Hello, World!\n" );
} printf( "Hello, World!" );
}
which produces the following result −
This program will produce the following result −

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!

Hello World Program in Python

Following is the equivalent program written in Python. This


program will also produce the same result Hello, World!.
11
IT 112 – PROGRAM LOGIC FORMULATION MODULE 1

I LOVE ABRA VALLEY COLLEGES


CODE:

12

You might also like