Download presentation
Presentation is loading. Please wait.
Published byWinfred Logan Modified over 8 years ago
1
Alterations u assignment is the operation provided by programming languages for altering the value stored by a variable. u The assignment operator (=) uses the following form Variable = computation u For example switchedOn = true ; timeSwitchedOn = now() ; batteryLevel = batteryLevel - batteryUsage(timeOn) ; batteryLevel = batteryLevel + charge() ; u The assignment operator is interpreted as u switchedOn becomes true u timeSwitchedOn becomes now() ;
2
Assignment u The last two examples are particularly interesting. They are interpreted as u The new batteryLevel becomes the current batteryLevel minus the value resulting from batteryUsage(timeOn) ; u The new batteryLevel becomes the current batteryLevel plus the value resulting from charge()
3
Assignment u This type of assignment is used for counting! u To make a computer count you have to u inspect the current state to get the current counter value; u do a computation (e.g. add/subtract) to produce a new value (i.e. new state) u alter the current state by replacing it with the newly computed state
4
Assignment - Counting int i ; //what is the value of i at this point? i = 0 ; //what is the value of i at this point? i = i + 1 ; //what is the value of i at this point? This type of activity (stepping through a series of statements) is called TRACING.
5
if u As mentioned earlier programming languages provide an if statement for inspecting state u The Java syntax is as follows if(state inspection) { What to do if it is true }
6
If…else u Sometimes it is useful to do something if the inspection is true and something else if it is false. u This is usually referred to as an if…else u The Java syntax is as follows if(state inspection) { what to do if it the state inspection is true } else { what to do if it the state inspection is false }
7
while u Again, as mentioned previously, the ability to repeat something is not just useful but actually essential. u There would be no benefit to using a computer if we did not have repetition. u For repetition, programming languages provide a while operation. u The Java syntax is while(not in required state) { Do something to move toward the required state }
8
While example u Consider the counting example earlier. u How could we count 10 times? int i ; //value of i at this point? i = 0 ; //value of i at this point? while(i < 10) {//value of i at this point? i = i + 1 ; //value of i at this point? }
9
Delimiters u Note the use of u { } u ( ) u ; u if u else u while
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.