Download presentation
Presentation is loading. Please wait.
Published byMaria Farmer Modified over 9 years ago
1
Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions using Variables Incrementing Values in Variables Accumulating in Variables Exercises 415.111 Monday 12 th May Java Lecture 4 Overview Explorer’s Guide Chapter 3.
2
A place in the computer’s memory –With a unique name It is not the same thing as a variable in algebra or a variable in the social sciences. Whenever you see “variable”, think “storage box”. What is a variable?
3
What we can do with variables? First we can take a box and give it a name: Then we can put a value in the box: We can change the value (contents) inside the box:
4
Declaring is the action of naming a variable and saying what type of data is to be stored Examples: –String message; –String mountain; –int height; All variables must be declared before they are used Declaring variables
5
A variable name must begin with a letter, and then may contain any series of numbers or letters. There must be no spaces between the characters of the name. It DOES matter whether uppercase or lowercase letters are used. E.g. message is not the same as Message. By convention: the first word starts with a small letter and all succeeding words in the name with capital letters. E.g. myMessage. Naming variables
6
message = "Success!"; mountain = "Everest"; height = 8848; Assigning a Value to a Variable
7
Displaying the Value of a Variable E. G. Chapter 3 p56 String message; String mountain; int height; message = "Success!"; mountain = "Everest"; height = 8848; System.out.println(); System.out.println(message); System.out.println(mountain); System.out.println("Height: " + height); message = "Goodbye"; System.out.println(message);
8
If we have: int height; and height = “8848”; We get: Incorrect assignment
9
Meaningful variable names make programs easier to read, easier to change, and easier to debug. Although the meaning of a variable name means nothing to the computer we must use meaningful variable names // A badly written program segment System.out.println(); System.out.println(a); System.out.println(b); System.out.println("Height: " + c); a = "Goodbye"; System.out.println(a);
10
Exercise 1 E.G. p58 What is the output? String sort; int four, two; sort = "chance"; four = 4; two = 2; System.out.println(); System.out.println("You only get a few " + sort + "s."); System.out.println("So make the most of it."); System.out.println("What are you waiting " + four + "?"); System.out.println("Tomorrow may be " + two + " late.");
11
We can assign the result of an expression to a variable. –height = 20 * 3; First the expression is evaluated, –then its value is assigned to the variable on the left-hand side of the assignment operator. Right-hand side can be an Expression E.G. 61
12
We can assign the result of an expression involving integer variables to an integer variable days = weeks * 7; totalDays= nbrYears *365; Expressions using Variables E. G p61
13
// Assigning expressions which contain variables E. G. p61 String mountain; int weeks, days, hours // Assign values mountain = "MT COOK"; weeks = 3; // Evaluate expressions and assign to variables days = weeks * 7; hours = days * 24; // Output results System.out.println(); System.out.println(mountain); System.out.println("Weeks: " + weeks); System.out.println("Days: " + days); System.out.println("Hours: " + hours);
14
// Assigning expressions which contain variables E. G. p61 String mountain; int weeks, days, hours // Assign values mountain = "MT COOK"; weeks = 3; // Evaluate expressions and assign to variables days = weeks * 7; hours = days * 24; // Output results System.out.println();~~~ System.out.println(mountain); MT COOK System.out.println("Weeks: " + weeks);Weeks: 3 System.out.println("Days: " + days);Days: 21 System.out.println("Hours: " + hours);Hours: 504
15
Exercise 3 E. G. p62 What is the output of the following program segment? // Find the area of a tent // Declare the variables int length, width, area; // Assign values to the variables length = 3; width = 4; // Assign the result of the expression to a new variable area = length * width; // Display the result System.out.println(); System.out.println("Area of tent = " + area + " square metres");
16
Exercise 3 E. G. p62 What is the output of the following program segment? // Find the area of a tent // Declare the variables int length, width, area; // Assign values to the variables length = 3; width = 4; // Assign the result of the expression to a new variable area = length * width; // Display the result System.out.println(); System.out.println("Area of tent = " + area + " square metres"); Area of tent = 12 square metres
17
Incrementing Values in Variables E.G. p66
18
Giving a variable a value at the beginning of a program ( counter = 0;) (counter = counter + 1 ;) -----> ---------------------------------------------------- ( counter = 5;) (counter = counter + 1 ;) -------> Java will warn you if you haven’t initialised a variable Initialisation of Variables E.G. p 66
19
// Count to five the slow way E.G. p65 int counter; counter = 0; // increment the value in counter counter = counter + 1; System.out.println(counter); 1 counter = counter + 1; System.out.println(counter); 2 counter = counter + 1; System.out.println(counter); 3 counter = counter + 1; 4 System.out.println(counter); counter = counter + 1; 5 System.out.println(counter); 0 Counter
20
Accumulating in Variables E.G. p71
21
E.G. p70 /* Program to demonstrate that we can accumulate values in a variable */ int total; //Initialise to 10 total = 10; //Accumulate total = total + 5; total = total + 2; //Display the result System.out.println(); System.out.println("Total = " + total);
22
E.G. p70 /* Program to demonstrate that we can accumulate values in a variable */ int total; //Initialise to 10 total = 10; //Accumulate total = total + 5; total = total + 2; //Display the result System.out.println();~~~ System.out.println("Total = " + total);Total = 17
23
Work through, and completely understand, every line of every one of the demo programs in Chapter 3. Complete as many of the Chapter 3 exercises as you can. Home work (before your lab)
24
Check: (i) That both sides of every assignment statement are of the same type: –both String, –or both int. (ii)Check that all variables that need to be initialised are initialised correctly. Checking Your Program
25
All of our programs, except in Lab08, will use variables. Always use meaningful variable names. Read Explorer’s Guide Chapter 3 Run and modify the Chapter 3 Sample programs. Conclusion of the Intro to Variables
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.