Variables, Printing and if-statements

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
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.
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
© 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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Mathematical Calculations in Java Mrs. G. Chapman.
CSC 107 – Programming For Science. The Week’s Goal.
Chapter 2 Variables.
Mathematical Calculations in Java Mrs. C. Furman.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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.
Bill Tucker Austin Community College COSC 1315
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Chapter 2: Introduction to C++
Java Variables and Types
Computing and Statistical Data Analysis Lecture 2
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
Computer Programming Methodology Introduction to Java
Chapter 2 - Introduction to C Programming
Chapter 2 Basic Computation
Statements, Comments & Simple Arithmetic
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables ICS2O.
Introduction to Java, and DrJava part 1
Chapter 2 - Introduction to C Programming
Numbers.
Chapter 2 Variables.
C++ Data Types Data Type
Chapter 2 - Introduction to C Programming
Introduction to Java, and DrJava
elementary programming
Chapter 2: Introduction to C++.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 - Introduction to C Programming
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Just Enough Java 17-May-19.
Introduction to C Programming
Presentation transcript:

Variables, Printing and if-statements Lecture 2 Variables, Printing and if-statements

Variables integers AgeOfBaby Variables are containers for different values. In a program, the programmer has to create(instantiate) these variables. To Initialize variables: DataType identifier [= Expression] Data Type: What can this box hold? Identifier (Name): What’s the name of this box? Expression: What do you want to do with this box? (Often you will put something in it.) The whole things is called a statement. Statements are instructions for the computer. Statements end with a semicolon (;) integers AgeOfBaby

Data Types – Primitive Types Integer (int) Holds integers; whole numbers Double (double) Holds numbers with decimal points. Boolean (bool) Holds true of false String (String) Holds words/sentences. Denoted via double quotes (“ ”)

Quick Practice “Hello World” int ageOfBaby = 3; String greetMsg = “Hello World”; integers String Note: Semicolon AgeOfBaby greetMsg

Literals “Hello World” Literals are the “thing” you can put into variables “Hello World” Literals int ageOfBaby = 3; String greetMsg = “Hello World”; integers String AgeOfBaby greetMsg

Naming Variables The name of a variable adheres to a strict set of rules: All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($) After the first initial letter, variable names may also contain letters and the digits 0 to 9.  No spaces or special characters are allowed (periods or dashes).   You cannot use a java keyword (reserved word) for a variable name. if, else, true, false are examples of reserved words Optional (But Good Practice): use camelCaseForNamingVariables

Naming Variables The name of a variable adheres to a strict set of rules: Java is case-sensitive. Different variable names should represent different objects Log, log, LOG are different variables You cannot create multiple objects with the same name int x = 3; int x = 5; int x = 3; String x = "Hello"; BAD

Displaying Information to User Printing to the user requires 1 line of code: System.out.println(<Things to print go here>); To print “Hello World”: System.out.println(“Hello World”); What happens in this code? String greeting = “Hello Dave”; System.out.println(greeting); Turns out, you can print almost anything. The exceptions will be discussed when we get to them.

Statements A statements is the smallest instruction that can be carried out. Statements end with a semicolon ‘ ; ’ What does it mean by smallest instruction? Big instruction Put on your clothes Smaller instructions Put on shirt Put on pants Put on socks Etc.

Write the Hello World Program in IntelliJ

Tricks with assigning numbers to variables What happens when we… 1. Put a double literal in an integer variable? int numberOfPencils = 12.5; Truncating or Not allowed (language dependent) 2. Put an integer literal in a double variable? double height = 65; Try in IntelliJ

Arithmetic Operators Operator Meaning Example + Addition X + 3 - Subtraction X – 3 / Division 9 / x * Multiplication 5 * 6 % Modulus Remainder Test in IntelliJ

Tricky Arithmetic with Variables Add two integers 1 Add two doubles 2 Add integer and double 3 Add integer and String 4 Open IntelliJ to Find Out. What if you store a double in an int? Store and int in a double?

Casting Numbers Store a double in an integer: Truncate Lose data Store an in in a double: Decimal point gets added Safe Changing an objects type from one to another is called casting. double cost = 12.5; int numDollars = (int) cost; Cast the “cost” to an “int” using parenthesis.

If - Statements Allows us to execute different code based on different conditions Am I allowed to drive? Am I in preschool, elementary, highschool or college? if (<condition is true>) { //do something with this code inside the brackets } Code inside the brackets will run if the condition is true. “if” must be lowercase. “If” is incorrect.

Boolean Expressions You can Relational Operators to compare two primitive types to form Boolean expressions Boolean expression return true or false ageToDrink == yourAge heightToRideRollerCoaster <= yourHeight When comparing Strings use “.equals()”, gives you true or false yourName.equals(myName) Conditions will usually look like one of the red pieces of code above. IntelliJ For Examples.

Relational Operators Operator Meaning Example == Equal to if (x == 3) != Not equal to if (x != 3) > Greater than if (salary > 10000) < Less than if (salary < 10000) >= Greater than or equal to if (salary >= 10000) <= Less than or equal to if (salary <= 10000)

If Statements – Multiple Conditions You can put if statements within if statements Print message saying a person can ride a roller coaster if they are 4 feet tall AND over 10 years old. if(personHeight >= 4){ if(personAge > 10){ //you can ride the roller coaster. }

Compound Boolean Expression OR you can put multiple conditions in an if statement to form a compound Boolean expression Compound Boolean expressions: Boolean expressions joined by logical operators Print message saying a person can ride a roller coaster if they are 4 feet tall AND over 10 years old. if (personHeight >= 4 && personage > 10){ //you can ride the roller coaster. } IntelliJ for Examples

Logical Operators Operator Meaning Example ! NOT if (!found) && AND if (x < 3 &$ y > 4) || OR if (age < 2 || height < 4)

If Else Statement Like an if-statement but with a default action of none of the previous conditions are met. Print message saying a person can ride a roller coaster if they are 4 feet tall AND over 10 years old. If they can’t ride, also print an apology message. if(personCanRide){ //print “You can Ride” } else { //print “Sorry you cant Ride” }

Else If Statements Specifies a new condition if the condition above it is false Print message saying a person can ride a roller coaster if they are 4 feet tall OR over 10 years old. if (personIsOver4FeetTall){ //print “You can ride” } else if (personIsOver10YearsOld){ } else { //print “Sorry you can’t ride” } Similar to which operator? || operator

Quick Note if (personIsOver4FeetTall){ //print “You can ride” } else if (personIsOver10YearsOld){ } else { //print “Sorry you can’t ride” } If Statement Block The moment a condition is true, the computer executes the code for that condition the exits the block.