You are on page 1of 23

Basic Programming Concepts

Session 1
Session Objectives
 Discuss algorithms
 Draw flowcharts
 List the symbols used in flowcharts
 Discuss variables
 List the different data types and make use of them in your own C
programs
 List and explain the different methods used for testing an algorithm
 Test a program using dry run method
 Understand the structure of a sample C program
 Identify the types of errors that can occur

Basic Programming Concepts / 2 of 23


Basic Approach to Writing a
Program
C

BASIC # include <stdio.h>


void main()
10 Input name$ {
char name[15];
20 Print “Hello”; name$ scanf(“Enter name : %s”, &name);
30 END printf(“Hello %S”, name);
}

JAVA
Class Hello
{
public static void main(String args[]) Basic approach to programming
{ has remained the same inspite of
String name;
System.in.readln(name); variations in languages. The
System.out.println(“hello “ + name); three codes perform the same
}
} task.
Basic Programming Concepts / 3 of 23
The Programming Approach to
Solving Problems Classroom

Algorithm is a set of steps that


are performed to solve a
problem. The example below
describes an algorithm Leaving the
classroom
Head towards
the staircase

Go to the
basement

Head for the


cafeteria These are the steps followed when a
student wants to go to the cafeteria
from the classroom.
Cafeteria
Basic Programming Concepts / 4 of 23
Solving a Problem
In order to solve a problem

Understand the problem clearly

Gather the relevant information

Process the information

Arrive at the solution

Basic Programming Concepts / 5 of 23


Pseudocode
It is not actual code. A method of algorithm - writing which uses
a standard set of words which makes it resemble code.

BEGIN
DISPLAY ‘Hello World !’
END

Each pseudocode starts with a BEGIN


To show some value , the word DISPLAY is used
The pseudocode finishes with an END
Basic Programming Concepts / 6 of 23
Flowcharts
It is a graphical representation of an algorithm

START

DISPLAY ‘Hello World !’

STOP

Basic Programming Concepts / 7 of 23


The Flowchart Symbol
Start or End of the program

Computational Steps

Input/Output Instructions

Decision making
& Branching

Connectors

Flow Line
Basic Programming Concepts / 8 of 23
Variables

Memory

Data15 15
Data in memory

Each location in the memory is unique

Variables allow to provide a meaningful name for the location in memory

Basic Programming Concepts / 9 of 23


An Example
BEGIN
DISPlAY ‘Enter 2 numbers’
INPUT A, B
C=A+B
DISPLAY C
END

A, B and C are variables in the pseudocode


Variable names takes away the need for a programmer to access
memory locations using their address
The operating system takes care of allocating space for the variables

To refer to the value in the memory space, we need to only use the variable
name. Basic Programming Concepts / 10 of 23
Guidelines to be followed while
Naming Variables
Variable names should begin with an alphabet

The first character can be followed by alphanumeric characters

Proper names should be avoided while naming variables

A variable name should be meaningful and descriptive

Confusing letters should be avoided

Some standard variable naming convention should be followed


while programming
Basic Programming Concepts / 11 of 23
Data Types
A data type describes the kind of data that will fit into a
variable
The name of the variable is preceded with the data type

For example, the data type int would precede the name varName

Datatype variableName

int varName

Basic Programming Concepts / 12 of 23


Basic Data Types
The basic data types are

int
float char
double boolean

Basic Programming Concepts / 13 of 23


Derived Data Types
Data type Basic Data
Derived data type
Modifiers types

unsigned int
unsigned int (Permits only
positive numbers)

short int
short int (Occupies less
memory place
space than int)

long/longdouble
long int/double (Occupies more
space than
int/double)
Basic Programming Concepts / 14 of 23
Testing the Algorithm
The three types of testing are

Dry Run Structured


Walkthrough
Independent
Inspection

Basic Programming Concepts / 15 of 23


Dry Run
Manual way of testing an algorithm for correctness
The values of the variables are tracked through every line of
the code
A table is first created, with as many number of columns as
the number of variables
The names of the variables are written in the column headers

The values of variables are noted down in the table at every stage
The final result is displayed at the end of the execution of the
code
Basic Programming Concepts / 16 of 23
An Example of Dry Run
Step 1. Start
Step 2. X = 10 X Y M
Step 3. Y = 5 Initial values 10 5 0
Step 4. M = 0 After Step 5 10 5 65
Step 5. M = X+Y+(X*Y) After step 6 10 9 65
Step 6. Y = Y+4 After step 7 10 9 74
Step 7. M = M+Y
Step 8. Display X,Y,M
Step 9. End
Basic Programming Concepts / 17 of 23
Independent Inspection
Algorithm is given to
Person 2

Person 1 Person 2 (not worked on the logic)

Returns the algorithm


to person 1 after using dry run
for testing
Basic Programming Concepts / 18 of 23
Structured Walkthrough
Peers will give
suggestions and
will suggest errors
Algorithm is or inefficiencies
passed on

Peers

The algorithm is accepted totally,


accepted after minor changes or
Person 1 rejected totally
Basic Programming Concepts / 19 of 23
A Sample C Program
# include <stdio.h>
void main() Program working begins here
{
int a, b,c; Variables declared

printf(“Enter the first number : “);


scanf(“%d”, &a);
printf(“Enter the second number : “); Accepting input
scanf(“%d”, &b);
c=a+b;
printf(“The sum of the numbers is %d”, c); Displaying number

Basic Programming Concepts / 20 of 23


Errors
An error is a deviation of a program from the result required

The three types of errors are:

Logical errors

Syntax errors

Run time errors


Basic Programming Concepts / 21 of 23
Other important programming
concepts I

Object Code
Source Code COMPILER O/S
Source Code

Compiler converts source code into object code, that is understood and
executed by O/S.

Basic Programming Concepts / 22 of 23


Other important programming
concepts II

INTERPRETER O/S
Source Code

Each instruction in the source code is the interpreter and passed on to


the O/S , where it is executed.

Basic Programming Concepts / 23 of 23

You might also like