UNIT II
DATA TYPES
A data type represents a type of the data which you can process using your computer program.
It can be numeric, alphanumeric, decimal, etc.
Data types are divided into two groups:
Primitive data types
A primitive data type specifies the size and type of variable values, and it has no
additional methods
includes byte, short, int, long, float, double, boolean and char
Non-primitive data types - such as String, Arrays and Classes
Data Type
Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal
digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal
digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
Example:
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
Non-primitive data types
Non-primitive data types are not predefined. These are user-defined data types created by
programmers. These data types are used to store multiple values.
There are four types of non-primitive data types in Java. They are as follows:
1. Class
2. Object
3. String
4. Array
String:
A string represents a sequence of characters for example "Javatpoint", "Hello world", etc.
String str = "You're the best";
Array:
An array is a data type which can store multiple homogenous variables i.e., variables of same
type in a sequence. They are stored in an indexed manner starting with index 0.
int a[10];
int m[3][3];
Class and objects:
A class is a user defined data type i.e. it is created by the user. It acts a template to the data
which consists of member variables and methods.
class ClassExample
{
}
Object is a real world entity. Instance of a class is called object.
ClassExample c;
INPUT/OUTPUT AND HIERARCHY OF OPERATIONS
Input
Input refers to any information or data that is sent to a computer for processing. It is often sent to
the computer from a device such as a keyboard, mouse, or another device.
Processing
Processing is the actions a processor performs when it receives information and is part of the
IPOS (input, processing, output, and storage) steps.
Arithmetic Operators
The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation,
and modulus operations.
Types of Arithmetic Operators
1. Binary Arithmetic Operators
2. Unary Arithmetic Operators
Binary Arithmetic Operators
The C binary arithmetic operators operate or work on two operands
Name of the
Operator Operator Arithmetic Operation Syntax
+ Addition Add two operands. x+y
– Subtraction Subtract the second operand from the first operand. x–y
* Multiplication Multiply two operands. x*y
/ Division Divide the first operand by the second operand. x/y
Calculate the remainder when the first operand is
% Modulus x%y
divided by the second operand.
Unary Arithmetic Operators
The unary arithmetic operators operate or work with a single operand.
Operator Symbol Operation
Decrement Operator -- Decreases the integer value of the variable by one.
Increment Operator ++ Increases the integer value of the variable by one.
Unary Plus Operator + Returns the value of its operand.
Unary Minus Operator – Returns the negative of the value of its operand.
HIERARCHY OF OPERATIONS
In a programming language, when an expression involves multiple operations, the hierarchy of
operations instructs the compilers and interpreters on the order in which the expression has to
be executed. Usually, the expression consists of operations like, addition, subtraction,
multiplication, division, exponents, and parentheses.
The hierarchy of operations is as follows:
• Parentheses ()
• Exponents
• Multiply or Divide
• Addition or Subtraction
Parentheses
Execution of the expressions inside the parentheses starting from the innermost parenthesis.
Other than parentheses the special characters like brackets [ ], braces { } and square root also fall
under this category.
Exponents
After solving the operations in the parentheses, choose the exponents for execution.
Multiplication or Division
The order of multiplication and division in an expression has the third priority. Perform the
multiplication or division operation in the left to right order.
Addition or Subtraction
The order of addition or subtraction in an expression has the last priority. Perform the addition or
subtraction operation in the left to right order.
Ex:
(5+10)-4*2
Ans= 7
PROGRAM DEVELOPMENT LIFE CYCLE (PDC PDLC)
The program development life cycle is a set of steps or phases that are used to develop a program
in any programming language.
Generally, the program development life cycle contains 6 phases, they are as follows….
Problem Definition
Problem Analysis
Algorithm Development
Coding & Documentation
Testing & Debugging
Maintenance
1. Problem Definition
In this phase, we define the problem statement and we decide the boundaries of the problem. In
this phase we need to understand the problem statement, what is our requirement, what should be
the output of the problem solution. These are defined in this first phase of the program
development life cycle.
2. Problem Analysis
In phase 2, we determine the requirements like variables, functions, etc. to solve the problem.
That means we gather the required resources to solve the problem defined in the problem
definition phase. We also determine the bounds of the solution.
3. Algorithm Development
During this phase, we develop a step by step procedure to solve the problem using the
specification given in the previous phase. This phase is very important for program development.
That means we write the solution in step by step statements.
4. Coding & Documentation
This phase uses a programming language to write or implement the actual programming
instructions for the steps defined in the previous phase. In this phase, we construct the actual
program. That means we write the program to solve the given problem using programming
languages like C, C++, Java, etc.,
5. Testing & Debugging
During this phase, we check whether the code written in the previous step is solving the specified
problem or not. That means we test the program whether it is solving the problem for various
input data values or not. We also test whether it is providing the desired output or not.
6. Maintenance
During this phase, the program is actively used by the users. If any enhancements found in this
phase, all the phases are to be repeated to make the enhancements. That means in this phase, the
solution (program) is used by the end-user. If the user encounters any problem or wants any
enhancement, then we need to repeat all the phases from the starting, so that the encountered
problem is solved or enhancement is added.
STRUCTURED PROGRAMMING
In structured programming, we sub-divide the whole program into small modules so that the
program becomes easy to understand. The purpose of structured programming is to linearize
control flow through a computer program so that the execution sequence follows the sequence in
which the code is written.
The languages that support Structured programming approach are:
C
C++
Java
C#
ALGORITHM
An algorithm is a step by step procedure used for solving a problem or performing a
computation.
Characteristics of an Algorithm
1. Finiteness, means it must always terminate after a finite number of steps.
2. Definiteness, means each step must be precisely defined and clear.
3. Input, means it has zero or more inputs, i.e., an algorithm can run without taking any input.
4. Output, means it has one or more outputs, i.e., an algorithm must produce atleast one output.
5. Effectiveness, means it is also generally expected to be effective.
An algorithm should be and unambiguous and independent of any programming code,
i.e., language independent.
Example: Addition of two numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop
Advantages and disadvantages of Algorithm
FLOWCHART
A flowchart is a type of diagram that represents a workflow or process. A flowchart can also
be defined as a diagrammatic representation of an algorithm, a step-by-step approach to
solving a task.
Flowchart symbols
Different types of boxes are used to make flowcharts flowchart Symbols. All the different
kinds of boxes are connected by arrow lines. Arrow lines are used to display the flow of
control. Let’s learn about each box in detail.
Symbol Representation
Symbol Name
Terminal/Terminator
Process
Decision
Document
Symbol Representation
Symbol Name
Data or Input/Output
Stored Data
Flow Arrow
Comment or Annotation
Predefined process
On-page connector/reference
Off-page connector/reference
Uses of Flowcharts in Computer Programming/Algorithms
The following are the uses of a flowchart:
It is a pictorial representation of an algorithm that increases the readability of the program.
Complex programs can be drawn in a simple way using a flowchart.
It helps team members get an insight into the process and use this knowledge to collect data,
detect problems, develop software, etc.
A flowchart is a basic step for designing a new process or adding extra features.
Communication with other people becomes easy by drawing flowcharts and sharing them.
When to Use Flowchart?
It is most importantly used when programmers make projects. As a flowchart is a basic step
to make the design of projects pictorially, it is preferred by many.
When the flowcharts of a process are drawn, the programmer understands the non-useful
parts of the process. So flowcharts are used to separate sound logic from the unwanted parts.
Since the rules and procedures of drawing a flowchart are universal, a flowchart serves as a
communication channel to the people who are working on the same project for better
understanding.
Optimizing a process becomes easier with flowcharts. The efficiency of the code is
improved with the flowchart drawing.
Advantages of Flowchart
It is the most efficient way of communicating the logic of the system.
It acts as a guide for a blueprint during the program design.
It also helps in the debugging process.
Using flowcharts we can easily analyze the programs.
flowcharts are good for documentation.
Disadvantages of Flowchart
Flowcharts are challenging to draw for large and complex programs.
It does not contain the proper amount of details.
Flowcharts are very difficult to reproduce.
Flowcharts are very difficult to modify.
PSEUDOCODE
Pseudocode is an informal way of programming description that does not require any strict
programming language syntax or underlying technology considerations.
It’s simply an implementation of an algorithm in the form of annotations and informative text
written in plain English. It has no syntax like any of the programming language and thus can’t
be compiled or interpreted by the computer.
Advantages of Pseudocode
Improves the readability of any approach. It’s one of the best approaches to start
implementation of an algorithm.
Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough
documentation, so the program of one developer can be understood easily when a pseudo
code is written out. In industries, the approach of documentation is essential. And that’s
where a pseudo-code proves vital.
The main goal of a pseudo code is to explain what exactly each line of a program should do,
hence making the code construction phase easier for the programmer.
How to write a Pseudo-code?
1. Arrange the sequence of tasks and write the pseudocode accordingly.
2. Start with the statement of a pseudo code which establishes the main goal or the aim.
Example:
This program will allow the user to check the number whether it's even or odd.
CODING
In this phase, developers start build the entire system by writing code using the chosen
programming language like C, C++, Java, PHP etc.
In the coding phase, tasks are divided into units or modules and assigned to the various
developers. It is the longest phase of the Software Development Life Cycle process.
In this phase, Developer needs to follow certain predefined coding guidelines. They also need
to use programming tools like compiler, interpreters, debugger to generate and implement the
code.
TESTING
Software testing can be stated as the process of verifying and validating whether a software or
application is bug-free, meets the technical requirements as guided by its design and
development, and meets the user requirements effectively and efficiently by handling all the
exceptional and boundary cases.
Software testing can be divided into two steps:
1. Verification: it refers to the set of tasks that ensure that the software correctly implements a
specific function.
2. Validation: it refers to a different set of tasks that ensure that the software that has been
built is traceable to customer requirements.
Verification: “Are we building the product right?”
Validation: “Are we building the right product?”
DOCUMENTATION
Software documentation refers to all the technical and written documentation related to a
software product that is:
Developed to assist and document the software development process, and
Created to help end-users make effective use of the software.
Types of Software Documentation
Software documentation is technical documentation that can be categorized into two main
categories:
Developer documentation: used to document software requirements, design,
architecture, and source code. It is created by dedicated technical writers or software
developers during the software development process. Developer documentation is used
by software developers, programmers, project managers, and other stakeholders
involved in the software engineering process. It serves as a reference for developers
who may later work on updates to the software. Developer documentation is also
known as system documentation.
User documentation: provides information about installing, configuring, or using
software. Software is a product, and software documentation is part of the product.
Comprehensive software documentation is one of the key factors that influence
businesses’ buying decisions.
COMMENTS
Comments in programming languages are used to document the programs. Comments are non-
executable and are used to explain the code written in the program.
Different programming languages have different syntaxes for writing the comments in the
program. Let’s have a look at a few of them.
Comments in C++, Javascript, JAVA, and PHP
There are two ways to comment in C+, Javascript, JAVA, and PHP:
Single-line comments //
Multi-line comments /*
In single-line commenting, the text starting with // to the end of the line is ignored by the
compiler and not executed.
In multiline commenting, the code between /* and */ is considered as comments and not
executed.
Comments in Python, Ruby, and R
There are two ways to comment in Python, Ruby, and R:
Single-line comments #
Multi-line comments
Single-line comments
Example:
print "Hello World"
# this is single line comments
Multi-line comments
Example:
print "Hello World"
"""
this is the
way to write
multiline comment in python
"""
ERRORS
Errors are the problems or the faults that occur in the program, which makes the behavior of the
program abnormal, and experienced developers can also make these faults.
These errors are detected either during the time of compilation or execution. Thus, the errors
must be removed from the program for the successful execution of the program.
Syntax error
Syntax errors are also known as the compilation errors as they occurred at the compilation time,
or we can say that the syntax errors are thrown by the compilers. These errors are mainly
occurred due to the mistakes while typing or do not follow the syntax of the specified
programming language.
o If we miss the parenthesis (}) while writing the code.
o Displaying the value of a variable without its declaration.
o If we miss the semicolon (;) at the end of the statement.
Run-time error
Sometimes the errors exist during the execution-time even after the successful compilation
known as run-time errors. When the program is running, and it is not able to perform the
operation is the main cause of the run-time error. The division by zero is the common example of
the run-time error. These errors are very difficult to find, as the compiler does not point to these
errors.
Linker error
Linker errors are mainly generated when the executable file of the program is not created. This
can be happened either due to the wrong function prototyping or usage of the wrong header file.
For example, the main.c file contains the sub() function whose declaration and definition is done
in some other file such as func.c.
Logical error
The logical error is an error that leads to an undesired output. These errors produce the incorrect
output, but they are error-free, known as logical errors. These types of mistakes are mainly done
by beginners. The occurrence of these errors mainly depends upon the logical thinking of the
developer. If the programmers sound logically good, then there will be fewer chances of these
errors.
Semantic error
Semantic errors are the errors that occurred when the statements are not understandable by the
compiler.
The following can be the cases for the semantic error:
o Use of a un-initialized variable.
int i;
i=i+2;
o Type compatibility
int b = "javatpoint";
MODULAR PROGRAMMING
Modular programming is the process of subdividing a computer program into separate sub-
programs. A module is a separate software component. It can often be used in a variety of
applications and functions with other components of the system.
Some programs might have thousands or millions of lines and to manage such programs it
becomes quite difficult as there might be too many of syntax errors or logical errors present
in the program, so to manage such type of programs concept
of modular programming approached.
Each sub-module contains something necessary to execute only one aspect of the desired
functionality.
Modular programming emphasis on breaking of large programs into small problems to
increase the maintainability, readability of the code and to make the program handy to make
any changes in future or to correct the errors.
Advantages of modular programming
o Code is easier to read - The use of modules should be done in a sensible manner so as
to avoid any problem. Functions should be neat, clean, and descriptive.
o Code is easier to test - In software, some functions perform fewer tasks and also
functions that perform numerous tasks. If the software is easily split using modules, it
becomes easier to test.
o Reusability - Instead of copying and pasting it, again and again, modularity gives us the
advantage of reusability so that we can pull our code from anywhere using interfaces or
libraries. The concept of reusability also reduces the size of our program.
o Faster fixes - Modularity can be a great help because we know that there will be a
separate function that will contain the code of payments, and only that function will only
be rectified. Thus using modules to find and fixing bugs becomes much more smooth and
maintainable.
o Low-risk update - In modular programming, a defined layer of APIs protects things that
use it from making changes inside the library. Unless there is a change in the API, there
is a low risk for someone's code-breaking.
o Easy collaboration - Different developers work on a single piece of code in the team.
There are chances of conflicts when there's a merge. This conflict can be reduced if the
code is split between more functions, files, repos, etc. We can also provide ownership to
specific code modules, where a team member can break them down into smaller tasks.
Disadvantages
o There is a need for extra time and budget for a product in modular programming.
o It is a challenging task to combine all the modules.
o Careful documentation is required so that other program modules are not affected.
o Some modules may partly repeat the task performed by other modules. Hence, Modular
programs need more memory space and extra time for execution.
o Integrating various modules into a single program may not be a task because different
people working on the design of different modules may not have the same style.
o It reduces the program's efficiency because testing and debugging are time-consuming,
where each function contains a thousand lines of code.