VARIABLES & CONSTANTS. Objective By the end of the lesson students should be able to:  Define a constant  Define a variable  Understand the rules for.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
CS150 Introduction to Computer Science 1
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Microsoft® Small Basic
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Introduction to Pascal The Basics of Program writing.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Programming, an introduction to Pascal
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
1 homework Due today: hw #1 (mailing list printout) readings to date: chapter 1 and chapter read appendix B (3 pages on DOS) and and.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 02 (Part II) Introduction to C++ Programming.
A Sample Program #include using namespace std; int main(void) { cout
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Constants, Data Types and Variables
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Chapter 1.2 Introduction to C++ Programming
CST 1101 Problem Solving Using Computers
Chapter 1.2 Introduction to C++ Programming
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Chapter 1.2 Introduction to C++ Programming
Variables Mr. Crone.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
Chapter 3 Machine Language and Assembly Language.
Chapter 3 Machine Language and Assembly Language.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Programming Fundamentals Lecture #4 Overview of Computer Programming
DATA TYPES AND OPERATIONS
Presentation transcript:

VARIABLES & CONSTANTS

Objective By the end of the lesson students should be able to:  Define a constant  Define a variable  Understand the rules for naming variables and constants

FINDING A SOLUTION TO THE PROBLEM THE CONCEPT OF VARIABLES  In the computer, values are stored in memory locations.  There are many memory locations, so in order to keep track of where our values are stored we need to place a label or identifier on a particular memory location.  The label or identifier is called a variable.  A variable is a symbolic name assigned to a memory location that stores a particular value.

THE CONCEPT OF VARIABLES  The word variable is derived from the verb ‘to vary’. This means that the value stored in a particular location can change from time to time, although the label remains the same.  When new values are placed into previously assigned memory locations, the old values are replaced by the new.

Problem  E.g. A program is required to read three numbers then calculate and print the sum. INPUTPROCESSINGOUTPUT 3 numbers e.g. Num1, num2, num3 1.Read num 1, num2, num3 2.Calculate the sum 3.Print Sum Sum

Definition  Variable: Data that changes  A variable is a name associated with a particular memory location.  To refer to these locations we make up variable names. E.g. Num1, Num2, SUM  A variable can only hold one value at a time. When you give a variable a new value, the old one is lost.

FINDING A SOLUTION TO THE PROBLEM THE CONCEPT OF VARIABLES  When we say num1, num2, etc we are actually defining a variable or an identifier for each number, so that we can refer to (access) it later.  We need to tell the computer where to put the result of the addition of num1, num2, num3  Add num1 + num2 + num3 storing in Sum  This would tell the computer that the result of the computation should be stored in a memory location called Sum.

FINDING A SOLUTION TO THE PROBLEM THE CONCEPT OF VARIABLES Likewise, we must address a similar issue in the last two statements where we stated, Divide result by 3 Print result  Computations are performed by the ALU, part of the CPU and results are temporarily stored in registers within the CPU  These values must be stored in memory locations if they are to be accessed later

FINDING A SOLUTION TO THE PROBLEM THE CONCEPT OF VARIABLES We can illustrate the concept of a variable by using diagrams to show the memory locations: Memory locations Num 1 Num 2 Num 3 Variables

THE CONCEPT OF VARIABLES CHOOSING VARIABLE NAMES  It’s good practice to choose variable names that reflect the kind of data that is being stored  It helps the programmer as well as the reader to understand the solution better

Try this example Write a program that allows the user to enter the radius of a circle. The program should then calculate the area of the circle and its circumference. (The area of a circle is pi*radius 2. The circumference of a circle is 2*pi*radius)

Solution INPUTPROCESSINGOUTPUT radiusRead radiusarea Calculate areacircumference Calculate circumference Print area, circumference NB. The user does not have to enter pi. The problem states that the user enters the radius. The value of pi is considered a constant i.e. the value will always remain the same. Pi will be used in the calculation of the circumference and the area of the circle.

Constant  Constant: Data that cannot be varied is a constant.  Can you think of other data that is constant.

Rules for Naming Variables  It must start with a letter  They can be any combination of characters, letters digits or underscore  It cannot exceed 31 characters  There should be no spaces in a variable name.  Pascal is case insensitive  There should be no use of reserved words e.g. var, begin, integer, if  A variable name should be meaningful. Netpay is a good variable name for representing net pay than X.  No special characters are allowed e.g. ()+{} [] / $*=

Advantages of using variables over constants  The user of the program can vary the value by changing the input value when variables are used, whereas the user cannot change the value when constants are used.  There is no need to modify the program when changes of values are required if variables are used, whereas there is need to do so if constants are used.