0% found this document useful (0 votes)
5 views

Progg basics and Arrays

The document serves as an introduction to programming in C, covering fundamental concepts such as tokens, data types, the main function, and basic program structure. It explains decision-making and looping control structures, including various forms of if statements, switch statements, and loop types like while, do-while, and for loops. Additionally, it introduces arrays, including one-dimensional and two-dimensional arrays, along with examples and exercises to reinforce learning.

Uploaded by

akul.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Progg basics and Arrays

The document serves as an introduction to programming in C, covering fundamental concepts such as tokens, data types, the main function, and basic program structure. It explains decision-making and looping control structures, including various forms of if statements, switch statements, and loop types like while, do-while, and for loops. Additionally, it introduces arrays, including one-dimensional and two-dimensional arrays, along with examples and exercises to reinforce learning.

Uploaded by

akul.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Introduction to Basics of

Programming in C

06/04/2025 Dept of I&CT 1


C Tokens

06/04/2025 Dept of I&CT 2


Data Types

06/04/2025 Dept of I&CT 3


main()
 The main function is the point where all C programs start their execution,
independently of its location within the source code.
 it is essential that all C programs have a main function.
 The word main is followed in the code by a pair of parentheses (). That is
because it is a function declaration.
 Optionally, these parentheses may enclose a list of parameters within them.
 Right after these parentheses we can find the body of the main function
enclosed in braces{ }.

06/04/2025 Dept of I&CT 4


Simple C Program
/* Display “This is my first C++ program”
Single line comment*/
#include <stdio.h>

int main( ) // Entry point for program execution


{
// block of statements:
printf(“This is my first C program”);
// block of statements:
return 0;
}

06/04/2025 Dept of I&CT 5


Program to read and display a number
#include<stdio.h>
int main() { //program body begins
int number; //variable declaration
printf("enter number“); // user friendly info display
scanf(“%d”, &number); // reading or input value to the variable
printf("the no. is %d\n”, number); //writing or output variable value
return 0;
} // end of program

06/04/2025 Dept of I&CT 6


Program to check pass or fail
#include<stdio.h>
int main()
{
int RollNo,marks1,marks2,marks3;
float avg;
float minimum = 35.0;
printf("Enter Roll Number and marks of three subjects“);
scanf(“%d%d%d”, &marks1, &marks2, &marks3);
avg = (marks1+marks2+marks3)/3;
if (avg < minimum )
printf(“%d is fail”, RollNo);
else
printf(“%d is pass”, RollNo);
return 0;
}

06/04/2025 Dept of I&CT 7


C decision making and branching statements

1. if Statement
2. switch statement

06/04/2025 Dept of I&CT 8


Different forms of if statement
1. Simple if statement.
2. if…else statement.
3. Nested if…else statement.
4. else if ladder.

06/04/2025 Dept of I&CT 9


Simple if Statement
General form of the simplest if statement:

if (test Expression)
{
statement‐block;
}
statement_x;

06/04/2025 Dept of I&CT 10


If else statement

06/04/2025 Dept of I&CT 11


Nesting of if-else Statements

06/04/2025 Dept of I&CT 12


else if Ladder

06/04/2025 Dept of I&CT 13


switch Statement
• Switch is multiple–branching statement - based on a condition, the control is
transferred to one of the many possible points.
• The most flexible control statement in selection structure of program control.
• Enables the program to execute different statements based on an expression
that can have more than two values. Also called multiple choice statements.

06/04/2025 Dept of I&CT 14


General form:
switch(expression)
{
case value_1 : statement(s);
break;
case value_2 : statement(s);
break; ...
case value_n : statement(s);
break;
default : statement(s);
}
next_statement;
06/04/2025 Dept of I&CT 15
switch- example

06/04/2025 Dept of I&CT 16


Decision Making and Looping Control Structures
• Iterative (repetitive) control structures are used to repeat certain statements for
a specified number of times.
• The statements are executed if the condition is true
• These kind of control structures are also called loop control structures
• Three kinds of loop control structures:
• while
• do while
• for

06/04/2025 Dept of I&CT 17


While statement
Basic format:
while (test condition)
{
body of the loop
}
 Entry controlled loop statement
 Test condition is evaluated & if it is true, then body of the loop is executed.
 After execution, the test condition is again evaluated & if it is true, the body is
executed again.
 This is repeated until the test condition becomes false, & control transferred out of the
loop.
 Body of loop may not be executed if the condition is false at the very first attempt.
06/04/2025 Dept of I&CT 18
Do - While statement
General form:
do
{
body of the loop
}
while (test condition);

06/04/2025 Dept of I&CT 19


for statement
The general form:

for (initialization; test condition; increment)


{
Body of the loop
}
Next statement;

06/04/2025 Dept of I&CT 20


Nesting of for loop
One for statement within another for statement.
for (i=0; i< m; ++i)
{…..
….
for (j=0; j < n;++j)
{……
…..
} // end of inner ‘for’ statement
}// end of outer ‘for’ statement

06/04/2025 Dept of I&CT 21


Jumping out of a loop
• An early exit from a loop can be accomplished by using the break statement.
• When the break statement is encountered inside a loop, the loop is
immediately exited & the program continues with the statement immediately
following the loop.
• When the loops are nested , the break would only exit from the loop
containing it.
i.e., the break will exit only a single loop.

06/04/2025 Dept of I&CT 22


Exiting a loop with break statement

06/04/2025 Dept of I&CT 23


Skipping a part of loop
• Skip a part of the body of the loop under certain conditions
Using continue statement.
• As the name implies, causes the loop to be continued with next iteration, after
skipping rest of the body of the loop.

06/04/2025 Dept of I&CT 24


Exercise
1. Check if a given number is 6. Find cos(x) using series
prime or not 7. Find ex using series
2. Factorial of given 10 8. Print triangle in the following
numbers(do not use arrays)
form using loops until n.
3. Print all odd numbers between
m and n Ex. If n=6
4. Menu driven program to sum 1
all elements entered up to -1 2 3
5. Find sin(x) using series. 4 5 6

06/04/2025 Dept of I&CT 25


Arrays
• An array is a group of related data items that share a common name.
• The array elements are placed in contiguous memory locations.
• A particular value in an array is represented by writing an integer number
called index number or subscript in square brackets after the array name.
• The least value that an index can take in an array is 0.

06/04/2025 Dept of I&CT 26


Arrays
• Array Declaration:
type name [size];
where type is a valid data type (like int, float...), name is a valid identifier &
size specifies how many elements the array must contain.
• size field is always enclosed in square brackets [ ] and takes static values.
For example
• an array salary containing 5 elements is declared as follows:
int salary [5];

06/04/2025 Dept of I&CT 27


Arrays
One Dimensional Array
• A linear list of fixed number of data items of same type.
• These items are accessed using the same name using a single subscript. E.g.,
salary [1], salary [4]
• A list of items can be given one variable name using only one subscript and
such a variable is called a single-subscripted variable or a one-dimensional
array.

• Examples:

06/04/2025 Dept of I&CT 28


Arrays
• Initializing one-dimensional array (compile time)
type array‐name [size]={list of values}
type basic data type
array‐name name of the array.
size maximum number of elements and may be omitted.
List of values values separated by commas.
• E.g., int number[3] ={ 0,0,0} or {0} will declare the variable number as an
array of size 3 and will assign 0 to each element

06/04/2025 Dept of I&CT 29


2D Arrays
• It is an ordered table of homogeneous elements.
• It is generally referred to as matrix, of some rows and some columns.
• It is also called a two‐subscripted variable.

06/04/2025 Dept of I&CT 30


2D Arrays
• For example
int marks[5][3];
float matrix[3][3];
char page[25][80];
• The first example tells that marks is a 2‐D array of 5 rows and 3 columns.
• The second example tells that matrix is a 2‐D array of 3 rows and 3 columns.
• Similarly, the third example tells that page is a 2‐D array of 25 rows and 80
columns.

06/04/2025 Dept of I&CT 31


2D Arrays
• Declaration
type array_name[row_size][column_size];
• For example,
int arr [3][5];
arr represents a two-dimensional array or table having 3 rows
and 5 columns and it can store 15 integer values.

06/04/2025 Dept of I&CT 32


Read a matrix example

06/04/2025 Dept of I&CT 33

You might also like