0% found this document useful (0 votes)
15 views7 pages

? Programming Elements

The document outlines fundamental programming elements including variables, constants, data types, arrays, expressions, and statements essential for building programs. It also describes control structures such as sequence, selection, and iteration that dictate the flow of execution in a program. Additionally, it emphasizes the importance of functions and subroutines for modular programming and code reuse.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

? Programming Elements

The document outlines fundamental programming elements including variables, constants, data types, arrays, expressions, and statements essential for building programs. It also describes control structures such as sequence, selection, and iteration that dictate the flow of execution in a program. Additionally, it emphasizes the importance of functions and subroutines for modular programming and code reuse.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🎓 Programming Elements & Control

Structures
(BCA – Programming in C / Fundamentals of Programming)

� 1. Programming Elements
Programming elements are the basic building blocks used to write programs.

1 Variables
� Definition:

A variable is a named memory location used to store data whose value can change during
program execution.

� Syntax:
data_type variable_name;

� Example:
int age = 20;
float salary = 25000.50;
char grade = 'A';

� Types of Variables:

 Local Variable (inside function)


 Global Variable (outside function)
 Static Variable (retains value)

2 Constants
� Definition:

A constant is a fixed value that does not change during program execution.

� Types of Constants:
1. Integer constant → 10
2. Float constant → 3.14
3. Character constant → 'A'
4. String constant → "Hello"

� Using #define:
#define PI 3.14

� Using const keyword:


const int x = 100;

3 Data Types
Data types specify the type of data a variable can store.

� Basic Data Types


Data Type Description Example
int Integer value 10
float Decimal value 3.14
char Single character 'A'
double Large decimal 12.3456

� Derived Data Types


 Array
 Pointer
 Structure
 Union

4 Arrays
� Definition:

An array is a collection of elements of the same data type stored in contiguous memory
locations.
� Syntax:
data_type array_name[size]; int a[10]

� Example:
int marks[5] = {10, 20, 30, 40, 50};

� Accessing Elements:
printf("%d", marks[0]);

� Types of Arrays:
1. One-dimensional array
2. Two-dimensional array (Matrix)

Example:

int a[2][2] = {{1,2},{3,4}};

5 Expressions
� Definition:

An expression is a combination of variables, constants, and operators that produces a value.

� Example:
sum = a + b;
area = 3.14 * r * r;

� Types of Expressions:
 Arithmetic Expression → a + b
 Relational Expression → a > b
 Logical Expression → a > 10 && b < 5
 Assignment Expression → x = 10

6 Statements
� Definition:
A statement is a complete instruction executed by the computer.

� Types of Statements:

1. Declaration Statement
2. Assignment Statement
3. Input/Output Statement
4. Control Statement

Example:

int x; // Declaration
x = 10; // Assignment
printf("%d", x); // Output

� 2. Control Structures
Control structures determine the flow of execution of a program.

1 Sequence Control Structure


� Definition:

Statements are executed one after another in order.

� Example:
int a = 10;
int b = 20;
int sum = a + b;
printf("%d", sum);

Flow → Top to bottom

2 Selection Control Structure (Decision


Making)
Used when program must choose between alternatives.
� if Statement
if (a > b) {
printf("A is greater");
}

� if-else
if (a % 2 == 0){
if(a% !=0){
printf(‘no even’)
printf("Even");// f
}else
printf("Odd");t

� Nested if

� switch Statement
switch(1) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
default: printf("Invalid");
}

3 Iteration Control Structure (Looping)


Used to repeat statements multiple times.

� for Loop
for(int i=0; i<5; i++) {
printf("%d", i);
}

� while Loop
int i=0;
while(i<5) {
printf("%d", i);
i++;
}
� do-while Loop
int i=0;
do {
printf("%d", i);
i++;
} while(i<5);

4 Subroutines and Functions

� Function Definition:
A function is a block of code that performs a specific task.

� Syntax:
return_type function_name(parameters) {
// body
}

� Example:
#include <stdio.h>

int sum(int a, int b) {


return a + b;
}

int main() {
int result = sum(10, 20);
printf("Sum = %d", result);
return 0;
}

� Advantages of Functions:
 Code Reusability
 Modularity
 Easy Debugging
 Better Program Structure
� 10 Marks Long Answer Summary
Programming elements include variables, constants, data types, arrays, expressions, and
statements which are used to build programs. Control structures like sequence, selection, and
iteration control the execution flow. Functions and subroutines help in modular programming
and code reuse.

You might also like