01

Programming Fundamentals

Chapter 1 • Beginner

40 min

Programming Fundamentals

Programming fundamentals form the foundation of computer science. Understanding these concepts is essential for GATE CS.

Programming Concepts

Variables and Data Types

Variables are named storage locations that hold values.

Common Data Types:

  • Integer (int): Whole numbers (e.g., 5, -10, 0)
  • Float/Double: Decimal numbers (e.g., 3.14, -0.5)
  • Character (char): Single character (e.g., 'a', 'Z')
  • String: Sequence of characters (e.g., "Hello")
  • Boolean: True or false values

Control Structures

1. Conditional Statements

If-Else:

c.js
if (condition) {
    // code if true
} else {
    // code if false
}

Switch-Case:

c.js
switch (variable) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

2. Loops

For Loop:

c.js
for (initialization; condition; increment) {
    // code
}

While Loop:

c.js
while (condition) {
    // code
}

Do-While Loop:

c.js
do {
    // code
} while (condition);

Functions

Function is a block of code that performs a specific task.

Function Declaration:

c.js
return_type function_name(parameters) {
    // function body
    return value;
}

Example:

c.js
int add(int a, int b) {
    return a + b;
}

Recursion

Recursion is when a function calls itself.

Base Case: Condition that stops recursion.

Recursive Case: Function calls itself with modified parameters.

Example - Factorial:

c.js
int factorial(int n) {
    if (n == 0 || n == 1)  // Base case
        return 1;
    else                    // Recursive case
        return n * factorial(n - 1);
}

Recursion vs Iteration:

  • Recursion: Elegant, but uses stack space
  • Iteration: More efficient in terms of space

Parameter Passing

1. Pass by Value

  • Copy of value is passed
  • Changes don't affect original

Example:

c.js
void increment(int x) {
    x++;  // Doesn't affect original
}

2. Pass by Reference

  • Address is passed
  • Changes affect original

Example:

c.js
void increment(int *x) {
    (*x)++;  // Affects original
}

Scope and Storage Classes

Scope: Region where variable is accessible.

Types:

  • Local Scope: Within function/block
  • Global Scope: Throughout program

Storage Classes:

  • auto: Default for local variables
  • static: Retains value between calls
  • extern: External linkage
  • register: Suggests CPU register storage

GATE CS Weightage

Programming Fundamentals typically accounts for:

  • 2-3 marks out of 100 in GATE CS
  • Foundation for data structures
  • Frequently tested in combination with DSA

Important Topics for GATE CS

  1. Recursion (High Priority)
  • Recursive functions
  • Base cases
  • Stack usage
  1. Parameter Passing (Medium Priority)
  • Pass by value vs reference
  • Pointer operations
  1. Control Structures (Medium Priority)
  • Loop analysis
  • Conditional logic