Intro to C Chapter 3

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

Branch

A branch is a program path taken only if an expression's value is true. Ex: A hotel may discount a price only for people over age 60.

Nested if-else statements

A branch's 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 AND, OR, and NOT (general)

A logical operator treats operands as being true or false, and evaluates to true or false. Logical operators include AND, OR, and NOT. Programming languages typically use various symbols for those operators, but below the words AND, OR, and NOT are used for introductory purposes.

Relational operators

A relational operator checks how one operand's value relates to another, like being greater than. Some operators like >= involve two characters. A programmer cannot arbitrarily combine the >, =, and < symbols; only the shown two-character sequences represent valid operators.

Switch Statements

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 statement's expression should be an integer or char. The expression should not be a string or a floating-point type. Each case must have a constant expression like 2 or 'q'; a case expression cannot be a variable.

Boolean

An equality operator checks whether two operands' values are the same (==) or different (!=). Note that equality is ==, not just =. An expression involving an equality operator evaluates to a Boolean value. A Boolean is a type that has just two values: true or false.

If statements

An if statement executes a group of statements if an expression is true. Braces surround the if branch's statements. Braces { }, sometimes redundantly called curly braces, represent a grouping, such as a grouping of statements. Note: { } are braces, [ ] are brackets.

If-else statement

An if-else statement executes one group of statements when an expression is true, and another group of statements when the expression is false.

If-else branch

An if-else structure has two branches: The first branch is taken if an expression is true, else the other branch is taken.

Boolean Data Type

Boolean refers to a quantity that has only two possible values, true or false.The language has the built-in data type bool for representing Boolean quantities. The programmer must add #include <stdbool.h> to use bool. A Boolean variable may be set using true or false keywords. Ex: isMale = true; assigns isMale with the Boolean value true. 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.

Epsilon

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, the 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 program's expected values, but 0.0001 is common.

Conditional expressions

If-else statements with the form shown below are so common that the language supports the shorthand notation shown. myVar = (condition) ? expr1 : expr2 A conditional expression has the form condition ? exprWhenTrue : exprWhenFalse. All three operands are expressions. If the condition evaluates to true, then exprWhenTrue is evaluated. If the condition evaluates to false, then exprWhenFalse is evaluated. The conditional expression evaluates to whichever of those two expressions was evaluated. For example, if x is 2, then the conditional expression (x == 2) ? 5 : 9 * x evaluates to 5. A conditional expression has three operands and thus the "?" and ":" together are sometimes referred to as a ternary operator. Good practice is to restrict usage of conditional expressions to an assignment statement, as in: y = (x == 2) ? 5 : 9 * x;. Common practice is to put parentheses around the first expression of the conditional expression, to enhance readability.

Null character

So that code can detect where a string ends, the compiler ends a string with a null character, written as '\0'. The string's char array must be large enough to include the null character; "Hi" requires a char array of size at least 3 for the 'H', 'i', and the null character.

Equality operator

The equality operator == evaluates to true if the left side and right side are equal. Ex: If numYears holds the value 10, then the expression numYears == 10 evaluates to true. Note that the equality operator is ==, not =.

Precedence rules

The order in which operators are evaluated in an expression are known as precedence rules. Arithmetic, logical, and relational operators are evaluated in the order shown below.


Conjuntos de estudio relacionados

Micro Chapter 2: Demand: Thinking Like a Buyer

View Set

Nursing Care of the Child With an Alteration in Bowel Elimination/Gastrointestinal Disorder

View Set

Organizational Behavior Chapter 7

View Set

2016 AP Lang Practice Exam: Multiple Choice

View Set

Pharmacology III Exam 2 Study Set

View Set