c++ ch3

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

What library can setprecision() be found?

<iomanip>

Branch

A branch is a program path taken only if an expressions value is true.

nested if-else statements

A branches statements can include any valid statements including another if-else statement, which are known as nested if-else statements.

short circuit evaluation

A logical operator evaluates operands from left to right. Short circuit evaluation skips evaluating later operands if the result of the logical operator can already be determined. The logical AND operator short circuits to false if the first operand evaluates to false, and skips evaluating the second operand. The logical OR operator short circuits to true if the first operand is true, and skips evaluating the second operand.

Logical Operators

A logical operator treats operands as being true or false, and evaluates to true or false. Logical operators include AND, OR, and NOT. (&&, || and !, respectfully).

Relational Operators

A relational operator chekcs how one operands value relates to another, like greater than. Relational operators (known thus far) are <, <=, > and >=.

String character indices

A string is a sequence of characters in memory. Each string character has a position number called an index, starting with 0.

Switch statement

A switch statement can more clearly represent multi-branch behavior involving a variable being compared to constant values. The program executes the first case whose constant expression matches the value of the switch expression, executes that case's statements, and then jumps to the end. If no case matches, then the default case statements are executed. The switch statements expression should be an integer or a char. The expression should not be a string or floating-point type. Each case must have a constant expression like 2 or 'q'; a case expression cannot be a variable.

Equality Operators

An equality operator checks whether two operands values are the same (==) or different (!=). An expression involving an equality operator evaluates to a Boolean value.

Exception

An exception is a detected runtime error that commonly prints an error message and terminates the program.

If statement

An if statement executes a group of statements if an expression is true. Braces {} surround surround the if branches statements (Braces are sometimes called curly braces).

conditional expressions (if-else short hand)

An if-else statement can be written as a conditional expression. A conditional expression has the form condition ? expressionWhenTrue : expressionWhenFalse; All three operands are expressions. If the condition evaluates to true, then expressionWhenTrue is evaluated. If the condition evaluates to false, then expressionWhenFalse is evaluated. If the conditional expression evaluates to whichever whichever of those two expressions (x == 2) ? 5 : 9 * x; //evaluates to 5. Good practice is to restrict usage of conditional expressions to an assignment statement, as in y = (x == 2) ? 5 : 9 * 2; common practice is to put parentheses around the first expression to enhance readability.

if-else branches

An if-else structure has two branches. The first branch is taken if an expression is true, else the other branch is taken. It executes one group of statements when the expression is true, and another group of statements when the expression is false.

Boolean data type

Boolean refers to a quantity that only has two possible values, true or false. The language has the built-in data type bool for representing Boolean quantities. A Boolean variable may be set using true or false keywords. A Boolean variable may also be set to the result of a logical expression. Ex: isOverweight = (userBmi >= 25); assigns isOverweight with the result of the expression userBmi >= 25.

What kind of operators does C++ have built into the language?

C++ is rich in built-in operators and provide the following types of operators: arithmetic, relational, logical, bitwise, assignment, and misc operators.

If-elseif-else branches

Commonly, a programmer wishes to take multiple (three or more) branches. An if-else can be executed 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 to be true, execution will reach the else branch, which then executes. A branch only executes if the branches expression is true, if none is true, none will execute. Execution will simply proceed to the next statement following the if-elseif statement. As soon as one branch evaluates to true, that branches statements will execute (and no subsequent branch is considered).

Ins + outs of string comparisons

Equal strings have the same number of characters, and each corresponding character is identical. A programmer can compare two strings using the equality operators, i.e., == and !=. A common error is to forget that case matters in a string comparison. A programmer can ignore case by first converting both strings to lowercase before comparing.

Detecting ranges implicitly vs explicitly

IF a program should detect increasing ranges without gaps, a multi-branch if-else statement can be used without logical operators; the low-end of the range is implicitly known upon reaching an expression. Likewise, a decreasing range without gaps has implicitly-known high ends. In contrast, when gaps exist, the ranges low and high ends must both be explicitly detected using a logical operator.

Strings are equal if __?

If they have the same number of characters and the corresponding characters are identical.

Character functions

Including the cctype library via #include <cctype> provides access to several fucntions for working with characters. ctype stands for character type. The first c indicates the library is originally from the C language. Character functions ---------------------- isalpha(c) -- true if alphabetic isdigit(c) -- true if digit isspace(c) -- true if whitespace toupper(c) -- uppercase version tolower(c) -- lowercase version toupper/tolower does not modify its strings argument, but rather returns a value.

Operands

Operands are expressions or values on which an operator operates or works (often constants or variables but sub-expressions are also permitted).

Significance of multiple distinct if statements

Sometimes the programmer has multiple if statements in sequence, which looks similar to a multi-branch if-else statement but has a very different meaning. Each if statement is independent, and thus more than one branch can execute, in contrast to the multi-branch if-else arrangement.

at()

The notation stringVariable.at(x); access the character at index x of a string. A character in a string can be assigned. If userString is "abcde", then userString.at(3) = 'X'; yields "abcXe" (this is only for single characters, not strings. Assigning a character with a string is not allowed). The .at() function will generate an exception if the index is out of range for the strings size. An exception is a detected runtime error that commonly prints an error message and terminates the program.

When using a switch statement, does the order of cases matter?

The order of cases doesn't matter assuming break statements exist at the end of each case.

Comparing characters, strings, and floating-point types

The relational and equality operators work for integer, character, and floating-point built-in types. Comparing characters compares their ASCII numerical encoding. Floating-point types should not be compared using the equality operators, due to the imprecise representation of floating-point numbers. The operation can also be used for the string type. Strings are equal if they have the same number of characters and the corresponding characters are identical.

Floating-point numbers should not be compared with ==. True or false.

True. Floating-point numbers should not be compared using ==. Floating-point numbers should be compared for "close enough" rather than exact equality. Ex: If (x - y) < 0.0001, x and y are deemed equal. Because the difference may be negative, absolute value is used: fabs(x - y) < 0.0001. fabs() is a function in the math library. The difference threshold indicating that floating point numbers are equal is often called the epsilon. Epsilon's value depends on the programs expected values, but 0.0001 is common. The std::abs() function is overloaded to support floating-point and integer types. Good practice is to use the fabs() function to make the operation clearer.

An expression involving an equality operator evaluates to a Boolean variable

True. Remember, a Boolean is a type that has just two values: true or false.

Operator

We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. An operator is a symbol that tells the compiler to perform a specific mathematical or logical manipulation.

find() function

find(item) returns the index of first item occurence, else returns string::npos (a constant defined in the string library). Item may be a char, string, variable, string literal (or char array). You can add an extra argument to be a bit more specific with your search when indexing, for example: find(item, indx) //starts at index indx. userText = "Help me!"; userText.find('p'); //returns 3 userText.find('z'); //returns string::npos

insert()

insert(indx, subStr) Inserts string subStr starting at index indx. userText.insert(0, "Well ");

push_back()

push_back(c) appends character c to the end of the string. I.e., userText.push_back('c');

replace()

replace(indx, num, subStr) replaces characters at indicews indx to inx + num - 1 with a copy of subStr userText = "You have many gifts"; userText.replace(9, 4, "a plethora of "); //userText is now "You have a plethora of gifts"

str1 + str2

returns a new string that is a copy of str1 with str2 appended. If one of str1 or str2 is a string, the other can be a character or a string. I.e., myString = userText + '!';

append()

string s1 = "Hey"; string s2 = "!!!"; The function s1.append(s2) appends string s2 to string s1. So, s1.append(s2) makes s1 become "Hey!!!".

size()/length()

stringVariable.size()/length() returns stringVariables length.

substr() function

substr(index, length) returns substring starting at index and having length characters. Examples: //userText is "http://google.com" userText.substr(0, 7) //returns "http://" userText.substr(userText.size() - 4, 4) //returns ".com"

A boolean value is either ? or ?

true or false.

Precedence rules for arithmetic, logical, and relational operators.

(descending order in importance, the top is most important) -items with parentheses are evaluated first -! (logical NOT) is next -arithmetic operators (using their precedence) -relational operators -equality/inequality operators -logical AND (&&) -logical OR (||)


Conjuntos de estudio relacionados

Chem 175 - Chapter 16 (cont.) Exam 4

View Set

Chapter 3: Making Apps More Interactive Through Data Input

View Set

Unit 2, Assign. 2: Skills and Aptitude

View Set