Variables and Decisions Flashcards
In a program, a (_____) is a named item, such as x or numPeople, used to hold a value.
variable
An (_______) assigns a variable with a value, such as x = 5. That assignment means x is assigned with 5, and x keeps that value during subsequent assignments, until x is assigned again.
assignment
In programming, = is an (______) of a left-side variable with a right-side value. = is NOT equality as in mathematics. Thus, x = 5 is read as “x is assigned with 5”, and not as “x equals 5”. When one sees x = 5, one might think of a value being put into a box.
assignment
What are the basic fundamental data types in C++?
Name Description Size* Range*
char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short int (short) Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false. 1byte true or false
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t Wide character. 2 or 4 bytes 1 wide character
A(n) ________ represents a storage location in the computer’s memory.
Variable
Data items whose values do NOT change while the program is running are
literals
You must have a ________ for every variable you intend to use in a program
variable definition
What are the rules of a variable identifier
Neither spaces nor punctuation marks or symbols can be part of an identifier.
Only letters, digits and single underscore characters are valid.
In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ), but in some cases these may be reserved for compiler specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case can they begin with a digit
Besides the decimal number system that is most common (base 10), two other number systems that can be used in C++ programs are
octal and hexadecimal
Which of the following lines must be included in a program that has string variables?
include
As you may see in the previous example, strings can be initialized with any valid string literal just like numerical type variables can be initialized to any valid numerical literal. Both initialization formats are valid with strings:
string mystring = "This is a string"; string mystring ("This is a string");
A programmer should choose a variable’s type based on the type of value held.
Integer variables are typically used for values that are counted, like 42 cars, 10 pizzas, or -95 days.
Floating-point variables are typically used for measurements, like 98.6 degrees, 0.00001 meters, or -55.667 degrees.
Floating-point variables are also used when dealing with fractions of countable items, such as the average number of cars per household.
Branch basics (If)
In a program, a branch is a sequence of statements only executed under a certain condition. Ex: A hotel may discount a price only for people over age 60. An if branch is a branch taken only IF an expression is true.
If-elseif-else branches
s. An if-else can be extended to an if-elseif-else structure. Each branch’s expression is checked in sequence; as soon as one branch’s expression is found to be true, that branch is taken. If no expression is found true, execution will reach the else branch, which then executes.
What is a flag?
Generally flag is a variable which signels that some event happened (flag raised) or not.