Basic Programing Constructs PDF
Basic Programing Constructs PDF
USING C
SEMESTER: I SEMESTER
COURSE: BCA
SUBJECT TEACHER: Dr. K.Chitra,
Assistant Professor,
Department of Computer Science
Basic programing constructs
Chapter -2
• There are 3 basic programming constructs
sequential
selection
Iteration
Sequential Construct
In a sequential construct , program statements
are executed one after another.
Input statement
Assignment statement
Output statement
Selection construct
• It is also known as conditional construct. It is
used to indicate decision in program.
• There are different kinds of selection constructs.
They are
simple if
if…else
if…else if
nested if
Switch statement
Iteration constructor
• Some of the statements have to be executed
repeatedly, we can use repetition construct to
perform many iterations.
• Conditional looping
• Unconditional looping
• Conditional looping
Many programs requires that a group of
instructions be executed repeatedly until the logical
condition has been satisfied.
(E-x) while , do..while
• Unconditional looping
Execution of a group of consecutive instructions is
repeated some specified number of times.
(E-x ) for statement
Chapter-3 overview of c
#define pi 3.14
Global Declaration section
• This section is used to define the variables
that would be used in more than one
function.
• This section should be declared before main()
function.
• E-x
int a;
main()
main() function
• All c programs must contain main() function.
• It denotes the starting of the program.
Braces
• Execution of the program begins at the
opening brace { and ends at its closing brace.
Declaration part
• This part is used to declare all the variables
used in executable part.
• The syntax for declaration is
datatype list of variables;
E-x
int a,b;
float b,d;
Executable part
• This part of the program consists of a set of
executable statements.
• Every statement in the declaration part and
executable part ended with ;
e-x
scanf %d%d ,&a,&b);
C=a+b;
printf %d , );
Subroutine
• This section is optional and consists of all the user-
defined functions.
E-x
Function1()
{
Printf hello world ;
}
Function2()
{
Printf wel o e to the world ;
}
Example program
/* program to find circumference of the circle */
#include <stdio.h>
#define pi 3.14
main()
{
float circum,r;
Scanf %f ,&r);
circum=2*pi*r;
printf %f ,circum);
}
Executing a c program
• Creation of program
• Compilation of a program
• Linking of a program
• Executing the program
Creation of program
• Programs should be written in C editor.
• After typing the program ,save the program
with extension .c
Compilation of a program
• The source program should be compiled by
using c compiler.
• If any syntax errors in a source program, then
compiler checks for it.
• Once all the errors are corrected, the compiler
converts the source program into object
program.
Linking the program
• All the library files are linked to the main
program.
Executing the program
• After compilation and linking, the executable
o je t ode will e loaded i the o puter’s
main memory and the program is executed
Programming style
• All the statements should be written in lower
case letters.
• The programmer can write the statement
anywhere between two braces.
• The opening and closing braces should be
balanced.
• Use comment line whenever necessary.