Suggested answer key to an accompanying worksheet for the book: Digital Media Primer, Yue
Ling Wong. Copyright (c)2015 by Pearson Education, Inc. All rights reserved.
Worksheet: Syntax Review and Code Writing Exercises for Part 1 of
Chapter 10
Objectives:
Review the syntax by writing general syntax and example code for:
o variables
o assignment statements
o operators
o expressions
The completed worksheet can be used as a quick reference for programming labs and
studying for tests and exams.
Syntax Review:
Variables
Declare and initialize a variable
General Syntax:
Assignment Statements
What do assignment statements do?
Assigns a value to a variable
General Syntax:
Operators
What are operators?
comparison operators:
Assignment Operators
+=, =, *=, /=, %=
a += 5;
is same as the statement:
Assignment Operators
+= vs. = +
= vs. =
a = 8;
a = -6;
Increment and Decrement Operators
++,
Page 2 of 4
a++;
is same as the statement:
a–; is same as the statement:
a = 8;
a–;
Expressions
What are expressions?
Code Writing Exercises:
1. Write one statement to declare a variable (that will be used to store the score of a game)
and initialize the variable to zero.
2. Write one statement to declare a variable (that will be used to store the name of the game
player in a game) and initialize the variable to an empty string.
3. Write one statement to declare a variable (that will be used to store the feedback message
for the game player in a game) and initialize the variable.
Page 3 of 4
5. Write one statement that calculates the sum of 7 and a variable (named balance) and store
6. Write one statement that calculates the double of the value of a variable (named price)
7. Write one statement that calculates the double of the value of a variable (named balance)
8. Write one statement that calculates the value of a variable (named price) divided by 2, and
Page 4 of 4