Docsity

Variables and Calculations - C Sharp Programming - Lecture Slides, Slides of C programming

Partial preview of the text

Download Variables and Calculations - C Sharp Programming - Lecture Slides and more Slides C programming in PDF only on Docsity!

Variables and Calculations

02_variables .ppt

Overview of Topics

  • Declaring Variables
  • Data Types
  • Input and Output
  • Arithmetic Operators
  • Handling Exceptions
  • IPO Charts

Variable Declaration

  • Variables are declared by specifying the

data type and name of the variable.

  • Syntax:

dataType variableName;

int intQty; decimal decPrice; string strName;

  • The dataType specifies that only numbers or strings

can be assigned to the named variable.

Variable Initialization

  • When a variable is declared they are assigned a

default value.

  • String variables are set to null or nothing.
  • Numeric variables are set to zero, but the compiler will

require that they be initialized before using them in a calculation or trying to display them.

  • Treat local numeric variables as if they have not been

initialized.

  • Variables can be initialized to a value when declared or

by assigning a value to them.

When to Declare Variables

  • Variables can technically be declared

anytime before they are used.

  • However, in structured programming, we

define our variables at the beginning of a

program or method.

Variables Allocated Memory

  • When a variable is declared it is assigned a

memory location.

  • Each time the program is ran, a different

address may be assigned.

  • The computer uses the memory address, but

we programmers reference that location with

the variable name.

Trivia Question

  • What is a nibble?
  • A nibble is half a byte.
  • A byte is 8 bits, so a nibble is 4 bits. 

Variables in Memory

Variable Name Address (hex)^ Value

intQty A000 0

decPrice A008 0

strName A016 “null”

Preferred Naming Conventions

  • Variables should have meaningful names.
  • Precede each variable name with a prefix of three letters in lowercase to clarify the data type.
  • Camel-case: all lowercase with the first letter of significant words in upper case. intQty, decPrice, strName, intHoursWorked
  • Variables defined as constants (will not change value during the execution) should be in all uppercase with significant words separated with an underscore. decTAX_RATE, decTUITION_RATE
  • Use the const modifier for constants. const decimal decTAX_RATE = 0.07M;

Variable Scope

  • Variables can only be referenced within the section of code it was declared in.
  • Namespace variables may be referenced in entire project (multi-form project).
  • Class-level variables may be referenced in all methods of a form.
  • Local variables may be referenced only within the method in which it was declared.
  • Block variables may be referenced only within a block of code inside a method.
  • A block of code is defined with any matching open and close braces { }.

Lifetime of Global Variables

  • Class-level and Namespace variables exist for

the entire time a form is loaded.

  • Use global variables to store constants.
  • Use global variables to store a running total

(accumulation) or count that is displayed at

the end of a session (ie: number of

transactions posted, total sales).

Class-Level Variables

namespace CS

{ public class CS3Form { int cintQuantitySum; int cintSaleCount; const decimal cdecDISCOUNT_RATE = 0.15M;

private void btnCalculate_Click(…) { … class-level variables can be referenced here }

private void btnSummary_Click(…) { … class-level variables can be referenced here } }

} Docsity.com

Method Variables

namespace CS { public class CS3Form { int cintQuantitySum; int cintSaleCount; const decimal cdecDISCOUNT_RATE = 0.15M; private void btnCalculate_Click(…) { int intQuanity; decimal decPrice; … } private void btnSummary_Click(…) { decimal decExtended; decExtended = intQuantity * decPrice; // Error - intQuantity and decPrice not defined in procedure } } Docsity.com

Global Variable Misuse

  • Do NOT use global variables to store values that change and could be declared as local variables in methods. Even if a variable of the same name and type is needed in various procedures.
  • Using global variables to store local values
    • leads to bad programming habits
    • makes the code in methods less reusable in other programs (same variable names would need to be used in all programs).
  • Later we will be defining our own methods, and we will understand better why Global variables are not necessarily good.