UNIT 2
UNIT 2
OF CONTROL
Structured programming – Algorithms – Structure of
a C program – Variables – Data types – Operators
and expressions – Input and Output statements –
Tokens –Type Conversion – Control statements.
STRUCTURED PROGRAMMING
•In 1968, Edsger Dijkstra, a computer scientist from the
Netherlands, published an goto statement
•The goto statement allows a program to jump to
different parts of the code, which can make the program
difficult to follow, understand, and maintain. Dijkstra
advocated for a more organized and systematic way of
writing code, which he called structured programming.
•Structured programming is a method of writing
programs that focuses on making code easier to
understand, maintain, and debug. It avoids chaotic
control flows (like using goto), and instead organizes
code into logical structures.
CONCEPT OF STRUCTURED PROGRAMMING INCLUDE:
•Top-down approach: Programs are broken down into smaller,
manageable parts (modules). This helps solve complex problems
step by step, starting with the big picture and working down to the
details.
•Modularization: Programs are divided into self-contained blocks or
modules that perform specific tasks. Each module can be developed
and tested independently. This helps make programs easier to read,
modify, and reuse.
•Structured code: Instead of using random jumps (like with goto),
structured programming uses three basic control structures:
•Sequence: Executing statements one after the other.
•Selection: Using structures like if or switch to make decisions in the
code.
•Iteration: Using loops like for and while to repeat certain
operations.
Example of Structured Programming
if (condition) {
// do something
} else {
// do something else
}
Sequence:
Sequence means that each step or process in the algorithm is
executed in the specified order.
Decision:
The decision constructs—if ... then, if ... then …else...
The decision can also be stated as:
if (proposition)
then (process1)
else (process2)
Repetition:
Repetition can be implemented using constructs like the repeat loop, while
loop, and if.. then .. goto .. loop.
The Repeat loop is used to iterate or repeat a process or sequence of processes
until some condition becomes true. It has the general form:
Repeat
Process1
Process2
..............
………..
ProcessN
Until proposition.
Termination:
Termination is the area of study that investigates whether or not a given
algorithm or process will eventually stop. It’s not always possible to determine
in advance if an algorithm will halt, making this a complex problem.
Variable
The variable is a container for a value that may vary during the
execution of the program. For example, in the tea-making
algorithm.
Subroutines:
Flowcharts
A flowchart is a visual tool used to map out the steps of a process or a solution
to a problem.
It is created before writing the actual program, helping to plan the logic clearly.
How Flowcharts Work:
A flowchart is made up of standard shapes (like rectangles, diamonds, etc.) that
represent different actions.
These shapes are connected with arrows, showing the direction of the process
flow.
The activities are written inside the shapes in plain English, not code.
Why Use Flowcharts?
Flowcharts make it easier to understand the logic of a program, especially for
complicated problems.
They help programmers, users, and business people communicate better.
Once a flowchart is ready, writing the actual program becomes much easier.
Flowchart Standards:
Draw on white paper, with flow starting from the top and moving
down/right.
Use standard symbols and templates to ensure neatness and
clarity.
Keep the writing clear, brief, and in English.
Each subroutine (if any) should be on a separate page.
Guidelines for Drawing Flowcharts:
The flowchart must have a clear start and stop.
Follow a logical sequence, with steps going from left to right or
top to bottom.
Avoid too many flow lines crossing each other, and use
connectors if needed.
Test the flowchart with simple data to ensure it works correctly.
STRUCTURE OF A C PROGRAM
Program Structure:
•A C program is organized into sections like declarations and functions.
•Declarations communicate important information about variables and data
types to the compiler.
•Preprocessor directives (like #include) tell the compiler to include special
libraries or make changes to the code before compiling it.
Global Declaration:
•Global variables are declared outside of any function and can be accessed by
any part of the program.
•These variables are visible to all functions in the program, making them
globally accessible.
Local Definitions:
•Every function, including main(), has two sections:
• Local definitions (placed at the start of the function) specify the data
(like variables) the function will use.
• Statements (following the local definitions) are the instructions the
function will execute.
Local variables are only visible to the specific function in which they are
declared, unlike global variables.
Declaration vs. Definition:
•Declaration:
• This tells the compiler about a data object (like a variable) or a function,
including its type.
• It doesn’t allocate memory or provide the full implementation—just
information.
• The main purpose is type checking to ensure that variables or functions
are used correctly in the program.
•Definition:
•A definition actually allocates memory for a variable or provides the
body (implementation) of a function.
•In the case of a variable, it sets aside storage space. In the case of a
function, it provides the actual code that will be executed.
DATA TYPES IN C
“Primitive" data types are the building blocks for creating more complex data
structures. These types define the kind of data a variable can hold.
1.Character (char):
a) Keyword: char
b) Purpose: This type is used to store single characters such as letters, digits,
or symbols (e.g., 'a', 'Z', '1', '@').
c) Size: Typically, it takes 1 byte of memory and can hold values from -128 to
127 (or 0 to 255 if unsigned) based on the ASCII character set.
Integer (int):
a) Keyword: int
b) Purpose: Used for storing whole numbers (i.e., numbers without fractional
parts) like 1, -100, or 5000.
c) Size: Generally, it takes 4 bytes of memory, though this can vary based on the
system, and it can store values in a range like -2,147,483,648 to
2,147,483,647.
Floating-point (float):
d) Keyword: float
e) Purpose: This is used for numbers that have a decimal point or fractional part,
like 3.14 or -0.001.
f) Size: It usually occupies 4 bytes in memory and provides around 6-7 digits of
precision.
#include <stdio.h>
int main()
{
int a = 25;
int b = 2;
int result;
float c = 25.0;
float d = 2.0;
printf(“6 + a / 5 * b = %d \n”, 6 + a / 5
* b);
printf(“a / b * b = %d\n”, a / b * b);
printf(“c / d * d = %f\n”, c / d * d);
printf(“-a = %d\n”,-a);
return 0;
}
Relational Operators Logical Operators
‘A’ < ‘F’ /* gives 1 (is like 65 < 70) */
int m = 1, n = 2, min;
min = (m < n ? m : n); /* min is assigned a
value 1 */
Comma Operator
expressionM = (expression1, expression2, …,expressionN);
sizeof Operator
#include <stdio.h>
int main() char size = 1 bytes
{ short size = 2 bytes
printf(“char size = %d bytes\n”, sizeof(char)); int size = 2 bytes
printf(“short size = %d bytes\n”, sizeof(short)); long size = 4 bytes
printf(“int size = %d bytes\n”, sizeof(int)); float size = 4 bytes
printf(“long size = %d bytes\n”, sizeof(long)); double size = 8 bytes
printf(“float size = %d bytes\n”, sizeof(float)); 1.55 size = 8 bytes
printf(“double size = %d bytes\n”, 1.55L size = 10 bytes
sizeof(double)); HELLO size = 6 bytes
printf(“1.55 size = %d bytes\n”, sizeof(1.55));
printf(“1.55L size = %d bytes\n”, sizeof(1.55L));
printf(“HELLO size = %d bytes\n”,
sizeof(“HELLO”));
return 0;
}
Expression