Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables, Printing and if-statements

Similar presentations


Presentation on theme: "Variables, Printing and if-statements"— Presentation transcript:

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

2 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

3 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 (“ ”)

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

5 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

6 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

7 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

8 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.

9 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.

10 Write the Hello World Program in IntelliJ

11 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

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

13 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?

14 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.

15 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.

16 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.

17 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)

18 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. }

19 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

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

21 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” }

22 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

23 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.


Download ppt "Variables, Printing and if-statements"

Similar presentations


Ads by Google