Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Lecture 2 Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Lecture 2 Basics of Java Programming Language Slides produced by Antonio Martinez Java Programming: From the Ground Up.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
JavaScript, Third Edition
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CPS120: Introduction to Computer Science Lecture 8.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CSC204 – Programming I Lecture 4 August 28, 2002.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
CPS120: Introduction to Computer Science
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Java Java Translation Program Structure
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 2. Program Construction in Java. 01 Java basics.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
2.5 Another Java Application: Adding Integers
ITEC113 Algorithms and Programming Techniques
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Computer Programming Methodology Introduction to Java
Introduction to Programming in Java
Statements, Comments & Simple Arithmetic
5 Variables, Data Types.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Expressions and Assignment
Anatomy of a Java Program
Chapter 2: Introduction to C++.
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Just Enough Java 17-May-19.
Presentation transcript:

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types

Revision of Session 1 Differences between: Procedural and object-oriented languages Interpreted and compiled languages The basics of Java programming How computer programs are constructed Statements, comments and basic arithmetic

Anatomy of a Java program – 1 class myprog { public static void main (String[ ] args) { System.out.println(“Hello world!”); }

Anatomy of a Java program – 2 Reserved words 'class' is a Java reserved word Identifier 'myprog' is an identifier This is a word we make up to identify part of the program (in this case, the program itself) Identifiers must be a single word Remember - Java is case sensitive! class myprog

Anatomy of a Java program – 3 Code braces Braces { or } usually separate off a block of code All programs have several blocks of code Braces must be evenly balanced Braces are often nested class myprog { }

Anatomy of a Java program – 4 Methods Methods contain blocks of functional code Methods are named by an identifier This is a method called 'main' (applications execute their main method on starting) class myprog { public static void main (String[ ] args) { }

Anatomy of a Java program – 5 Statements This program contains a single statement Statements are terminated by a semi-colon class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); }

Anatomy of a Java program – 6 println This statement calls a 'print' method Methods can be given data (arguments) which are contained in brackets class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); }

Anatomy of a Java program – 7 The argument of println here is a string A string is a sequence of characters Java strings are bound in double quotes class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); }

Code Presentation Add coments to clarify what the code does // comments a single line. /* and */ comment multiple lines. Comments should be brief and helpful! Use blank lines to seperate different tasks. Indent code inside curly braces One tab or three/four spaces.

Session 2 - aims & objectives Find out how to declare variables and how to assign values to them Appreciate the main Java variable types: char byte boolean Perform arithmetic using variables Introduce concept of decision making String integer double

Variables Symbolic representation of data of a specific type variables are named by an identifier the type must be declared before a variable can be used e.g. int a Values can be assigned to a variable Java assignment is = e.g. a = 10; b = 5; c = a + b; Variables can be modified during program execution (usually by assignment)

Text-based variable types char a single ASCII character (all letters, all numbers, all punctuation marks etc) bound by single quotes e.g. ‘a’ String a series of characters i.e. text, of any length note capital S at start of the word String bound by double quotes e.g. “some text”

Numeric variable types byte whole number in the range -128 to 127 integer whole number in the range to double floating point numbers (15 decimal places) scientific notation The letter 'e' means "times 10 raised to the power" e.g. 3.45e-3 = ; 1e6 =

Other variable types boolean Used for creating true or false variables Useful in program control and decision making e.g. if condition is true then do this else do something else

Decision making Sometimes you will want the program to perform a function based on a decision e.g. withdrawing or depositing money into a bank account withdrawal - subtract sum from balance deposit - add sum to balance. A decision is required: if deposit then add sum to balance else subtract sum from balance

Arithmetic Operations Addition x=x+10; Subtraction x=x-10; Multiplication x=x*10; Divisionx=x/10; Incrementx++;(equivalent to x=x+1) Decrementx--;(equivalent to x=x-1) The modulo operator gives the remainder when dividing x by some number. Useful for deciding if x is odd/even: x=x%2;

The ‘if’ statement This statement requires a boolean expression as part of its code. e.g. compare numeric variables a and b if (a > b) {... } if (a > b | b == 0) {... } if (a > b) {... } else {... }

Relational operators > greater than < less than == is equal to != is not equal to >= greater or equal to <= less or equal to | or & and

Coming up in Session 3... Flow control! How to easily make your code repeat a task many times.