0% found this document useful (0 votes)
5 views6 pages

Programming Environment Essentials

The document outlines the essential software setup required for programming, including a text editor, compiler, and interpreter. It explains the roles of functions, comments, whitespace, and control structures in programming, as well as the importance of data structures like lists and hash maps for efficient data management. Additionally, it discusses the differences between compiled and interpreted languages, emphasizing the need for syntax correctness in programming.

Uploaded by

ph.kariyawasam
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)
5 views6 pages

Programming Environment Essentials

The document outlines the essential software setup required for programming, including a text editor, compiler, and interpreter. It explains the roles of functions, comments, whitespace, and control structures in programming, as well as the importance of data structures like lists and hash maps for efficient data management. Additionally, it discusses the differences between compiled and interpreted languages, emphasizing the need for syntax correctness in programming.

Uploaded by

ph.kariyawasam
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

Environment

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.

Text Editor
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.

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.

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.

Program Entry Point


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.
But python doesn’t need one

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 but languages itself provides various built-in functions
Some of the programming languages use the word sub-routine instead of function, but their functionality
is more or less the same.

Comments
A program can have statements enclosed. (eg – in C -> inside /*.....*/., in python -> #) 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. 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.

Semicolons
Program Explanation
Let us understand how the above C program works. First of all, the above program is converted into a
binary format using C compiler. So let’s put this code in test.c file and compile it as follows −
$gcc test.c -o demo
If there is any grammatical error (Syntax errors in computer terminologies), then we fix it before converting
it into binary format. If everything goes fine, then it produces a binary file called demo. Finally, we execute
the produced binary demo as follows −
$./demo
which produces the following result −
Hello, World!
Here, when we execute the binary [Link] file, the computer enters inside the program starting from main()
and encounters a printf() statement. Keep a note that the line inside /*....*/ is a comment and it is filtered at
the time of compilation. So printf() function instructs the computer to print the given line at the computer
screen. Finally, it encounters a right curly brace which indicates the end of main() function and exits the
program.
AD

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 −

Hope you noted that for C and Java examples, first we are compiling the programs and then executing the
produced binaries, but in Python program, we are directly executing it. As we explained in the previous
chapter, Python is an interpreted language and it does not need an intermediate step called compilation.
Python does not require a semicolon (;) to terminate a statement, rather a new line always means
termination of the statement.

Control structures
ou see, when a program runs, the code is read by the computer line by line
(from top to bottom, and from left to right), just like you would read a book.
At some point, the program may reach a situation where it needs to make
a decision such as jump to a different part of the program or re-run a
certain piece again. These decisions that affect the flow of the program’s
code are known as a Control Structures.
Control Structures are the blocks that analyze variables and choose
directions in which to go based on given parameters.

he basic Control Structures in programming languages are:


 Conditionals (or Selection): which are used to execute one or more
statements if a condition is met.

 Loops (or Iteration): which purpose is to repeat a statement a certain


number of times or while a condition is fulfilled.

1) If Statements

2) If-Else Statements

This Control Structure allows a program to follow alternative paths of


execution, whether a condition is met or not.

Loops

“Loop statements” are nothing more than the automation of multi-step


processes by organizing sequences of actions, and grouping the parts that
need to be repeated.

1. “For Loops”: are the ones that execute for a prescribed number of
times, as controlled by a counter or an index.

2. “While Loops” and “Repeat Loops”: are based on the onset and
verification of a logical condition. The condition is tested at the start or
the end of the loop construct.
3. Unlike “If statements”, in which a condition tested as TRUE executes
an expression only once and ends, “While Loops” are iterative
statements that execute some expression over and over again until
the condition becomes FALSE. If the condition never turns out to be
FALSE, the “While Loop” will go on forever and the program will crash.
The other way around, if the condition test results FALSE in the
beginning of the loop, the expression will never get executed.

Data structures
In computer science, a data structure is a particular way of storing and
organizing data in a computer so that it can be used efficiently.
If we need to store 10 contacts, we would probably define 10 variables, right?
String contact1, contact2, contact3, contact4, contact5,
contact6, contact7, contact8, contact9, contact10;
contact1 = "John Smith ([Link]@[Link])";
contact2 = "Jane Smith ([Link]@[Link])";
contact3 = //... etc.
In the world of programming, this is just a terrible way of trying to store 10
different variables. This is because of two main reasons:

1. The sheer amount of text that you'll need to write in your program
o Sure, right now we only have 10 contacts, so it's not too bad, but what if
we had 1,000 contacts! Imagine typing that out a thousand times! Forget
about it!
2. The flexibility of code
o If we need to add another contact, we wouldn't be able to do it
without manually editing our code. We would have to go into our code,
physically write out contact11, and then try to store whatever information is
needed into the new variable. This is just crazy talk!

So, what is the RIGHT WAY?

I'm glad you asked, it's a data structure of course!

In this case, we have a list of contacts, and with Java there is a data structure
called a List! Okay, so what does it look like? Here's the code:

List contacts = new ArrayList();


[Link]("John Smith ([Link]@[Link])");
[Link]("Jane Smith ([Link]@[Link])");
Voila, we've added another contact to our contacts list! So, you may be saying, the
right way looks like just as much typing as the wrong way. You've got a point, but the
main difference is that with the first approach, you had to “create” 10 unique variables
(i.e. contact1, contact2, contact3, contact4…), but with the second approach, we only
created 1 variable (contacts). Because we only had to create 1 variable, and because
we can say [Link](someRandomContact), means that our code is much more flexible
and dynamic. When I say dynamic, I mean that the outcomes of the program can
change depending on what variables you give to it. That's really the key to
using data structures! We want our code to be as dynamic as possible, we want it
to be able to handle a bunch of situations without having to write more and more
code as the days go by.

In essence, a data structure is just a way to get around having to create tons of
variables.

The next one I'll talk about is the HashMap. This doesn't sound as straightforward as
our last data structure (the List), but it's really just a fancy name for a fairly simple
concept. So, what is a hash map? Here's a visual representation:
“Honda” -> “Civic”, “Prelude”
“Toyota” -> “Corolla”, “Celica”, “Rav4”
“Ford” -> “Focus”, “Mustang”
“Audi” -> “R8”
What you see here, is the make of a car on the left, which points to
different models of that make. This is known in the programming world as
a Key/Value pair. The key in this case, is the Make of the car (Toyota, Honda, Ford,
Audi), the value in the case, is the model (Civic, Corolla, Focus, R8). That is how
a HashMap works, and it's just another data structure! T

I just said that we have a HashMap data structure, and inside the HashMap we are storing
a List (which is another data structure). This is done on a fairly regular basis in the
programming world. We can use one data structure inside of another! So, if the
point of data structures is to help us minimize the number of variables we need to
create, then we are really saving ourselves the hassle of creating a lot of variables!

You might also like