Basic Programing Constructs
Basic Programing Constructs
in
Chapter -2
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
File Downloaded From www.Bustudymate.in
File Downloaded From www.Bustudymate.in
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
Importance of c
• It is robust.
robustness is the ability of a computer system to
cope with errors during execution.
• C has the advantage of assembly language
programming like bit manipulation and features
of high level language programming like
debugging.
• C is called as middle level language since it
combines low level language and high level
language.
File Downloaded From www.Bustudymate.in
File Downloaded From www.Bustudymate.in
Applications of c
• Compilers
• Loaders
• Linkers
• Interpreters
• Operating system
• Database Management system
• Spread sheets
• Subroutine section
f1()
f2()
.
.
fn()
Documentation section
• This section consists of set of comment lines
starting with /* and ends with */
• This section helps the users to understand the
program.
• This section is optional and are not executed
by compiler.
E-x
/* To add two numbers */
File Downloaded From www.Bustudymate.in
File Downloaded From www.Bustudymate.in
Preprocessor section
• This section contains header files which begins
with a #symbol and are extended with .h
• This section is used to include the header file.
E-x
#include <stdio.h>
#include <math.h>
Definition section
• Symbolic constants are included using #define
e-x
#define pi 3.14
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 , );
File Downloaded From www.Bustudymate.in
File Downloaded From www.Bustudymate.in
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 ;
}
File Downloaded From www.Bustudymate.in
File Downloaded From www.Bustudymate.in
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.
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.