Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01. OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing.
Lecture 12: Using Classes Yoni Fridman 7/19/01 7/19/01.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
CSC Programming I Lecture 5 August 30, 2002.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
IB Computer Science Unit 1 – Introduction to Java Variables.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Primitive Data Types. Identifiers What word does it sound like?
Variables & Operators 1 Variables Store information needed by the program Must have a TYPE int - can only store a number without a fractional part float,
Primitive Variables.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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 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.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
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.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Fundamentals 2.
Topic 2 Elementary Programming
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Data Types, Identifiers, and Expressions
Variables Store information needed by the program Must have a TYPE
Building Java Programs Chapter 2
Data Types, Identifiers, and Expressions
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Unit-1 Introduction to Java
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Chapter 2: Java Fundamentals
Building Java Programs
Expressions and Assignment
Expressions.
Chapter 2: Java Fundamentals
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01

OutlineOutline ä Getting started with Visual J++ ä A simple program ä Using variables ä Declaring variables ä Initializing variables ä Changing the value of a variable ä Types ä Identifiers ä Expressions and the assignment operator ä Constants ä Getting started with Visual J++ ä A simple program ä Using variables ä Declaring variables ä Initializing variables ä Changing the value of a variable ä Types ä Identifiers ä Expressions and the assignment operator ä Constants

Getting Started With Visual J++ ä Let’s start up Visual J++ and write a simple program. ä ä ä Let’s start up Visual J++ and write a simple program. ä ä

Declaring Variables ä Every variable must be declared before it’s used. ä A variable declaration includes three parts: 1. The type of the variable. 2. The name of the variable. 3. A semicolon. ä (And possibly a fourth part – a comment) ä Examples:  int a; // Declare an integer variable named “a.”  int a, b, c; // Declare 3 integer variables – a, b, and c. ä ä ä Every variable must be declared before it’s used. ä A variable declaration includes three parts: 1. The type of the variable. 2. The name of the variable. 3. A semicolon. ä (And possibly a fourth part – a comment) ä Examples:  int a; // Declare an integer variable named “a.”  int a, b, c; // Declare 3 integer variables – a, b, and c. ä ä

Initializing Variables ä Next, we must initialize the variable – give it a value.  Example: a = 1; // Initialize the variable a to 1. ä Shortcut: Declare and initialize on the same line. ä Examples: ä int a = 1; // Declare a, and then initialize a to 1. ä int a = 1, b, c = 2; // Initialize a to 1, c to 2 (b uninitialized). ä ä ä Next, we must initialize the variable – give it a value.  Example: a = 1; // Initialize the variable a to 1. ä Shortcut: Declare and initialize on the same line. ä Examples: ä int a = 1; // Declare a, and then initialize a to 1. ä int a = 1, b, c = 2; // Initialize a to 1, c to 2 (b uninitialized). ä ä

Changing the Value of a Variable ä After a variable has been initialized, its value can be changed whenever you want. ä Example: ä a = 1; // The value of variable a is 1. ä a = 2; // Now, the value of variable a is 2. ä Don’t redeclare a variable: ä int a = 1; // Declare and initialize a. ä int a = 2; // This gives an error. ä ä ä After a variable has been initialized, its value can be changed whenever you want. ä Example: ä a = 1; // The value of variable a is 1. ä a = 2; // Now, the value of variable a is 2. ä Don’t redeclare a variable: ä int a = 1; // Declare and initialize a. ä int a = 2; // This gives an error. ä ä

TypesTypes ä Every variable must have a type. ä The type indicates what kind of data the variable can store. ä Java has many types, for example:  int – stores an integer number, e.g., 0, 72000, -3.  double – stores a floating point number, e.g., 0.0, 72.4,  boolean – stores either true or false. ä All variables are declared the same way: ä double x = 1.5, y = -3.2; ä boolean z = true; ä Every variable must have a type. ä The type indicates what kind of data the variable can store. ä Java has many types, for example:  int – stores an integer number, e.g., 0, 72000, -3.  double – stores a floating point number, e.g., 0.0, 72.4,  boolean – stores either true or false. ä All variables are declared the same way: ä double x = 1.5, y = -3.2; ä boolean z = true;

IdentifiersIdentifiers ä The name given to a variable is known as the variable’s identifier. ä Identifiers must follow a set of rules: 1. They may contain letters, digits, _, and $. 2. They may not begin with a digit. 3. Identifiers are case-sensitive – X and x are different identifiers (they represent different variables). 1. What’s wrong with these identifiers? 1. hi! 2. a<b 3. 12months ä The name given to a variable is known as the variable’s identifier. ä Identifiers must follow a set of rules: 1. They may contain letters, digits, _, and $. 2. They may not begin with a digit. 3. Identifiers are case-sensitive – X and x are different identifiers (they represent different variables). 1. What’s wrong with these identifiers? 1. hi! 2. a<b 3. 12months

IdentifiersIdentifiers ä Use descriptive names for identifiers. ä A variable that stores my age should be called “age” or “myAge,” not “a.” This makes the program easier to understand. ä Convention: Capitalize the first letter of all but the first word. ä Example: myAge (not myage, MyAge, or MYAGE). ä Keywords are words reserved by Java that can’t be used as identifiers. ä See page 45 of the textbook. ä Also null, true, and false. ä Use descriptive names for identifiers. ä A variable that stores my age should be called “age” or “myAge,” not “a.” This makes the program easier to understand. ä Convention: Capitalize the first letter of all but the first word. ä Example: myAge (not myage, MyAge, or MYAGE). ä Keywords are words reserved by Java that can’t be used as identifiers. ä See page 45 of the textbook. ä Also null, true, and false.

ExpressionsExpressions ä Expressions are composed of operators and operands. ä Operators: +, -, *, /, % (remainder), - (unary). ä Operands: A number or a variable. ä Trick questions: ä What’s the value of 1.0 / 2.0? ä What’s the value of 1 / 2? ä Precedence and associativity follow the rules of algebra. ä Use parentheses if you’re not sure, or for clarity. ä Expressions are composed of operators and operands. ä Operators: +, -, *, /, % (remainder), - (unary). ä Operands: A number or a variable. ä Trick questions: ä What’s the value of 1.0 / 2.0? ä What’s the value of 1 / 2? ä Precedence and associativity follow the rules of algebra. ä Use parentheses if you’re not sure, or for clarity.

The Assignment Operator ä The assignment operator (=) saves the result of an expression in a variable. ä Example: ä a = 2 + 2; ä b = a + 4; ä b = b + 1; ä What will the value of b be now? ä IMPORTANT: = is assignment, not equality. ä Shortcut operators: +=, -=, *=, /=, %=.  Example: a = a + 1 is the same as a += 1. ä ä ä The assignment operator (=) saves the result of an expression in a variable. ä Example: ä a = 2 + 2; ä b = a + 4; ä b = b + 1; ä What will the value of b be now? ä IMPORTANT: = is assignment, not equality. ä Shortcut operators: +=, -=, *=, /=, %=.  Example: a = a + 1 is the same as a += 1. ä ä

ConstantsConstants ä Turn a variable into a constant by writing “final.” ä Unlike variables, the value of a constant cannot be changed once it’s initialized. ä Example: ä final int myAge = 24; // Declare myAge to be a constant. ä myAge = 25; // This won’t work, since myAge is a constant. ä Convention: For a constant’s identifier, use all caps, and separate words with an underscore. ä Example: MY_AGE instead of myAge. ä ä ä Turn a variable into a constant by writing “final.” ä Unlike variables, the value of a constant cannot be changed once it’s initialized. ä Example: ä final int myAge = 24; // Declare myAge to be a constant. ä myAge = 25; // This won’t work, since myAge is a constant. ä Convention: For a constant’s identifier, use all caps, and separate words with an underscore. ä Example: MY_AGE instead of myAge. ä ä

ConstantsConstants ä Constants are never required, but they have several advantages: 1. Programs are easier to read (PI rather than ). 2. Programs are easier to modify. 3. Inconsistencies are less likely. ä Use constants! It may seem like more work, but it will save time in the long run. ä Constants are never required, but they have several advantages: 1. Programs are easier to read (PI rather than ). 2. Programs are easier to modify. 3. Inconsistencies are less likely. ä Use constants! It may seem like more work, but it will save time in the long run.

HomeworkHomework ä Read: 2.9, 2.10 (to bottom of p. 60), 3.9 (to top of p. 114), ä Remember: Both parts of HW1 due Monday at the beginning of class. me with any questions. ä Read: 2.9, 2.10 (to bottom of p. 60), 3.9 (to top of p. 114), ä Remember: Both parts of HW1 due Monday at the beginning of class. me with any questions.