Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Programming Concepts

&
Logic
Introduction to programming languages
 As we know, to communicate with a person, we need a specific language, similarly
to communicate with computers, programmers also need a language is called
Programming language.
 Language is a mode of communication that is used to share ideas, opinions with
each other.
 A programming language is a computer language that is used by programmers
(developers) to communicate with computers. It is a set of instructions written in
any specific language ( C, C++, Java, Python) to perform a specific task.
Continue..
 A computer is a sequence of instruction written using a computer
programming language to perform a specified task by the computer.
Characteristics of Good Program
 Portability:- Portability refers to an application to run on a different platform,
which or without minimum changes. If the program is developed for a particular
platform then the like Span of the program is severely affected.

 Readability:-The programmer should be written in with a more user-friendly


approach or in such a way that it makes other programmer users follow the logic
of the program without much effort.

 Efficiency:-Live program at required certain processing time and memory to


process the instruction and data a program should be let out in such a manner,
that it utilized the least amount of memory and processing time.
Characteristics of Good Program
 Structural:- If a program is developed structurally the program not only
becomes more readable but the testing and the documentation process also
get easier.

 Flexibility:- A program is flexible enough to handle most of the changes


without having the rewrite the entire program, most of the program is
dependent on a certain period and their required modification from time to
time.

 Generality:- Apart from the flexibility of the program should also be general.
By Generality, we mean that, if a program is developed for a particular task,
then it should also be used for all similar tasks of the same domain.
Types of programming language
 Machine Level language
 Assembly Language
 High Level Language
Machine Level language
 Machine language is the lowest level of programming language that directly corresponds
to the instructions executed by a computer's hardware.
 Low-Level language is the only language which can be understood by the computer. Low-
level language is also known as Machine Language.

 The machine language contains only two symbols 1 & 0. All the instructions of machine
language are written in the form of binary numbers 1's & 0's. A computer can directly
understand the machine language.

High-Level Machine
Language Language

101011010
( A + B ) /2 Translator 00101010
111000110
Source Code Object Code
Assembly Language
 Middle-level language is a computer language in which the instructions are created using
symbols such as letters, digits and special characters. Assembly language is an example of
middle-level language. In assembly language, we use predefined words called mnemonics.

 Binary code instructions in low-level language are replaced with mnemonics and operands
in middle-level language. But the computer cannot understand mnemonics, so we use a
translator called Assembler to translate mnemonics into machine language.

 Assembler is used to translate middle-level language into low-level language

High-Level Machine
Language Language

101011010
( A + B ) /2 Translator
00101010
111000110
Source Code Object Code
High Level Language
High-level language is a computer language which can be understood by the users. The high-
level language is very similar to human languages and has a set of grammar rules that are
used to make instructions more easily.
Every high-level language has a set of predefined words known as Keywords and a set of
rules known as Syntax to create instructions.

High-level language needs to be converted into the low-level language to make it


understandable by the computer. We use Compiler or interpreter to convert high-level
language to low-level language.

High-Level Machine
Language Language

101011010
( A + B ) /2 Translator 00101010
111000110
Source Code Object Code
Types of High level Language
 Procedural oriented language (3GL)
 Problem oriented language (4GL)
 Natural Language (5GL) / Artificial Intelligence Languages
Types of Translator
 A language processor is a software component or a set of tools that performs
various tasks related to the processing of human languages. There are different
types of language processors, each serving a specific purpose. Here are a few key
types:

 Compiler: A compiler is a type of language processor that translates high-level


programming code written in a source language (like C or Java) into machine code
or an intermediate code. The resulting code can be executed directly by a
computer's hardware.
Types of Translator Continue..
 Interpreter: An interpreter is another type of language processor that also
translates high-level programming code, but it does so line by line during the
execution of the program. Interpreters do not produce a standalone
executable file; instead, they execute the source code directly.

 Assembler: An assembler is a language processor that converts assembly


language code into machine code. Assembly language is a low-level
programming language that is specific to a particular computer architecture.
Error
 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.
 Programming errors are also known as the bugs or faults, and the process of removing
these bugs is known as debugging.
Types of Error
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.
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:
 Use of a un-initialized variable.
int i;
i=i+2;
 Type compatibility
int b = "javatpoint";

 Errors in expressions
int a, b, c;
a+b = c;
 Array index out of bound
int a[10];
a[10] = 34;

Note: In the above code, we use the statement a+b =c, which is incorrect as we cannot use the
two operands on the left-side.
Control Structures
 Control structures in C programming are constructs that enable you to control the flow
of execution within your program. These structures allow you to make decisions,
repeat actions, and execute code based on certain conditions. The three main types of
control structures in C are:
Selection Structures (Conditional Statements)
 if statement: Executes a block of code if a specified condition is true.
 if-else statement: Executes one block of code if a specified condition is true and another
block of code if the condition is false.
 nested if-else statement: Allows you to have multiple levels of conditional statements.
 switch statement: Evaluates an expression and executes code based on different possible
values of the expression.
Iteration Structures (Loops)
 while loop: Executes a block of code repeatedly as long as a specified condition is true.
 do-while loop: Executes a block of code once, and then repeatedly executes the block
as long as a specified condition is true.
 for loop: Executes a block of code repeatedly for a fixed number of times.
Select /Jump Statements
 break statement: Terminates the current loop or switch statement and transfers
control to the statement immediately following the terminated statement.
 continue statement: Skips the current iteration of a loop and continues with the next
iteration.
 Go to statement: Transfers control to a labeled statement within the same function.
Program Design Tools
Algorithm Flowchart Pseudo Code
Algorithm
 An algorithm is a step-by-step procedure or set of rules that must be followed to
perform a specific task or solve a particular problem.
 In the context of computer science and mathematics, an algorithm is a well-defined,
finite sequence of instructions or computational steps that transforms input data into
output.

Finding area of Rectangle


Key characteristics of algorithms
 Well-Defined: Algorithms are precisely and unambiguously defined, with clear and specific
instructions for each step.
 Finite: Algorithms must be composed of a finite number of steps. They should eventually
terminate after a finite number of instructions.
 Inputs and Outputs: Algorithms take inputs and produce outputs. The inputs are
transformed through a series of steps to produce the desired output.
 Deterministic: Each step of an algorithm must be deterministic, meaning that the same
input will always produce the same output.
 Correctness: An algorithm is considered correct if it produces the correct output for all valid
inputs.
 Efficiency: Algorithms are often evaluated based on their efficiency in terms of time and
space complexity. Efficient algorithms perform the task with optimal use of resources.
Pseudo code
 Pseudo code is a high-level, informal notation used to represent algorithms in a
structured and human-readable form. It is not a programming language, but rather a
way to express the logic and steps of an algorithm in plain language that is easy for
humans to understand.
 Pseudo code is a valuable tool for planning, designing, and communicating algorithms
before implementing them in code.
Flowchart
 Flowcharts are nothing but the graphical representation of the data or the algorithm for a
better understanding of the code visually. It displays step-by-step solutions to a problem,
algorithm, or process.
 It is a pictorial way of representing steps that are preferred by most beginner-level
programmers to understand algorithms of computer science,

 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 Notation
Computer Codes
Absolute Binary BCD ASCII

EBCDIC Unicode
Absolute Binary
 "Absolute binary" is not a standard term in computing. However, it's possible that you're
referring to binary representation without any encoding scheme or modification, where each
binary digit (bit) represents the exact value of a number in base-2.

 Absolute binary is the simplest form of binary representation, where each bit directly
corresponds to the value of a power of 2 without any encoding, padding, or modification.
Binary-Coded Decimal(BCD)
 BCD, or Binary-Coded Decimal, is a way of representing decimal numbers using binary
notation. In BCD, each decimal digit is represented by a 4-bit binary code. This allows for
efficient storage and manipulation of decimal numbers in digital systems.
 In standard binary representation, each decimal digit (0-9) is represented using 4 binary
bits (0000 to 1001).
 In BCD, each decimal digit is also represented using 4 bits, but the range is limited to 0000
to 1001 to only represent the decimal digits (0-9).
 For example, the decimal number 123 in BCD would be represented as follows:
 1 is represented as 0001
 2 is represented as 0010
ASCII
 ASCII, which stands for American Standard Code for Information Interchange, is a character
encoding standard that assigns numeric codes to represent characters in computers.
 It was developed in the early days of computing to provide a standardized way for computers
to represent and exchange text data.

 For example, here are some ASCII codes and their corresponding characters:
 ASCII code 65 represents the uppercase letter 'A'.
 ASCII code 97 represents the lowercase letter 'a'.
 ASCII code 32 represents the space character.
EBCDIC
 EBCDIC, which stands for Extended Binary Coded Decimal
Interchange Code, is another character encoding standard used
in computing, particularly in IBM mainframe systems.
 EBCDIC was designed to support punched cards, which were
commonly used for data entry and storage in early computing
systems
 Despite its declining use, EBCDIC is still used in some legacy
systems, where mainframe systems are still in use and
compatibility with older data formats is required.
 Each character is represented by an 8-bit binary number (0 or 1),
providing a total of 256 possible characters.
Unicode
 Unicode is a character encoding standard that aims to represent every character from every
language and symbol system in the world.

 It is designed to be a universal character encoding standard that supports the needs of


multilingual text processing and communication.

 Unicode maintains backward compatibility with older character encoding standards like
ASCII.

 Unicode includes characters from virtually all writing systems and symbol sets used
throughout the world, making it a comprehensive standard for text representation in
computing.
Homework
 What is loop? List its types and explain each.
 What is an algorithm? Explain need of an algorithm.
 Write an algorithm to find average age of a group of 10 players.
 Explain use of flowchart.
 Draw a flowchart to find the sum of first 100 natural numbers.
 Draw a flowchart to know a number is even or odd.
Thank you !!!

You might also like