You are on page 1of 13

Designing High Quality Programs

1. Working with variables and constants


2. Data Types
3. Arithmetic operations
4. Modularization
5. Hierarchy charts
6. Features of good program design
7. Basic Structures
8. Sequence, Selection, Loop
9. Reasons for structure

Course Outcome : Apply concepts of structured program design such as modularity,


sequence, selection, and repetition for given problem statement
Designing High Quality Programs

Data type in any programming language :

Numeric data String

Numbers Non-numeric data (Alpha numeric)

Integers Floating point


(Real Numbers)

Use of Quotation marks


Variables

Definition: Variables are named memory locations whose contents can vary or differ
over time.
eg

int x;
x=10;
x=x+10;

Advantage of variables: ** One memory location can be used repeatedly with


different values,
** We can write program instructions once and then use them
for thousands
of separate calculations
Variables Declaration

A declaration is a statement that provides a data type and an identifier for a


variable
An identifier is a program component’s name

A data item’s data type is a classification that describes the following:


• What values can be held by the item
• How the item is stored in computer memory
• What operations can be performed on the item

• Declare & Initialize


• Where to initialize?
• Garbage value in a variable
Naming Variable

• Every computer programming language has its own set of rules for creating
identifiers
• Reserved keywords
• Different languages put different limits on the length of variable names
• Case sensitive identifiers
• Commonly used variable naming conventions
Convention for naming Examples Languages where
variables commonly used

Camel casing is the myName Java, C#


convention in which the inputData
variable starts with a
lowercase letter and any
subsequent word begins with
an uppercase letter.
It is sometimes called lower
camel casing to emphasize the
difference from Pascal casing.
Naming Variable

Convention for naming Examples Languages where


variables commonly used

Pascal casing is a convention MyName Visual BASIC


in which the first letter of a InputData
variable name is uppercase. It
is sometimes called upper
camel casing to distinguish it
from lower camel casing.
Hungarian notation is a form numHourlyWage C for Windows API
of camel casing in which a stringLastName programming
variable’s data type is part of
the identifier
Snake casing is a convention hourly_wage C, C++, Python, Ruby
in which parts of a variable last_name
name are separated by
underscores.
Naming Variable

Convention for naming Examples Languages where


variables commonly used

Mixed case with underscores Hourly_Wage Ada


is a Last_Name
variable naming convention
similar to snake casing, but
new
Kebobwords
case start with an used
is sometimes hourly-wage Lisp (with lowercase
uppercase
as the letter. last-name letters), COBOL (with
name for the style that uses uppercase letters)
dashes to separate parts of a
variable name. The name
derives from the fact that the
words look like pieces of food
on a skewer.
Variables Declaration

Common rules to name variables:


1. Variable names must be one word.
2. Variable names must start with a letter
3. Variable names should have some appropriate meaning

Assigning values to variables: Concept of l-value and r-value


• a=b
• b=a
• a= 3+b
• b+3=a

Difference between String data and Numeric data


x=5
x=‘5’
Variables Declaration

#include <stdio.h>
int main()
{ int a,b,c;
char x,y,z;
a=5;b=4;
x='5';y='4';
c=a+b;
z=x+y;
printf("\na=%d\tb=%d\taddition of int %d and %d is %d",a,b,c,a,b);
printf("\nx=%c\ty=%c\taddition of char %c and %c is %d",x,y,z,x,y);
return 0;
}
Named Constants

• A named constant is similar to a variable, except it can be assigned a value only


once
• Use a named constant when you want to assign a useful name for a value that will
never be changed during a program’s execution.
eg : Declare pi as a named constant
Named Constants

• A named constant is similar to a variable, except it can be assigned a value only


once
• Use a named constant when you want to assign a useful name for a value that will
never be changed during a program’s execution.
eg : Declare pf as a named constant for printf
Arithmetic Operations

Basic Arithmetic operations in HLL are :


• + (plus sign)—addition
• – (minus sign)—subtraction
• * (asterisk)—multiplication
• / (slash)—division
Modulo, Exponential, Bit-wise operations can be added

• Unary, Binary and Ternary operators


Rules of precedence
• Expressions within parentheses are evaluated first.
• If there are multiple sets of parentheses, the expression within the innermost
parentheses is evaluated first.
• Multiplication and division are evaluated next, from left to right.
• Addition and subtraction are evaluated next, from left to right.
• All the arithmetic operators have left-to-right associativity
The assignment operator has a very low precedence
Properties of Integer data type
Data type Data type of b Data type of Data of result
of a c
int a =7 int b =2 int c Integer division, result is 3
float a=7 int b=2 int c Mixed mode operation result is 3.5 but
truncated to 3 to store in int c
int a=7 float b=2 int c Mixed mode operation result is 3.5 but
truncated to 3 to store in int c
float a=7 float b=2 int c Mixed mode operation result is 3.5 but
truncated to 3 to store in int c
int a=7 int b =2 float c Integer division, result is 3; and then
converted to float to be stored as 3.0
float a=7 int b=2 float c Mixed mode operation result is 3.5,
stored as 3.5
int a=7 float b=2 float c Mixed mode operation result is 3.5,
stored as 3.5
float a=7 float b=2 float c Mixed mode operation result is 3.5,

You might also like