Programming week 2
RAM
(random access memory), memory that is available for your programs to use
Example of good code
//player just leveled up, so we need to increase their health by a factor relative to their level! health = health + (level*1.2) //player just got hit by poisoned dart/ poisoned = true;
_________ is the output stream operator indicating data that will be displayed on the console when used in collaboration with std::cout
<<
assignment opeator in C++
=
translation unit
A code file with translations applied to it
translation
A phase that happens before code is compiled
value
A single piece of data, stored in memory somewhere
A name declared in a namespace may still be mistaken for an identical name declared in another scope
False
For C++, you can start a new line ANYWHERE in a line of code
False
In C++ the objects refer to a variable, data structure in memory, or function
False
Preprocessor macro directives are still commonly used in modern program.
False
a variable is ALWAYS unitialized if it was not given a value at definition (initialized)
False
a variable is always uninitialized if it was no given a value at definition (initializated)
False
comments remain in code, unchanged after compilation
False
every c++ program must have an int main() though some compilers allow a void main() function
False
using directives such as "using namespace std;" are preprocessor directives
False
when creating multiple variables on the same line, in one command, it is possible to create variables of differing types
False
What are some debugging tools built into Visual Studio that allow you to "watch" the values stored in a variable when a program is running in debug mode and execution is paused:
Hover mouse over the variable
Autos
The variables currently being "acted upon"
Locals
The variables that exist within the current code block. That is, within the current set of { }
reasons to comment out code
To find the source of an error. You're working on a new piece of code that won't compile yet, and you need to run the program. You want to replace one piece of code with another piece of code. You've written new code that compiles but doesn't work correctly, and you don't have time to fix it until later
A good strategy for writing code is add one piece at a time, make sure it compiles, and test it. WHen your sure its working, move to next piece
True
ALL lines are ignored by the compiler that are between the /* symbol and the */ symbol, as these denote a multi-line comment.
True
It is possible to define multiple variables of the same type in a single statement by separating the names with a comma
True
Some operators can unary or binary, depending on how they are being used
True
There can be only ONE function called "main" in your program
True
Unless you have an explicit reason to do otherwise, you should always initialize your variables upon creation.
True
Variable initialization can ONLY happen when a variable is defined
True
You should favor list (uniform) initialization over copy and direct initialization.
True
a function always has () after it's name, though there may be things between the ( and the )
True
a function is a named collection of statements that executes sequentially
True
blank lines are ignored by the compiler
True
curly braces {} are often used to indicate a block of code which groups multiple statements such as for function bodies
True
operations can be chained together such that the output of one operator can be used as the input for another operator
True
there can only be one function called main in your program
True
when you use an identifier that is defined inside a namespace, you have to tell the compiler what the identifier lives inside the namespace
True
Watch
Variables the programmer manually added to the window
What is best practice for naming an identifier?
When working in an existing program, use the conventions of that program (even if they don't conform to modern best practices). Use modern best practices when you're writing new programs. avoid naming your identifiers starting with an underscore, as these names are typically reserved for OS, library, and/or compiler use. Third, your identifiers should make clear what the value they are holding means (particularly if the units aren't obvious). Identifiers should be named in a way that would help someone who has no idea what your code does be able to figure it out as quickly as possible. In 3 months, when you look at your program again, you'll have forgotten how it works, and you'll thank yourself for picking variable names that make sense. However, giving a trivial variable an overly complex name impedes overall understanding of what the program is doing almost as much as giving a widely used identifier an inadequate name. Therefore, a good rule of thumb is to make the length of an identifier proportional to how widely it is used. An identifier with a trivial use can have a short name (e.g. such as i). An identifier that is used more broadly (e.g. a function that is called from many different places in a program) should have a longer and more descriptive name (e.g. instead of open, try openFileOnDisk) In any case, avoid abbreviations. Although they reduce the time you need to write your code, they make your code harder to read. Even if the abbreviation is unambiguous, it takes the reader a moment to figure out what you meant. Code is read more often than it is written, the time you saved while writing the code is time that every reader, including the future you, wastes when reading it. If you're looking to write code faster, use your editor's auto-complete feature.
Code implementing undefined behavior may exhibit any of the following symptoms:
Your program produces different results every time it is run. Your program consistently produces the same incorrect result. Your program behaves inconsistently (sometimes produces the correct result, sometimes not). Your program seems like its working but produces incorrect results later in the program. Your program crashes, either immediately or later. Your program works on some compilers but not others. Your program works until you change some other seemingly unrelated code.
expression
a combination of literals, variables, operators, and explicit function calls that produce a single output value.
literal
a fixed value that has been inserted directly into the source code
operation
a mathematical calculation involving zero or more input values
variable
a named object
preprocessor
a phase of translation
expression statement
a statement that consists of an expression followed by a semicolon. When the statement is executed, the expression will be evaluated (and the result of the expression will be discarded).
instantiation
act of creating an object and assigning a memory address
conditional compilation preprocessor directives
allow you to specify under what conditions something will or won't compile. There are quite a few different conditional compilation directives, the three that are used by far the most are: #ifdef, #ifndef, and #endif
instance
an instantiated object
data
any info that can be moved, processed, or stored by a compiler
#define directive
can be used to create a macro. In C++, a macro is a rule that defines how input text is converted into replacement output text.
program
collection of instructions that manipulate data to produce a desired result
operands
input values in a mathematical operation
Preprocessor directives
instructions that start with a # symbol and end with a newline (NOT a semicolon).
_______ have a special meaning within the C++ language
keywords
identifier
name of the object
integer (int)
number that can be written without fractional component (2, 0, -25)
unary
operators that act on one operand
binary
operators that act on two operands (known as left and right)
Ternary
operators that cat on three operands, only one of these in C++
#include <iostream> is a special instruction called a:
preprocessor directive
the process of making structural changes to your code without changing its behavior (to make it more maintainable) is called ________
refactoring
Object
region of storage (memory) that has a value and other associated properties
result
single value after evaluation
Which commands can be used to move the cursor to the next line
std::cout << std::endl; std::cout << '\n';
data type (type)
tells compiler what type of value (number, letter, text, etc.) the variable will store
uninitialized
the object has not been given a known value yet
initialization
the object is given a known value at the point of definition
assignment
the object is given a known value beyond the point of definition
#include directive
the preprocessor replaces the this directive with the contents of the included file. The included contents are then preprocessed (along with the rest of the file), and then compiled.
evaluation
the process where each of the terms in the expression is evaluated until a single value remains
operator
the specific operation to be performed, denoted by a construct (symbol or pair of symbols)
it is possible to grab multiple words of input on a single line using std::cin
true
Programming statements:
type of instruction that causes program to perform some action, most common instruction in C++, smallest independent unit of computation in C++ language, act like a sentence in natural language, most end in a semicolon, a single statment may compile into many machine language instructions
Mark all that are true about a programming statement
type of instruction that causes the program to perform some action most common type of instruction in a C++ program smallest independent unit of computation in the C++ language act much like sentences do in natural language Most (but not all) statements in C++ end in a semicolon a single statement may compile into many machine language instructions
______ _______is the result of executing code whose behavior is not well defined by the C++ language
undefined behavior
A variable that has not been given a known value (usually through initialization or assignment) is called
uninitialized variable
uninitialized variable
variable that has not been given a known value through initialization or assignment
hat are some debugging tools built into Visual Studio that allow you to "watch" the values stored in a variable when a program is running in debug mode and execution is paused
watch window
run-time
when a program is run
compile-time
when the program is compiled