Programming chapt 4

¡Supera tus tareas y exámenes ahora con Quizwiz!

Logical operators precedence

! && - rank lower than relational operators || - rank lower than relational operators to avoid precedence problems

Block

Enclosing a group of statements inside a set of braces, should all be indented,

==

Equal to

Stores as number 0

False

>

Greater than

>=

Greater than or equal to

short-circuit evaluation

If any of them are false, program will just skip the rest. Would waste CPU to check

<

Less than

<=

Less than or equal to

&&

(AND) connects two expressions into one. Both must be true. Uses short-circuit evaluation. Best to use when deciding if number is within range

!

(NOT) reverses the 'truth' of an expression. Makes true expressions false and false into true. Has the highest precedence of the C++ operators. Always enclosed in parenthesis unless applied to a variable or simple expression with no other operators

||

(OR) Connects two expressions into one. One or both can be true also uses short-circuit. Best used when deciding if number is outside range

Boolean expressions

(relational expressions) value can be true or false

Disadvantages to using nested if/else

* the code can grow complex and become difficult to understand * because indenting is important in nesting, a long series of nested can become too long and will 'wrap-around' when printed

input validations performed by programs

1. Numbers are checked to ensure they are within range of values 2. values checked for "reasonableness" 3. items selected from menu or other set of choices are checked to ensure they are available options 4. variables are checked for values that may cause problems, such as division by zero

types of integer expressions with switch

1. Variable of any datatypes (char included) 2. expression whose value is of any integer type

rules for expressions

1. When a relational expression is true, it has value 1 2. when expression is false, value is 0 3. any expression that has the value 0 is considered false by if. This includes bool false 4. any expression that has a value other than 0 is considered true by if. This includes bool true

Rules for writing 'if' statements

1. conditionally executed statement should appear on the line after the if statement 2. conditionally executed statement should be indented one 'level' from the if

Set up of switch

1st line: starts with switch followed by integer expression in parenthesis 2nd line: beginning block of several case statements 3rd line: after case if constant expression followed by a colon (constant must be integer literal or integer named constant)

Relational operators

C++ uses to compare numeric data. Determines whether a specific relationship exists between values. All are binary (use 2 operands) higher precedence than assignment operators

Branch

Occurs when part of program causes another to execute. if/else if allows program to branch into paths. Performs test, usually relational, and branches when 1 is true

Division by zero

Mathematically impossible to perform, normally causes program to crash

!=

Not equal to

Conditionally executed

Performed only when a certain condition exists. Statement isn't complete without this, semi-colon doesn't mark end of statement

4.1 Concept

Relational operators allow you to compare numeric and char values and determine whether one is greater than, less than, equal to, or not equal to another

Input validation

Process of inspecting data given to program by user and determining whether valid

Stores as number 1 or anything more than 0

True

Comparing Values for greater than, less than or equal too

valuable for tasks such as examining sales figures, determining profit and loss, checking a number to ensure it is within an acceptable range, and validating input given by user

"fall through"

all statements below one with matching case expression. Good when you want program to branch to same set of statements for multiple case expressions

case statement format

case ConstantExpression // place 1 or more statements

ASCll code

characters stored in memory as integers, every character (even blanks) have this, compares these when two characters are being compared, lowercase have higher codes

Null statement

empty statement that does nothing, will prematurely terminate an 'if' statement

comes into scope

enters section of code that constitutes variables scope

Round off errors

floating-point stored in a way that they can occur, so be careful using (==). To avoid, stick with the greater than or less than

Format of If statement

if (expression) statement;

Trailing else

last else clause that does not have an if statement following. Optional but in many situations can be used to catch errors

Switch statement

lets value of a variable or expression determine where programs will branch. tests value of integer expression and uses value to determine which statements to branch

default statement

line of code that a program will go to if none of case match switch.

scope

part of program where variable may be used

Menu-driven

program that allows the user to determine the course of action by selecting it from a list of actions

Decision structure

program that executes some statements only under certain circumstances. in simplest form, a specific action is only taken when a specific condition exists

conditional operator

provides shorthand method of expressing simple if/else statement consists of (?) and (:)

Menu

screen displaying a set of choices from which the user selects. Select one of the operations by entering a number. Can set up with nested if/else - enters number and program compares number with selections and executes statements that perform that operation

conditional expression

statement using conditional operators. Ternary operator, much like if statement, if true expression between ? and : is executed, gives ability to pack decision-making power into line of code

Sequence structure

statements are executed in sequence without branching off, often need more than one path of execution

if/ else if statement

test a series of conditions, makes certain types of nested decision logic simpler to write

flag

typically a bool variable that signals when some condition exists in a program. When set to false, indicates that condition doesnt exist. When set to true, indicates that it does exist. Initialize with false because arent sure if it exists. Can use integer variables

local scope / block scope

variable defined in set of braces. only used in part between their definition and blocks closing brace

break statement

where to stop, would execute all lines from case to end of block with out


Conjuntos de estudio relacionados

American Government - FINAL REVIEW

View Set

Research Methods in Sports Midterm

View Set

Tell me about yourself - a good answer to this interview question

View Set

ICSI 213: chapter 10 - inheritance (interface and polymorphism)

View Set

Biomolecules: Peptide Bonds: Formation and Cleavage

View Set

Photosynthesis and Cellular Respiration Review

View Set

Chapter 2: Religion and Global Ethics

View Set