Constants Lesson CS1313 Spring 2015 1 Constants Lesson Outline 1.Constants Lesson Outline 2.What is a Constant? 3.The Difference Between a Variable and.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Slide 1 Insert your own content. Slide 2 Insert your own content.
Basic Java Constructs and Data Types – Nuts and Bolts
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
0 - 0.
Addition Facts
Pointer Lesson 2 CS1313 Spring Pointer Lesson 2 Outline 1.Pointer Lesson 2 Outline 2.Pass by Reference Bad Example 3.Pass by Reference Good Example.
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
5.9 + = 10 a)3.6 b)4.1 c)5.3 Question 1: Good Answer!! Well Done!! = 10 Question 1:
Lessons 6.5 Circumference and 8.5 Area of a Circle
11.4 Circumference and Arc Length
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.
CSci 1130 Intro to Programming in Java
Addition 1’s to 20.
CS 240 Computer Programming 1
Test B, 100 Subtraction Facts
Chapter Three Arithmetic Expressions and Assignment Statements
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Standard I/O Lesson Outline
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Introduction to C Programming CE Lecture 2 Basics of C programming.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
How to start Visual Studio 2008 or 2010 (command-line program)
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses.
Circumference Lesson #33. What is Circumference? The distance around the outside of a circle is called the circumference (essentially, it is the perimeter.
Variables & Constants Lesson 2 Outline
Data Types Declarations Expressions Data storage C++ Basics.
Khalid Rasheed Shaikh Computer Programming Theory 1.
Circumference Review. Review What is the relationship between a radius and a diameter? What does a circumference measure? What formulas do we use to calculate.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Do Now:. Circumference What is circumference? Circumference is the distance around a circle.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
Data Types, Variables & Arithmetic
Lecture2.
Numeric Data Types Outline
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Arithmetic Expressions Lesson #2 Outline
Variables Lesson Outline
Arithmetic Expressions Lesson #1 Outline
Variables and Constants
Presentation transcript:

Constants Lesson CS1313 Spring Constants Lesson Outline 1.Constants Lesson Outline 2.What is a Constant? 3.The Difference Between a Variable and a Constant 4.Categories of Constants: Literal & Named 5.Literal Constants 6.Literal Constant Example Program 7.Named Constants 8.Name Constant Example Program 9.The Value of a Named Constant Can’t Be Changed 10.Why Literal Constants Are BAD BAD BAD Tax Program with Numeric Literal Constants Tax Program with Numeric Literal Constants 13.Why Named Constants Are Good Tax Program with Named Constants Tax Program with Named Constants

Constants Lesson CS1313 Spring What is a Constant? In mathematics, a constant is a value that cannot change. In programming, a constant is like a variable, except that its value cannot change.

Constants Lesson CS1313 Spring The Difference Between a Variable and a Constant The difference between a variable and a constant: a variable’s value can vary, but a constant’s value is constant.

Constants Lesson CS1313 Spring Categories of Constants: Literal & Named There are two categories of constants: literal constants, whose values are expressed literally; named constants, which have names.

Constants Lesson CS1313 Spring Literal Constants A literal constant is a constant whose value is specified literally: int literal constants EXAMPLES: 5, 0, -127, , float literal constants EXAMPLES: 5.2, 0.0, , , e+08 char literal constants EXAMPLES: ’A’, ’7’, ’?’ character string literal constants EXAMPLES: "A", "Henry", "What’s it to ya?"

Constants Lesson CS1313 Spring Literal Constant Example Program % cat tax1997_literal.c #include int main () { /* main */ float income, tax; printf("I'm going to calculate the federal income\n"); printf(" tax on your 1997 income.\n"); printf("What was your 1997 income in dollars?\n"); scanf("%f", &income); tax = (income - ( )) * 0.15; printf("The 1997 federal income tax on $%2.2f\n", income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1997_literal tax1997_literal.c % tax1997_literal I'm going to calculate the federal income tax on your 1997 income. What was your 1997 income in dollars? The 1997 federal income tax on $ was $

Constants Lesson CS1313 Spring Named Constants A named constant is a constant that has a name. A named constant is exactly like a variable, except that its value is set at compile time (by initializing it) and CANNOT change at runtime. A named constant is exactly like a literal constant, except that it HAS A NAME. In a named constant declaration, we indicate that it’s a constant via the const attribute, and we MUST initialize it: const float pi = ;

Constants Lesson CS1313 Spring Name Constant Example Program % cat circlecalc.c #include int main () { /* main */ const float pi = ; const float diameter_factor = 2.0; float radius, circumference, area; printf("I'm going to calculate a circle's\n"); printf(" circumference and area.\n"); printf("What's the radius of the circle?\n"); scanf("%f", &radius); circumference = pi * radius * diameter_factor; area = pi * radius * radius; printf("The circumference is %f\n", circumference); printf(" and the area is %f.\n", area); } /* main */ % gcc -o circlecalc circlecalc.c % circlecalc I'm going to calculate a circle's circumference and area. What's the radius of the circle? 5 The circumference is and the area is

Constants Lesson CS1313 Spring The Value of a Named Constant Can’t Be Changed % cat constassign.c #include int main () { /* main */ const float pi = ; pi = 3.0; } /* main */ % gcc -o constassign constassign.c constassign.c: In function ‘main’: constassign.c:7: error: assignment of read-only variable ‘pi’

Constants Lesson CS1313 Spring Why Literal Constants Are BAD BAD BAD When you embed numeric literal constants in the body of your program, you make it much harder to maintain and upgrade your program.

Constants Lesson CS1313 Spring Tax Program with Numeric Literal Constants % cat tax1997_literal.c #include int main () { /* main */ float income, tax; printf("I'm going to calculate the federal income\n"); printf(" tax on your 1997 income.\n"); printf("What was your 1997 income in dollars?\n"); scanf("%f", &income); tax = (income - ( )) * 0.15; printf("The 1997 federal income tax on $%2.2f\n", income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1997_literal tax1997_literal.c % tax1997_literal I'm going to calculate the federal income tax on your 1997 income. What was your 1997 income in dollars? The 1997 federal income tax on $ was $

Constants Lesson CS1313 Spring Tax Program with Numeric Literal Constants % cat tax1999_literal.c #include int main () { /* main */ float income, tax; printf("I'm going to calculate the federal income\n"); 1999 printf(" tax on your 1999 income.\n"); 1999 printf("What was your 1999 income in dollars?\n"); scanf("%f", &income); tax = (income - ( )) * 0.15; 1999 printf("The 1999 federal income tax on $%2.2f\n", income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1999_literal tax1999_literal.c % tax1999_literal I'm going to calculate the federal income tax on your 1999 income. What was your 1999 income in dollars? The 1999 federal income tax on $ was $

Constants Lesson CS1313 Spring Why Named Constants Are Good When you use named constants in the body of your program instead of literal constants, you isolate the constant values in the declaration section, making them trivial to find and to change.

Constants Lesson CS1313 Spring Tax Program with Named Constants % cat tax1997_named.c #include int main () { /* main */ const float standard_deduction = ; const float single_exemption = ; const float tax_rate = 0.15; const int tax_year = 1997; float income, tax; printf("I'm going to calculate the federal income tax\n"); printf(" on your %d income.\n", tax_year); printf("What was your %d income in dollars?\n", tax_year); scanf("%f", &income); tax = (income - (standard_deduction + single_exemption)) * tax_rate; printf("The %d federal income tax on $%2.2f\n", tax_year, income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1997_named tax1997_named.c % tax1997_named I'm going to calculate the federal income tax on your 1997 income. What was your 1997 income in dollars? The 1997 federal income tax on $ was $

Constants Lesson CS1313 Spring Tax Program with Named Constants % cat tax1999_named.c #include int main () { /* main */ const float standard_deduction = ; const float single_exemption = ; const float tax_rate = 0.15; 1999 const int tax_year = 1999; float income, tax; printf("I'm going to calculate the federal income tax\n"); printf(" on your %d income.\n", tax_year); printf("What was your %d income in dollars?\n", tax_year); scanf("%f", &income); tax = (income - (standard_deduction + single_exemption)) * tax_rate; printf("The %d federal income tax on $%2.2f\n", tax_year, income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1999_named tax1999_named.c % tax1999_named I'm going to calculate the federal income tax on your 1999 income. What was your 1999 income in dollars? The 1999 federal income tax on $ was $