Untitled
Untitled
Untitled
Michelle M. Khalifé
Assignment 1
Byte (byte), 1bytes Single (float), 4bytes Char (char), 2bytes Boolean String, 4bytes address
Int16 (short), 2bytes Double (double), 8bytes (bool), 1byte
Int32 (int), 4bytes
Int64 (long), 8bytes
For whole numbers, For decimal/real For single letters, Truth values: For a sequence of
e.g.: -7, -5, 0, 3, 7, numbers, e.g.: -5.5, digits, special True, False characters (one or
11, 999 0.00, 10.9999 characters and/or more) e.g.: “algorithms
symbols, e.g., ‘a’, ‘b’, and programming”,
‘c’, ‘X’, ‘Y’, ‘Z’, ‘1’, ‘2’, “123456789”, “Joe
‘3’, ‘@’, ‘#’, ‘!,’ ‘?’ Smith”, “Jan-20”
Declaration Syntax
Convention:
Choose an informative name for your variable – typically, you would be looking at one or more nouns.
C# favors camelCase notation for naming local variables. Start with a lower-case letter, then every subsequent word
starts with an upper-case letter. E.g.: myVariable, myVar1, day, weekDay, dayOfTheWeek, hourlyWage, rate, ….
To do so, you must think of any possible value that represents this variable. In the case of the temperature, one
would think: -20, 0, 10, 25, but also 18.5, 7.5, … This will allow you to determine the type. Once you’ve got the type
and the name, you may go ahead assign values to these variables and print them.
#2 [explicit casting]
Casting is when the value of a variable changes type. For example, in int a = 5, the 5 is an integer. If we wish to turn
the 5 into 5.0, i.e., a float, then we need to prepend it with the desired type between parenthesis: (float)5. Seeing
that the 5 is contained in variable a, this becomes (float)a. This results in a float and should be assigned to a float
variable.
Casting syntax:
type1 var1 = some_value; // first, for an assigned &/or scanned value, declare a variable to contain it.
type2 var2 = (type2) var1; // then, cast this variable into another type, & save the result in a variable of that new type
Declare a(n) ________ variable and cast it into a(n) ________ variable. Print the latter. What do you observe in each
case?
a. integer, float
b. float, integer
c. float, double
d. char, integer
e. char, float
f. integer, char
g. float, char
#3 [implicit casting]
“Prompt the user” translates into a Console.WriteLine() function call, which allows you to communicate to the user
what you want them to enter, followed by a Console.ReadLine() function call, which allows you read their input.
1. a. Prompt the user for a float and save it in an integer. Print the integer. What do you observe?
b. Now, prompt the user to enter an integer or a float. This time, save it in the right variable type.
c. Print that variable. What do you observe?
2. a. Prompt the user for an integer and save it in a float. Print the float. What do you observe?
b. Repeat steps 1b and 1c.
#4 [logic]
A statement is either true or false, but it cannot be both at the same time. For instance:
The table on the left depicts the resulting truth value when two statements are joined with logic operators:
S1 S2 && || S !S (not S)
T T T T T F
T F F T F T
F T F T && ||
F F F F || &&
1. Come up with 4 or 8 pairs of statements that would allow you to understand how && and || operate. For
example, the pair below illustrates the first row in the table:
S1: 2 is even
S2: 2 is prime
S1 && S2 => true and S1||S2 => true
2. Throw a third statement S3 in the mix. Let S3 be true, then let it be false. What happens?
#5 [function naming]
main() is a function. It is called main because it is the main point of entry & exit of the program. Other functions that
programmers create will be defined outside main(). Unlike variables, functions do not carry information: they DO
something. They perform an action. Therefore, they are best named with a verb followed by a noun. C# favors
PascalCase notation for function names. The first letter of every word is capitalized while the rest of the word is in
lower-case. E.g.: MakeBananaMangoSmoothie, MakeCappuccino, IsSunny, RunAnalytics, …