Basic C++ Flashcards
selection structure (definition)
statement that allows the program to choose between alternative actions (make a decision)
selection structure (examples)
- ifj
if statement building blocks are…
- logical expressions
action
a C++ statement
C++ statements that can be actions
- output (cout)
bool data type
- used to store a logical value
bool example code
bool valid, finished;
logical (Boolean) expression (definition)
an expression that evaluates to true or false
relational operators
operators that are used to compare values
logical (Boolean) expression (facts)
- in C++, any non-zero value is considered true, 0 is considered false
relational operators (definition)
operators that are used to compare values
relational operators (examples)
< <= > >=
Equality operator (symbol)
==
Assignment operator (symbol)
=
Equality operator (==) vs. Assignment operator (=)
The equality operator (==) is used to compare values the assignment operator (=) is used to store values. It is important to use the == operator in logical expressions.
Compare like types (tip)
When creating logical expressions is to best to
logical operators (definition)
operators used to connect or change logical expressions
logical operators (examples)
! - not
Operator precedence
!
short circuit evaluation
process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known.
basic if statement syntax
if (logical expression)
if statement semantics
evaluate logical expression
extended form of if statement syntax
if (logical expression)
extended form of if statement semantics
evaluate logical expression
Nested if statements
if the logic needed requires more than two alternatives, __________ can be used.
repetition structure
statement that allows an action to be repeated (iteration, looping)
C++ repetition statements (examples)
while
while statement syntax
while (logical expression)
while statement semantics
step 1: evaluate expression
infinite loop definition
if the expression remains true, the loop will not be exited
++ increment operator (e.g., num++;)
unary operator used to add 1 to a variable
– decrement operator (e.g., num–;)
unary operator used to subtract 1 from a variable
batch processing
all input gathered together and placed in a file, program designed to get data from file as needed (no prompting required)
Linux input redirection operator: <
changes the default input source to a specified file
Linux input redirection operator: >
changes the default output destination to a specified file
Linux redirection sample code
[bobby ~]$ ./a.out < myinput > myoutput
The key to designing a program to run in batch mode.
Design the program as if were interactive but leave out the prompts.
Count-controlled looping
Loop designed to run a specific
Sentinel-controlled loop
Loop designed to repeat an action until a special value is encountered.
Sentinel-controlled syntax
while (data != sentinel)
End-of-file controlled loop
Loop designed to continue processing data until all data in the file has been read.
End-of-file controlled loop syntax
!cin.eof()
Flag-controlled loop
Loop where bool variable is used to control
Flag-controlled loop syntax
bool finished = false; //finished is the flag