Shared Flashcard Set

Details

C#
Chapter 2 - Variables and Data Types
20
Computer Science
Undergraduate 4
10/20/2024

Additional Computer Science Flashcards

 


 

Cards

Term
Data Types
Definition

Describes the format and size of (amount of memory occupied by) a data item and defines what types of operations can be performed with the item.

 

Simple Types: int, double, decimal, char, string, bool

Other Types: String, Object

Term
Declaring Variables
Definition

The statement that names a variable and reserves storage for it. You can have multiple variables on one line

 

Ex.) int myAge = 24, yourAge = 35;

Term
Concatenate
Definition

The process of joining strings together

 

Ex.) WriteLine("The money is $" + someMoney);

Term
Placeholder
Definition

Holds a position for a variable value within a string. It consists of a pair of curly braces containing a number that indicates the desired variable’s position in a list that follows the string. The first position is always position 0.

 

Ex.) WriteLine("The money is ${0} exactly", someMoney);

 

The above statement replaces "0" with whatever value is held in the variable someMoney.

Term
String Interpolation
Definition

A way to insert variables or expressions directly into a string using {}. It starts with a $ before the quotes, making string building easier and more readable.

 

Ex.)  int someMoney = 45;

string mySentence = $"The money is ${someMoney} exactly.";

WriteLine(mySentence);

 

Output: The money is $45 exactly.

Term
Integral Data Types
Definition

Stores whole numbers or characters, includes: int, short, long, byte, char, etc.

 

 

Term
Floating-Point Data Types
Definition

Contains numbers that can have decimal places

 

Ex.)

 

float - Holds up to 7 significant digits of accuracy

double - holds 15-16 digits of accuracy

decimal - holds 28-29 digits of accuracy but smaller range (used in financial applications)

Term
Arithmetic Operators
Definition

Addition, subtraction, multiplication, division, and remainder. Executes similarly to PEMDAS except you have the remainder operator as well. 

Instead of:

counter = counter +1 

You can shortcut with:

counter+=1;

 

Alternatives: -=, *=, /=, and %=

Term
Prefix Increment (++x) and Decrement (--x)
Definition

int x = 5;

int y = ++x; 

 

//In this case, x is incremented to 6 before its value is assigned to y.

 

Console.WriteLine(x);  // Output: 6

Console.WriteLine(y);  // Output: 6

Term
Postfix Increment (x++) and Decrement (x--)
Definition

int x = 5;

int y = x++; 

 

// Here, x++ means y gets the value 5 (the original value of x), and then x is incremented to 6.

 

Console.WriteLine(x);  // Output: 6

Console.WriteLine(y);  // Output: 5

 

Term
Boolean Variable
Definition

Can hold only one of two values—true or false. Declared with data type bool

 

 

Term
Comparison Operators
Definition

Compares two items, an expression containing one of these returns a Boolean value. 

Ex.)

Less Than:                             <

Greater Than:                        >

Equal To:                               ==   

Less Than Or Equal To:        <=

Greater Than Or Equal To:   >=

Not Equal To:                       !=

Term
ReadLine( ) Method
Definition

A method in C# that is used to read input from the console as a string. It waits for the user to type something and press the Enter key, and then it returns whatever was typed as a string.

Ex.)

 

string name = Console.ReadLine(); 

// Reads user input and stores it in 'name' as a string

Console.WriteLine($"Hello, {name}!");

 

Term
Using The Convert Class
Definition

Convert a string to a number in a different format.

[image]

Term
Parse( ) Method
Definition

An Alternative to using the convert class method to convert a string to a number. Turn a string into its numeric equivalent.

 

Ex.) 

itemPrice = double.Parse(itemPriceAsString);

score = Int32.Parse(ReadLine( ));

Term
char Data Type
Definition

Used to represent a single character. Can hold any single letter, number, symbol, or whitespace character. Enclosed in single quotes unlike strings.

 

Ex.)

 

____ letter = 'A';

Term
Escape Sequences
Definition

Always begins with a backslash, they are ways to store characters, even nonprinting characters like "tab" in a char variable. Ex.)

 

\t  - tab

\n - newline

\"anyword"\ - adds quotations around a word

 

Term
string Data Type
Definition

Holds a series of characters, values are expressed within double quotation marks. 

 

_____ firstName = "Bob";

Term
String Comparison Methods
Definition

Equals( ): takes the format name1.Equals(name2); its value is true or false.

CompareTo( ): takes the format name1.CompareTo(name2); and its value is integer—0 when the compared strings are equal, a negative number if the first string is less, and a positive number if the second string (the one in parentheses) is less.

Term
Methods for Manipulating Strings
Definition

Length: a property not a method, therefore does not need parentheses. It gives you the length of a string. e.g. name.length

 

StartsWith( ): determines whether a string starts with certain characters and returns true or false. e.g. name.StartsWith("Wi")

 

Substring( ): Extracts a portion of a string from a starting point for a specific length

[image]

 

Supporting users have an ad free experience!