Computer Science & IT
PROGRAMMING CONCEPTS
Definition of Programming
• Programming is the process of designing and building executable computer software to
accomplish a specific task. It involves writing instructions that a computer can understand and
execute.
Types of Programming Paradigms
Procedural Programming (e.g., C, Pascal):
• Uses a sequence of procedures or routines to perform computations; follows a top-down
approach.
Functional Programming (e.g., Haskell, Lisp):
• Emphasizes pure functions, immutability, and function composition.
Scripting Languages (e.g., JavaScript, Python, Bash):
• Ideal for automation, web development, and rapid prototyping.
Compilation vs. Interpretation
Compilation:
• The entire source code is converted into machine code before execution (e.g., C, C++),
resulting in faster execution.
Interpretation:
• Code is executed line-by-line (e.g., Python, JavaScript), offering more flexibility but generally
slower performance.
Basic Concepts of Errors and Debugging
Common Errors:
• Syntax Errors: Mistakes in the structure of code (e.g., missing semicolons, unmatched
braces).
• Runtime Errors: Errors that occur during the execution of the program (e.g., division by
zero).
• Logic Errors: Errors in the program’s logic that produce incorrect output despite the program
running without crashing.
Debugging Techniques:
1 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
• Print Statements: Inserting print or output statements to check variable values at different
points in the code.
• Using a Debugger: Tools like gdb (for C/C++) that allow step-by-step execution, breakpoints,
and inspection of variables.
• Code Reviews and Testing: Reviewing code with peers and writing tests to catch errors early.
Example:
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
Explanation:
• This basic C program prints "Hello, World!" to the console. It includes the standard
input/output header and defines the main() function as the entry point.
❑❑❑
2 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
C AND C++
Structure of a C/C++ Program
• Programs consist of functions, statements, and comments.
• The main() function is the starting point
Example:
#include <iostream >
using namespace std;
int main()
{
cout << "Hello, C++!";
return 0;
}
Explanation:
• This simple C++ program outputs "Hello, C++!" to the console. It uses the iostream library
and the standard namespace.
Syntax and Semantics:
• Syntax: The rules that define how to write programs.
• Semantics: The meaning behind those rules.
Variables and Data Types:
• Variable Declaration: For example, int a; creates an integer variable named a.
• Data Types: Include int, float, char, double, and bool.
Input and Output Operations:
• C: Uses functions like printf() and scanf().
• C++: Uses stream objects such as cout and cin.
I/O Example:
int num;
cout << "Enter a number: ";
cin >> num;
cout << "You entered: " << num;
3 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Explanation:
• This basic C++ example shows how to prompt for input and display the entered number.
❑❑❑
4 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Arithmetic Operators
• Perform mathematical operations.
Example 1 – Basic Arithmetic:
int a = 10, b = 3;
cout << a + b;
Explanation:
• This example adds two numbers and prints the result (13).
Example 2 – Increment Operator:
int num = 5;
num++; // Increases num by 1
cout << num;
Explanation:
• The variable num is incremented from 5 to 6, and the new value is printed.
Relational Operators
• Compare values.
Example – Equality Check:
int x = 5, y = 10;
cout << (x == y);
Explanation:
• This example compares x and y. Since 5 is not equal to 10, it prints 0 (false).
Logical Operators
• Combine Boolean expressions.
Example – Logical AND:
bool a = true, b = false;
cout << (a && b);
Explanation:
• Since one operand is false, the logical AND returns 0 (false).
5 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Bitwise Operators
• Operate at the bit level.
Example – Bitwise AND:
int a = 6, b = 3; // 6 -> 110, 3 → 011
cout << (a & b);
Explanation:
• Bitwise AND of 110 and 011 gives 010 (binary), which is 2.
Operator Precedence
• The order in which operators are evaluated.
Example – Precedence:
int result = 2 + 3 * 4;
cout << result;
Explanation:
• Multiplication is performed first (3*4 = 12), then added to 2, resulting in 14.
❑❑❑
6 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
CONTROL STATEMENTS
Conditional Statements
• Execute code based on conditions.
Example 1 – if-else:
int score = 70;
if (score ≥ 50)
cout << "Pass";
else
cout << "Fail";
Explanation:
• This example checks if score is 50 or higher. Since 70 is greater than 50, it prints "Pass".
Example 2 – Nested if:
int num = 4;
if (num > 0)
if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";
Explanation:
• The program checks if num is positive and then whether it is even or odd. Here, 4 is even, so
"Even" is printed
Example 3 – Switch-case:
int day = 1;
switch(day)
{
case 1: cout << "Monday"; break;
default: cout << "Other day";
}
Explanation:
• A simple switch statement that prints "Monday" when day equals 1.
7 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Looping Constructs
• Repeat code execution until a condition is met.
Example 1 – For Loop:
for (int i = 1; i <= 3; i++)
{
cout << i << " ";
}
Explanation:
• This loop prints numbers 1 to 3.
Example 2 – While Loop:
int i = 1;
while (i <= 3)
{
cout << i << " ";
i++;
}
Explanation:
• This while loop does the same, printing numbers 1 to 3
Example 3 – Precedence:
int j = 1;
do
{
cout << j << " ";
j++;
}
while (j ≤ 3);
Explanation:
• The do-while loop prints numbers 1 to 3 and guarantees at least one execution.
Example 4 – Nested Loops:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << "* ";
}
cout << endl;
}
8 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Explanation:
• Nested loops are used here to print a 2×2 grid of asterisks.
Jump Statements
• Alter the flow of execution
Example 1 – Break and Continue:
for (int i = 1; i <= 5; i++)
{
if (i == 3) continue;
if (i > 4) break;
cout << i << " ";
}
Explanation:
• This loop skips the iteration when i is 3 and stops completely when i exceeds 4.
Example 2 – goto:
int count = 0;
start: cout << count << " ";
count++; if (count < 2)
goto start;
Explanation:
• The goto statement creates a simple loop that prints 0 and 1. (Use goto sparingly.)
❑❑❑
9 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
FUNCTION
Definition of Function
• A self-contained block of code that performs a specific task.
Types of Functions:
○ User-defined: Created by the programmer.
○ Built-in: Provided by standard libraries.
○ Recursive Functions: Functions that call themselves.
○ Inline Functions: Functions that are expanded at compile time.
Basic Function Example
int add(int a, int b) {
return a + b;
}
int main() {
cout << add(2, 3);
return 0;
}
Explanation:
• The add function returns the sum of two numbers. The main() function calls add(2, 3) and
prints 5.
Recursive Function Example
int factorial(int n) {
if(n == 0) return 1;
return n * factorial(n - 1);
}
Explanation:
• This recursive function calculates the factorial of n. For n equal to 0, it returns 1; otherwise, it
multiplies n by the factorial of n - 1.
❑❑❑
10 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Arrays
Definition
• An array is a collection of elements stored in contiguous memory locations.
Types of Arrays:
o One-dimensional Array: Example – int arr[5];
o Two-dimensional Array: Example – int matrix[3][3];
Basic Examples
One-Dimensional Array:
int arr[3] = {1, 2, 3};
for (int i = 0; i < 3; i++)
cout << arr[i] << " ";
Explanation:
• This one-dimensional array stores three integers. A loop prints each element.
Two-Dimensional Array:
int matrix[2][2] = {
{1, 2},
{3, 4}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
Explanation:
• This two-dimensional array (matrix) contains 2 rows and 2 columns. Nested loops print the
matrix in a grid format.
Additional Examples
Simple Array Initialization and Update:
int nums[3] = {0};
11 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
nums[0] = 2;
nums[1] = 4;
nums[2] = 6;
for (int i = 0; i < 3; i++)
cout << nums[i] << " ";
Explanation:
• An array is initialized with zeros, then each element is updated and printed.
❑❑❑
12 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Strings
Definition
• A sequence of characters stored in memory, used to represent text.
String Types:
o C-style Strings: Null-terminated character arrays (e.g., char str[] = "Hello";).
o C++ Strings: Use the std::string class for enhanced functionality.
Basic Operations and Examples
Concatenation:
string s1 = "Hello";
string s2 = " World";
string s3 = s1 + s2;
cout << s3;
Explanation:
• Two strings are joined using the + operator, resulting in "Hello World", which is printed.
Comparison:
string str1 = "a";
string str2 = "b";
cout << (str1 < str2);
Explanation:
• This compares two strings lexicographically. Since "a" comes before "b", the expression prints
1 (true).
Copying and Assignment:
string original = "Basic";
string copy = original;
cout << copy;
Explanation:
• A string is copied from one variable to another and printed.
Length Determination:
string text = "Hello";
cout << text.length();
13 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Explanation:
• The length() function returns the number of characters in "Hello" (5), which is printed.
Advanced Operations and Examples
Substring Extraction:
string phrase = "Hello World";
string sub = phrase.substr(0, 5);
cout << sub;
Explanation:
• substr() extracts the first 5 characters ("Hello") from the string and prints them.
Searching:
string sentence = "Hello World";
size_t pos = sentence.find("World");
cout << pos;
Explanation:
• The find() function locates the substring "World" in "Hello World" and prints its starting
index.
Modification:
string modify = "Hello World";
modify.replace(6, 5, "C++");
cout << modify;
Explanation:
• This replaces "World" with "C++" in the string, resulting in "Hello C++", which is printed.
Tokenization using stringstream:
#include <sstream>
string data = "a,b,c";
stringstream ss(data);
string token;
while(getline(ss, token, ',')) {
cout << token << " ";
}
Explanation:
• A stringstream splits a comma-separated string into tokens, which are printed one by one.
❑❑❑
14 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Pointers
Definition
• A pointer is a variable that stores the memory address of another variable.
Core Concepts
Declaration and Initialization:
int a = 10;
int *ptr = &a;
cout << *ptr;
Explanation:
• A pointer ptr is declared and initialized with the address of a. Dereferencing ptr prints the
value of a (10).
Pointer Arithmetic:
(Not demonstrated here, but pointers can be incremented or decremented to traverse arrays.)
Types of Pointers
• Null Pointers: Set to nullptr when not pointing to valid data.
• Void Pointers: Can hold the address of any data type.
• Function Pointers: Point to functions for dynamic calls.
• Pointers to Pointers: Used for multi-level data structures.
Memory Management:
• Dynamic Allocation: Using new/delete in C++ (or malloc/free in C).
• Pitfalls: Avoid memory leaks and dangling pointers.
Advanced Topics
• Smart Pointers: e.g., std::unique_ptr and std::shared_ptr for automatic memory management.
• Pointer Constancy: Distinguishing between constant pointers and pointers to constant data.
❑❑❑
15 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
File Structure
Definition
• File handling involves reading from and writing to files for data persistence.
File Types:
o Text Files: Human-readable files (e.g., .txt).
o Binary Files: Files storing data in binary form.
Basic Operations
Opening Files:
#include <fstream>
ofstream outFile("example.txt");
ifstream inFile("example.txt");
Explanation:
• This opens a file named "example.txt" for writing using ofstream and for reading using
ifstream.
Reading and Writing:
• Use stream operators (<< and >>) for text mode or read()/write() for binary mode.
Closing Files:
• Always call close() when finished with file streams.
File Modes and Error Checking:
• Use modes like ios::in and ios::out, and check status with methods like is_open().
Advanced Techniques
• Random Access: Using seekg() and seekp() to navigate within a file.
• Robust I/O: Leveraging full stream capabilities.
❑❑❑
16 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Data Structures
Definition
• Data structures are organized ways to store and manage data efficiently.
Core Data Structures
Arrays:
• Fixed-size collections with constant-time access.
Linked Lists:
• Dynamic structures for efficient insertions and deletions.
Stacks and Queues:
▪ Stack (LIFO): Last-In, First-Out management.
▪ Queue (FIFO): First-In, First-Out management.
Trees:
• Hierarchical structures for quick searches, insertions, and deletions.
Graphs:
• Made of nodes and edges to model relationships.
Implementation and Usage
Stack Example:
#include <stack>
stack<int> s;
s.push(1);
s.push(2);
cout << s.top();
Explanation:
• This example shows a basic stack where two integers are pushed. The top() function prints the
last pushed element (2).
Queue and Deque:
• Use STL containers like queue and deque for similar functionality.
Linked List Operations:
• Basic operations include insertion, deletion, and traversal.
17 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Tree Traversals:
• Inorder, preorder, and postorder traversals navigate tree structures.
Algorithmic Complexity:
• Consider time (e.g., O(1), O(n)) and space complexities when evaluating data structures.
Advanced Data Structures:
• Hash Tables: For fast key-value lookups.
• Heaps: For priority-based retrieval.
• Graph Algorithms: e.g., DFS and BFS for exploring graphs.
❑❑❑
18 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
Database Management
Systems (DBMS)
Definition of Data and Database
Data:
• Raw facts and figures that can be processed to generate information.
Database:
• An organized collection of data, typically stored and accessed electronically from a computer
system.
Definition of DBMS:
• A Database Management System (DBMS) is software that interacts with end users,
applications, and the database itself to capture and analyze data. It manages the storage,
retrieval, and updating of data in a systematic way.
Additional Basic Definitions
Information:
• Data that has been processed and is meaningful to the user.
Metadata:
• Data about data, such as information on the structure and properties of data stored in a
database.
Types of DBMS
Relational DBMS (RDBMS):
• Stores data in tables with rows and columns (e.g., MySQL, PostgreSQL).
NoSQL Databases:
• Non-relational databases designed for scalable, flexible data storage (e.g., MongoDB,
Cassandra).
Core Concepts in RDBMS
Tables, Rows, and Columns:
• The fundamental structures in relational databases.
SQL (Structured Query Language):
• The standard language for querying and managing relational data.
Key Operations:
19 Computer Programming RRB-JE SHORTNOTES
Computer Science & IT
▪ Data Definition Language (DDL): Commands such as CREATE, ALTER, and DROP for
defining database structures.
▪ Data Manipulation Language (DML): Commands like INSERT, UPDATE, and DELETE
for handling data.
▪ Data Querying: Retrieving data using SELECT statements.
Transactions:
• Ensure ACID properties (Atomicity, Consistency, Isolation, Durability) for reliable
operations.
Additional Features and Concepts
• Indexing: Enhances query performance by creating indexes on key columns.
• Normalization: Organizes data to reduce redundancy and improve integrity.
• Stored Procedures and Triggers: Automate common operations and enforce business rules.
• Backup and Recovery: Strategies to protect data against loss or corruption.
• Security Measures: Use authentication, access controls, and encryption.
• Connection and Integration: Use APIs and connectors (e.g., ODBC, JDBC) to link
applications with the database.
Example SQL Query:
SELECT id, name
FROM students
WHERE age > 18;
Explanation:
• This SQL query retrieves the id and name columns from the students table for records where
the age is greater than 18.
❑❑❑
20 Computer Programming RRB-JE SHORTNOTES