0% found this document useful (0 votes)
27 views17 pages

PPL Unit2

The document covers various programming concepts including variable scope (local and global), binding (static and dynamic), pointers and references, arithmetic and relational expressions, data types, control structures, branching statements, assignment statements, operator and function overloading, type checking, and type conversion. It provides examples in C and C++ to illustrate these concepts. Additionally, it discusses the implications of binding variables in SQL and the importance of type compatibility in programming.

Uploaded by

Manoj D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views17 pages

PPL Unit2

The document covers various programming concepts including variable scope (local and global), binding (static and dynamic), pointers and references, arithmetic and relational expressions, data types, control structures, branching statements, assignment statements, operator and function overloading, type checking, and type conversion. It provides examples in C and C++ to illustrate these concepts. Additionally, it discusses the implications of binding variables in SQL and the importance of type compatibility in programming.

Uploaded by

Manoj D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit-2

1) Scope:
A variable's scope is the region of a program where it can be
accessed, and its lifetime is the duration of time it exists in
memory:
Scope
The scope of a variable is determined by where it is declared in
the file. A variable's scope can be local or global.
A local variable is only accessible within the function or code
where it is declared.
A global variable is accessible throughout the entire program.
Lifetime
The lifetime of a variable is the amount of time it exists in
memory during program execution.
A variable's lifetime starts when it is allocated in memory and
ends when it is deallocated or the program terminates.
Local Scope of Variable:
#include <stdio.h>
int main()
{
int my_num = 7;
{
my_num = my_num +10;
printf("my_num is %d",my_num);
}
Explanation:
• The main() function has an integer variable my_num that's
initialized to 7 in the outer block.
• There's an inner block that tries to add 10 to the
variable my_num.
Output:
my_num is 17

Local Scope Error:


#include <stdio.h>
int main()
{
int my_num = 7;
{
int new_num = 10;
}
printf("new_num is %d",new_num);
return 0;
}
Output:
Line Message
9 error: 'new_num' undeclared (first use in this function)
Global Scope of Variables
#include <stdio.h>
int my_num = 7;
int main()
{
printf("my_num can be accessed from main() and its value is
%d\n",my_num);
my_func();
return 0;
}
void my_func()
{
printf("my_num can be accessed from my_func() as well and its
value is %d\n",my_num);
}
Output:
my_num can be accessed from main() and its value is 7
my_num can be accessed from my_func() as well and its value is 7
2) BINDING:
A binding variable is a variable that is associated with a
command or formula, and cannot occur freely:
An alternative way to pass data to the database.
In SQL
Bind variables are also known as bind parameters or query
parameters.
They are often used in WHERE clauses to filter data.
Bind variables can help prevent SQL injection attacks, but they
can also present security risks.
Instead of putting the values directly into the SQL statement,
you just use a placeholder like ? , :name or @name and provide the
actual values using a separate API call.
There are two types of binding:
• Static binding: Also known as early binding, this occurs before
the program runs.
For example, a direct C function call is static binding.
• Dynamic binding: Also known as late binding or virtual
binding, this occurs while the program is running.
For example, a C++ virtual method call is dynamic binding.
Example of static binding
class Dog{
private void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}

Example of dynamic binding


class Animal{
void eat(){System.out.println("animal is eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Animal a=new Dog();
a.eat();
}
}
Output: dog is eating...
3)Pointer and Reference:
What is Reference?
A reference is a variable that is referred to as another name for
an already existing variable. The reference of a variable is created by
storing the address of another variable.
A reference variable can be considered as a constant pointer
with automatic indirection.
It is mainly used in 'pass by reference'
Syntax:
int &a = i;
Example:
#include <iostream>
using namespace std;
int main()
{
int i=8; // variable initialization
int &a=i; // creating a reference variable
cout<<"The value of 'i' variable is :"<<a;
return 0;
}
Output:
The value of 'i' variable is :8
What is Pointer?
A pointer is a variable that contains the address of another
variable.
It can be dereferenced with the help of (*) operator to access
the memory location to which the pointer points.

Syntax:
int *ptr;
Example:
4) Arithmetic Expression:
1.Infix,Postfix,Prefix

Relational Expression:

Boolean Expression:
5) Data Types:

6) Control Structures:

i)Syntax, ii)Example, iii)Flowchart


7) Branching Statement
A branching statement is a computer program instruction that
changes the order of instructions that are executed.
Branching statements allow the program to jump to a different
part of the code and make decisions about which parts of the code to
execute.
Some common branching statements include:
• Break: Terminates the current structure
• Continue: Stops the current iteration of a loop and starts the
next one
• Return: Causes a function to jump back to the function that
called it
8) Assignment Statement
An Assignment statement is a statement that is used to set a
value to the variable name in a program.
a=10;
Assignment Statement Forms
1. Basic Form: int a=5;
2. Tuple Assignment: a,b=50,10;
3. Multiple-target Assignment or Chain Assignment: a=b=20;
4. Augmented Assignment: a+=10;

9) Operator overloading
It is a programming technique that allows operators to have different
meanings depending on the arguments they are used with.
This is a type of polymorphism, and is also known as operator ad hoc
polymorphism.
Operator that cannot be overloaded are as follows:
o Scope operator (::)
o Sizeof()
o ternary operator(?:)

Function Overloading
Function Overloading is defined as the process of having two or more
function with the same name, but different in parameters is known as function
overloading in C++.
In function overloading, the function is redefined by using either
different types of arguments or a different number of arguments.
10) Type Checking
Type checking is the process of verifying that the types of all
variables, expressions, and functions are according to the rules of the
programming language.
It means operations operate on compatible data types, which
helps in preventing errors and hence ensures correctness of the
program.

Types of Type checking:


1. Static(at compile time)
2. Dynamic(at Run time)

11) Type Conversion


Type conversion is the process of converting one data type to
another. The type conversion is only performed to those data types
where conversion is possible.
In type conversion, the destination data type can’t be smaller
than the source data type. Type conversion is done at compile time
and it is also called widening conversion because the destination
data type can’t be smaller than the source data type.
There are two types of Conversion:
1. Implicit Type Conversion[‘automatic type conversion’]
2. Explicit Type Conversion[‘type casting’]
Implicit Type:

Example:
#include <stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
printf("x = %d ", x);
return 0;
}
Output:
107
Explicit Type:

It is user-defined. Here the user can typecast the result to make it of


a particular data type.
The syntax in C Programming:
(type) expression
Example:
int main()
{
float a = 1.5;
int b = (int)a;
printf("a = %f\n", a);
printf("b = %d\n", b);
return 0;
}
Output: a=1.500, b=1

You might also like