chapter 5-b

17
Slide 1 Chapter 5-b • Attributes of variables

Upload: evers

Post on 05-Jan-2016

10 views

Category:

Documents


0 download

DESCRIPTION

Chapter 5-b. Attributes of variables. Attributes of variables. { int sum = 5; ... ... }. Symbol Table. Identifier. Type. sum. int. 0000 0000 0000 0101. 0110 0010. Address. Value. Storage. Variable Names. sum=0.0 DO I = 1 TO 10 sum = sum + i END DO ... - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 5-b

Slide 1

Chapter 5-b

• Attributes of variables

Page 2: Chapter 5-b

Slide 2

Attributes of variables

{ int sum = 5;......

}

sum int

0000 0000 0000 0101

Identifier Type

Value

0110 0010

Address

Storage

Symbol Table

Page 3: Chapter 5-b

Slide 3

Variable Names

sum=0.0DO I = 1 TO 10 sum = sum + i END DO...t = SQR(SUM)...

FUNCTION sqr(val) val = val*valRETURN VAL

• Names: Fortran is not case sensitive.

•Type: Fortran allows implicit declarations.

Page 4: Chapter 5-b

Slide 4

Naming Conventions

public void actionPerformed (ActionEvent e){

int value = Math.PI;

. . .

}

Method names start with lower case

Class names start with upper case

Constants are all upper case

Local variables are all lower case

Page 5: Chapter 5-b

Slide 5

Aliasing: Variables

union {

short i;

float f;

char c;

} uval;

c

i

f

uval

Aliasing using variables is meant for conserving storage space.

Page 6: Chapter 5-b

Slide 6

Aliasing: Pointers

int i;

int *p=&i;

char *c;

c=(char*)p;p

c

i

Aliasing using pointer variables is not meant for conserving storage space.

Page 7: Chapter 5-b

Slide 7

Static Variables (Fortran)

SUBROUTINE fun()

DATA k/0/

K=K+1

PRINT *, K

RETURN

Call statement Output CALL FUN() 1 CALL FUN() 2 CALL FUN() 3 CALL FUN() 4 . . . . . .

In Fortran, all variables are by default static

Page 8: Chapter 5-b

Slide 8

Static Variables (C)

void fun(){

int k=0;

k++;

printf(“\n %d”,k);

}

Call statement Output fun( ); 1 fun( ); 1 fun( ); 1 fun( ); 1 . . .

fun( ); 1 fun( ); 2 fun( ); 3 fun( ); 4 . . .

void fun(){

static int k=0;

k++;

printf(“\n %d”,k);

}

Page 9: Chapter 5-b

Slide 9

Stack Dynamic Variables

i

ij

i

{ int i; . . . {

int j; . . .

. . . } . . . . . .}

Page 10: Chapter 5-b

Slide 10

Recursion

n=3

n=3

n=2

n=3

n=2

n=1

n=3

n=2

1

n=3

2

6 3

int fact (int n){ if(n==1) return 1; else return n*fact(n-1);}

2

int fact (int n){ if(n==1) return 1; else return n*fact(n-1);}

1

int fact (int n){ if(n==1) return 1; else return n*fact(n-1);}

Page 11: Chapter 5-b

Slide 11

Heap-Dynamic Variables (1)

int size=100;

int *p;

p = (int *) malloc(size*2);

...

...

free(p);

0

99

p

p

Heap allocation requires effective memory management

????

Page 12: Chapter 5-b

Slide 12

Heap-Dynamic Variables (2)

int *node;

node = new int; //Allocates an int cell.

...

...

delete node; //free the allocated memory.

C++

Stack s = new Stack(); Java

Page 13: Chapter 5-b

Slide 13

Scope (C)

int x;...

main(){int k;

......{

int k;......

}...

}

Global variable

Local variable

Local variable

Page 14: Chapter 5-b

Slide 14

Scope (Java)

public float fun(){int[] x = {10, 20, 30};float sum=0;for (int i=0; i<3; i++){

sum=sum+x[i]; }System.out.println(“Sum=”+sum);float v;v=sum/2; return v;

}

Local variables

Page 15: Chapter 5-b

Slide 15

Scope (Java classes)

public class Myclass{

public void add(int y){x = x+y;

}

int x=10;}

Java allows the above type of declaration of instance variablesbut it is not advised.

Page 16: Chapter 5-b

Slide 16

Scope Vs. Lifetime (1)

void compute() {static int k;............

}

Scope of the variable is limited to the function block, while its lifetime is the same as the total program execution time.

Page 17: Chapter 5-b

Slide 17

Scope Vs. Lifetime (2)

void compute() {int value;......printinfo();......

}void printinfo(){

...

...}

Scope of “value” does notextend to the function“printinfo”, but lifetime ofvalue does.