Chapter 5: Programming Concepts and Logic - CS Study Hub

Latest

Sunday, December 7, 2025

Chapter 5: Programming Concepts and Logic

Programs written in programming languages are not directly understood by  computers. Therefore, language translators or processors like assemblers, compilers, and interpreters are used to convert these programs into machine-level language.

Qualities of Good Programs:

  1. Understandability: Programs should be easily understood.
  2. Correctness: Programs should be error-free.
  3. Reliability: Programs should be dependable.
  4. User Interface: Programs should have an easily understandable user interface.
  5. Portability and Flexibility: Programs should be portable and flexible.


Programming Language:

In order to make communication between user and computer, we need a computer language that helps user to generate commands to perform as per the requirement. This language with which we can give instruction to the computer is known as programming language. Programming language are the set of different keywords, variables, operators, loops, and other entities using different character sets including numbers, special symbol and alphanumeric values.

Hence, the process of writing the programming language is known as programming and the person who writes program is programmers.

A programming language is a formal, structured language used to write instructions that a computer can understand and execute. It provides a set of rules, syntax, and keywords that allow humans to communicate with computers to build software, websites, applications, and control hardware. 


Types of Programming Language

There are several types of programming language which can be categorized as follows:

Low level language: Low level language are the machine dependent language which means program written for one type of system cannot be run in another system. Developer should have detail analysis and knowledge about the system for which s/he is going to write a program for. Hence, programming in low level language is very much difficult and time consuming.  Different types of programming language are:

a. Machine level language(1GL): This language consists a sequence of 0’s and 1’s to generate instruction. Since, it uses binary numbers, this type of language is directly understood by processor. So, it has higher execution speed. It is also a machine dependent language in which programmers should have detailed knowledge of the system.

Advantage:

  • It is directly understood by the processor so execution speed is relatively high.
  • Language translator or processor is not needed.
  • They can be used to program specific purpose computer.

Disadvantage:

  • It is difficult and time consuming to develop and debug program.
  • It is machine dependent language. So, program developed for one system cannot be operated in another system.
  • All the syntax and commands are in the form of binary numbers which is difficult to remember.
  • Programmers should have detail knowledge about particular system and its architecture.


b. Assembly language(2GL): Assembly language are also an example of low-level language. In this language, instead of writing instruction in the series of 0’s and 1’s we can use mnemonics (symbolic instructions) like ADD, SUB, RST, DIV, MOD and so on. Since it is closer to machine level language, a programmer should have detailed knowledge about computer internal architecture. This language is faster in comparison to high level language. Since, this language is not directly understood by the computer, we need language translator like assembler to convert it into machine level language.

Advantage: 

  • It is easier to write, debug and understand programming written in assembly level language compared to machine level language.
  • Program execution is higher compared to high level language,
  • Since they are machine dependent, they are used to develop different device drivers.

Disadvantage:

  • It is machine dependent language, i.e. program made for one processor doesn’t run in another processor.
  • Use of mnemonics code makes assembly language much more complex.
  • Program development and debugging is more difficult and time consuming compared to high level language.


2. High level language: This language is close to English language. High level language code is written in English like structure using mathematical notation. Since it is similar to English language, it is easier to develop and debug the program. It is machine independent language (i.e. program developed for one processor can work on another processor). Since HLL (high level language) are not directly understood by the computers, we need language processor or translator such as complier and interpreter for converting program written in high level language to machine level language. FORTAN (Formula Translator) introduced on 1956 A.D is the first high level language. Nowadays there are many high-level languages like C, C++, python, JavaScript, etc.

Advantage:

  • Since it is closer to English language, program written in this language is easier to write, debug and understand.
  • Since it is machine independent, program written for one processor can work in another processor.
  • Programmer doesn’t have to remember large number of mnemonics and other unusual codes.
  • Program development is faster and requires less effort than other language.

Disadvantage: 

  • Computer doesn’t understand high level language directly. So the program needs conversion before execution.
  • Program execution is slower compared to low level language.

High level language can further be classified into:

a. Procedural oriented Language(3GL): This type of language is high level language which primarily focuses on procedure rather than on data. Hence, they are used to express the logic and the procedure of the program. Since it focuses only on procedure, it is complex and time consuming to write a large program. This type of language follows top to bottom approach i.e. main function is written at the bottom of the program. This type of language doesn’t pose important and powerful feature like data encapsulation, data inheritance, data extraction and so on. So, this type of language has less security compared to object-oriented language. Because of their flexibility, procedure language is able to solve verities of problem. Examples: C, FORTAN, QBasic, etc.

Advantage:

  • Program development and debugging is easier compared to low level language.
  • More advance and user-friendly software can be developed.
  • It is also a machine independent language.
  • It is used as general purpose programming.

Disadvantage:

  • Language translator or processor is required to execute the program.
  • Program execution is slower.
  • Data security is less in comparison to other high-level language.

b. Problem/object Oriented Language(4GL): This is the advance form of high-level language which primarily focuses on data rather than procedure. It allows the user to specify what the output should be without describing all the detail (i.e. procedure) of how the data should be manipulated. This type of language follows bottom up approach. That means all main function are written at the bottom of the program whereas classes and object are described at top of the program. Since it has several powerful features such as data encapsulation, data extraction and inheritance, the data are more secure compared to procedure language. Examples: C#, C++, Java, etc.

Advantage:

  • Web based application and software can be developed.
  • More advance and user-friendly software can be developed.
  • It is also machine independent language.

Disadvantage:

  1. Language translator is required to execute the program.
  2. Program execution is slower.
  3. It is difficult to develop hardware-oriented language.

c. Natural Language (5GL): Natural language uses simple statement of common communication language where we could write statements that would look like normal sentences. It is still in developing stage; computer scientists are working hard for developing such language. However, programming language like PROLOG (programming logic) is currently in use. For example: - instead of writing some unusual code, programmers would write: “who are the salesmen who has sold more than 30,000 products last month?”

Advantage: 

  • It will be even easier to develop and debug the program.
  • It will also be machine independent language.
  • More advance and user-friendly program will be made.

Disadvantage: 

  • Language translator is required to execute the program.
  • Program execution is slower
  • It is difficult to develop hardware-oriented language.


Language translator/processor:

Language translator is that system development software which helps to convert program written in assembly or high-level language (source program) into machine level language (object program). Since it is difficult and incontinent to write a program in machine level language, language developer uses several assemblies and high level language which are not directly understood by the computer. Hence, we use different types of language processor to convert and make machine understandable. There are different types of language translators.

Assembler: These language translator/processor converts program written in assembly level language (source program) into machine understandable language (object language) Since assembly level language are closer to machine level language, the conversion taken by assembler is relatively less. It converts program at once into machine level language.

Compiler: Those language processors which helps to convert program written in high level language (source program) into machine level program (object program). It converts whole program into machine level language at once. It is the largest method of translating a program in which debugging is complex and time consuming. Programming languages like C, C++, Java, etc. use complier

Interpreter: This is the type of language which converts program written in high level language (source program) into machine level language (object program). It converts one statement at a time so its debugging can be easier and less time consuming. Its program execution is slower than that of compiler. Most of the new programming language use interpreter which allocate less memory space. Programs like BASIC, C#, Php, etc. use interpreter for conversion.

Difference between compiler and interpreter:

S.N. Compiler Interpreter
1 It translates the whole program into object code at a time. It translates one line or a single statement of a program into object code at a time.
2 It finds out the error after compiling the complete program. It finds out the error after translating a line of the program at a time.
3 The translating process is incredibly faster. The translating process is slower.
4 Compiler based program is difficult to code and debug. Interpreter based program is easy to code and debug.
5 It is usually a large program and requires larger memory space. It is usually a smaller program and requires less memory space.
6 New programming language don’t use compiler. New programming languages use interpreter.
7 For e.g.: C, C++, Java, FORTRAN etc. For e.g.: BASIC, C#, PHP etc.


Syntax and Semantics:

When we write a program, the computer checks two things:

  • Is the sentence written correctly? → Syntax
  • Does the sentence make sense? → Semantics


1. Syntax (Grammar of a Programming Language)

Syntax refers to the rules, structure, and grammar of a programming language.

If you break a syntax rule, the compiler shows a syntax error.


Simple Explanation:

Just like English grammar decides how to form a correct sentence, programming syntax decides how to write correct code.


C Example (Correct Syntax):

int x = 10;

printf("%d", x);


C Example (Syntax Error):

int x = 10      // Missing semicolon → syntax error

printf("%d" x); // Missing comma → syntax error


2. Semantics (Meaning of a Program)

Semantics refers to the meaning of a statement — what the program does when it runs.

Even if your syntax is correct, your program can still be semantically wrong.


Simple Explanation:

The sentence is written correctly (no grammar mistake) but doesn’t make sense logically.


C Example (Correct Syntax but Wrong Meaning):

int x = 10;

int y = 0;

int z = x / y;   // Syntax is correct, BUT meaning is wrong → runtime error (divide by zero)


Another Example:

int a = 5;

int b = 3;

int c = a + b;   // Meaning makes sense → correct semantics


But:

int a = 5;

int b = 3;

int c = a - b;   // Compiler accepts it, but if your intention was addition, semantics is wrong


Concept Meaning Example

  • Syntax => Rules for writing correct code (grammar) Missing semicolon → syntax error
  • Semantics => Meaning of the code (logic) Dividing by zero → semantic error


Easy Real-Life Analogy

Syntax:

Like writing:

"He is going to school." ✔

"Going he school is to." ✖ → wrong grammar

Semantics:

Correct grammar but meaningless:

"The car is eating grass." → grammatically correct but meaningless


Error (BUG):

There may be several errors inside a program which stops program from being executed. Thus, an error in a program is known as a bug. This type of error in a program may arise by not following proper rules given by the programming language due to poor problem analysis or maybe due to hardware failure. Error in the program may five ambiguous (not accepted/false) result. The process of removing error from a computer program is known as debugging. There are three types of error:


Syntax error: the error which may arise due to not following proper rules or format given by the programming language. Every programming language have their own vocabulary, punctuation and structure. Programmers need to follow every detail while programming. This type of error can be detected by language processor such as compilers and interpreter. For e.g. In C, every statement should end with semi-colon at the end of the statement then the errors encounter is syntax error.

Logical error: Logic refers to idea or concept used to solve a specific problem. If the problem is poorly understood than it may hamper while solving the problem. The error that is encountered due to poorly developed logic is called logical error. This type of error is not detected by language processor. For example: In order to calculate simple interest, we use the formula I =(P*T*R)/100, if we write above mentioned formula, as I=P*T*R, then the error encounter is logical error.

Runtime error: When a program is running or executing, it is said to be runtime. Hence, the error that are encounters/found while program is being executed is known as runtime error. It is also not detected by language translator. For example: if insufficient memory space, peripheral device not turning on.


Some common terminology in programming:

Data types: It specifies the type of data to be used in a program. Some common types of data types are:

Numeric: It is the combination of all the positive and negative whole number including zero.

Character: it is the possible combination of all the alphanumeric values including especial symbol.


Variable and Constant: 

Variable: Those entities which holds either numeric or alphanumeric values and changes its value throughout the time of program execution.

Constant: Those entities which holds either numeric or alphanumeric values but does not change its value throughout the time of program execution.


Operands and Operators and Operations:

Let us consider as;

S=A+B

Where S, A, B are operands

+, = are operands

S=A+B denotes sum of two operands A, B which is Operation


Types of operators:

It is a special symbol which helps to make operation successful i.e. it helps to make some specific operation.

Commonly used operators are:

Arithmetic operators: +, -, /, *

Relative operators: <, >, >=, <=, =

Logical operators: AND, OR, NOT


Program Design Tool

There are several program designs tools that are available. Some of the commonly used program design tool are algorithm, flowchart, pseudocode.


Algorithm:

The step by step procedure or sequential steps for solving a specific problem is known as algorithm. Algorithm are written in simple English language and are not even close to any programming language.


Rules for writing algorithm: 

Algorithm should execute output after a finite step.

Algorithm should not depend upon any programming language.

Algorithm should terminate after finite steps.


Examples:

1. Write an algorithm to calculate simple interest.

  • Step 1: Start
  • Step 2: Read the value of P.T.R
  • Step 3: Calculate simple interest by using i=(P*T*R)/100
  • Step 4: Display simple interest(i)
  • Step 5: Stop


2. Write an algorithm to check greatest number among two number.

Step 1: Start

Step 2: Read any two-number a, b

Step 3: check if (a>b)

              If yes, Display the greatest number is “a” and go to step 4

              If no, Display the greatest number is “b” and go to step 4

Step 4: Stop


Flowchart: 

It is a diagrammatic or pictorial representation of steps involve in algorithm for solving a particular problem is known as flowchart. It uses several geometrical shapes, symbol for representing different action involve in problem solving. There are two types of flowchart:


a. Program Flowchart: It is a type of flowchart which focuses on a specific problem. This type of flowchart portage only a single task (logic) at a time.

Symbols used in flowchart:



Examples:

1. Draw flowchart to calculate simple interest.


2. Draw flowchart to find greatest number among two numbers.


b. System Flowchart: It is also a type of flowchart that shows the logical flow of a program and its collection. It shows the overview of the data flow and sequence of operation in a system.


Symbols used in system flowchart:


Example of System Flowchart


What is c programming language?

It is a High Level Programming Language, developed by Dennis Ritchie at AT & T’s Bell Laboratories of USA, in 1970. It is a very powerful programming language. It is used for both system programming and application programming. So, often known as Middle Level Programming Language, also. It means, it supports or works as the low level language and high level language, both. 

What are the features of c language?

The features of c language are:

  • It supports high level and level programming both, so known as mid level programming language.
  • It is mostly used for System programming but very popular for Application programming, too.
  • The program development is faster and more efficient.
  • It has very powerful tools and keywords.
  • It uses its own text editor.
  • It has its own compiler.
  • It supports structured programming.
  • It is portable easily.
  • It has very powerful library functions set.
  • Flexible in programming.
  • What are the advantages and disadvantages of c language?

The advantages are:

  • Suitable for system programming.
  • It is easier to interact with hardware.
  • Easy to learn.
  • Program code is secure.
  • It is compact and efficient.
The disadvantages are:
  • It does not contain runtime checking.
  • There is no strict data type checking.
  • As the program extends, the debugging becomes more complex because it uses compiler instead of interpreter for the compilation.
  • Semi-colon requires for each statement.
 Define the following terms:

a) Editor: This is the area, in which we type the program. C language has its own text editor but we can use any other text editor program also.

b) Pre-processor: This is the collection of complex codes to instruct the machine to understand the certain keywords. It is written with preceding # symbol. For example: #include, in this example, include pre-processor with the hash symbol, uses the instruction or complex codes from the file ‘stdio.h’, which is already stored by c language. These .h files are called header files. So, include pre-processor is used for using the header files.

c) Compiler: It is a type of software to change the source code of high level programming language into machine readable form called object code.

d) Linker: It is also a type of software, which links the various library files with the object codes and creates an executable file with .exe extension. Then, exe file can run independently.


FUNDAMENTAL OF C

Define the character set. Also define the character set used in c language.

The set of alphabets, digits, white characters and other symbols used in any programming language is called a character set. The character set used in c are:

Alphabet – A, B, C, ……..Z and a, b, c, ….z

Digits – 0, 1, 2, …………9

Special Symbols - ~ ‘ ! @ % ^ & * ( ) - _ + = | \ { }  []  : ; “ ‘ <> , . ? /

Define comment: Comment is a text or set of other characters, which is not compiled or executed from the compiler. It is written in the program to give required information to other programmers and the developers themselves. There are two types of comment writing methods:

a. Single line comment: It is written with the // symbol.

Eg. // This is my first program.

b. Multi-line comment: It is written with /*………….*/ symbols.

Eg. /* This is

              multi line

           comment style */


Define C Tokens.
The set of identifiers, keywords, constants, variables and operators is known as C Tokens. It remains as a single unit and compiler does not break in the time of compilation. 

Define the following terms:

a) Identifiers: Identifiers are names for entities in a C program, such as variables, arrays, functions, structures, unions and labels.

b) Keyword: Keyword is a reserved word that is already stored by the c language developer. The reserved word cannot be used as the identifiers. There 32 Keywords in C according to the ANSI (American National Standard Institute) in c language. They are: void, printf, scanf, if, else, while, getch, do, for, break, continue etc 

c) Data types in c: The term Data Types in any language refers to the types of data which can be used in the program execution for entering and manipulating as the values. In the same way, the data types in c also refer to the data types, which can be used as the values. Basically it is of four types. They are:

int : It represents the integer type data. It means the numbers without the decimal points. It consumes 2 bytes memory. The derived data types from int are short int and long int.
float : It means the real numbers with the decimal points. It consumes 4 Bytes memory space. It can also be signed and unsigned. Derived data types of it are double and long double.
char : It is used for a single character (alphabet or symbol) like ‘A’, ‘f’, ‘@’ etc. It requires 1 byte memory. It can also be signed  or unsigned.
void : void means no value. It means null value. It is used for returning null value to the modules.

d) Constants

e) Variables

f) Statements : The set of keywords, identifiers, operators, variables and constants ended with the semicolon or a curly bracket is called statement. It is of two types:Simple 

Statement : The statement written in a single line and ended with the semicolon is simple statement.  Eg. printf (“My program”);
Compound Statement:  The group of simple statements grouped by the curly bracket open and close is the compound statement. Eg.
if (n>0)
{
   a=b+c;
   x=x*x;
   printf(“%d %d”, a,x);
}

g) Escape Sequences: The symbol \ or ‘ is supposed as the Escape character. It means, the compiler of c programming language understands this character and the text after this to take as a special case. And the set of this symbol and the character behind it is known as the Escape Sequence. Some common escape sequences are:

Character         Escape
New Line         \n
Horizontal tab         \t
Null character         \0
Quotation mark \”
Apostrophe         \’
Backslash (\)         \\

h) Operators: (imp)The symbols, which are used to operate on the operands for the mathematical and logical calculations are known as operators. C programming language has a rich set of built-in operators. Operator that requires two operands are binary (dyadic) operators, operators that require one operand is unary (monadic) and the operator that requires three operands is ternary operator. Eg. a+b, here + is an operator.

Types of operators:
  • Arithmetic Operator
  • Relational Operator
  • Equality Operator
  • Logical Operator
  • Assignment Operator
  • Increment/Decrement Operator
  • Conditional Operator (Ternary Operator)
  • Bitwise Operator
  • Special Operator
i) Library Functions: Functions are the independent sections or the block of statements given a specific name and they can be used by any program according to the requirements. They are developed to perform a certain task. They are of two types:

a) Library Functions: These are the ready-made formula-like functions stored in c language library. They can be used in any program. abs(), printf (), scanf (), clrscr(), getch(), strlen(), sqrt(), etc. are some examples of Library functions.

b) User defined function: The functions developed by the users in their own program are called User-defined functions.

INPUT/OUTPUT (I/O) functions

1. printf() : It is a formatted output function.

Eg. printf(“My School”);

2. scanf() : It is to enter any value to the program.

Eg. scanf(“%d”, &x);


Program Example

a) Write a c program to enter any two numbers and print the sum of them.
# include  
void main()
{
 int a, b, s;
 clrscr();
 printf(“Enter First Number”);
 scanf(“%d”, &a);
 printf(“Enter Second Number”);
 scanf(“%d”, &b);
 s = a + b;
 printf (“The sum is %d”, s);
 getch();
}

b) Write a c program to calculate the area of a rectangle, where the length is 20 and breadth is 15.
# include   
void main( )
{
 int l = 20, b = 15;
 int a;
 clrscr();
 a = l * b;
 printf (“The area is %d”, a);
 getch();
}

c) WAP to calculate the area of a rectangle in c. [Hints: a = l x b]
# include  
void main()
{
 int l, b, a;
 printf (“Enter Length: ”);
 scanf (“%d”, &l);
 printf (“Enter Breadth: “);
 scanf (“%d”, &b);
 a = l * b;
 printf (“The area is %d”, a);
 getch();
}

d) Write a program to enter length and breadth of a rectangle and calculate the perimeter. [p = 2 (l + b) ]
# include  
void main ()
{
 int l, b, p;
 printf (“Enter Length :”);
 scanf (“%d”, &l);
 printf (“Enter Breadth :”);
 scanf (“%d”, &b);
 p = 2 * (l + b);
 printf (“The perimeter is : %d”, p);
 getch();
}

e) WAP to calculate the area of a triangle .
# include  
void main()
{
 int b, h, a;
 printf (“Enter base and height”);
 scanf (“%d %d”, &b, &h);
 a = ½ * b * h;
 printf (“The area is %d”, a);
 getch();
}
 
f) WAP to enter the length of any one side of a square and calculate the area and perimeter in c language.
# include   
void main ()
{
 int l, a, p;
 clrscr ();
 printf (“Enter length”);
 scanf (“%d”, &l);
 a = l * l;
 p = 4 * l;
 printf (“The area is %d and the Perimeter is %d”, a, p);
 getch();
}

No comments:

Post a Comment