Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept. 2013.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

1 ICS103 Programming in C Lecture 3: Introduction to C (2)
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Introduction to Python
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Basic Elements of C++ Chapter 2.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
Chapter 2 Variables.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Constants, Data Types and Variables
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
TK1913 C++ Programming Basic Elements of C++.
Topic 2 Elementary Programming
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Data Types, Identifiers, and Expressions
ICS103 Programming in C Lecture 3: Introduction to C (2)
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Chapter 2: Java Fundamentals
elementary programming
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
CS2011 Introduction to Programming I Elementary Programming
Engineering Problem Solving with C++ An Object Based Approach
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Presentation transcript:

Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept. 2013

Using Variables Named Storing Location Stores a value Defined by type and identifier Type Type of data being stored – Indicates kind of variable to store

Using Variables Identifier – -Name of variable – - how value will be referred to in a program Example: double radius; –Variable named radius –Type double Number containing decimal points.

Using Variables Assignment - = operator -process of giving a variable a value - stores the value in the memory location referred to by radius 1. double radius = 12.3; 2. double radius; radius = 12.3; Variable declaration Assignment

-Variable name must be on the left -radius = 12.3; // correct = radius; // incorrect - can NOT include variable assignment in output statements - System...(radius = 12.3); // incorrect

Variable Example int variable = 5; System.out.println(variable); variable = 10; System.out.println(variable);

Variable Definitions Can define multiple variables int x, y z; Variable definitions can include assigments int x = 7;

Naming Constants defined in a statement Key word final Type identifier Assignment Example: final double PI = 3.14 Use all UPPERCASE for constants

Naming Constants Easier to understand Easier to modify Only have to change once Cannot change value of constant with an assigment PI = 22/7; // Error

Choosing Identifiers Must start with a letter Contain letters, digits, and the underscore Can be any length Case-sensitive Quickly and clearly understandable Begin with lowercase letters

Choosing Identifiers Keywords – turn blue Identifiers reserved by java Special meaning to compiler Examples double int float

Built-In Data Types double Positive or negative real numbers Uses scientific notation Rounded to 6 significant digits

PDN September 23, 2015 What variables would you create for the following output: Bob worked 17 hours for $ Wages = l7*13.75 Bob makes $ for the 17 hours he works.

When do you use variables? - Repeated values - User Input - Calculations and/or formulas - things that can be changed

What are the benefits to using variables? - Easily understood by developer - Easy to change - Less typos - Less code - Organized

int and long Positive or negative integers Can't store decimals - Error

char Stores a single character Letters, digits, symbols and spaces Requires single quotation marks EX: char ch; ch = ‘A’

char Char 4 and int 4 are NOT the same Arithmetic using char results in wrong answers

Caution: int x = 12; int y = 2*x; x = 4; System.out.println(y); Display reads: 24

What are the steps to accepting user input? -Import library -Create scanner -Prompt user -Get data -Store in a variable -Use/display data

String Library String sequence of characters access library by: #include using namespace std;

Using String string name; sets the type string allows user to enter strings (not numbers) and to output those strings

Case Study Specification 1 st step in programming defines what the program is to accomplish Design tells how the computer will be constructed

Coding writing the program Debugging process of getting a program to work correctly Testing process of selecting different possibilities and to reveal bugs