Chapter 3 Decisions:

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

strlen

(s1) returns s1's length

strcat

(s1, s2) appends string s2 to string s1.

If

A branch taken only if an expression is true.

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.

Uses of Boolean data types

A programmer can use a Boolean variable to simplify a complex expression.

index

A string is a sequence of characters in memory. Each string character has a position number called starting with 0 (not 1).

Multi-branch if-else statements

An If-else statement can be extended to have three (or more) branches. Each branch's expression is checked in sequence. As soon as one branch's expression is found to be true, that branch's statement execute (and no subsequent branch is considered). If no expression is true, the else branch executes.

If-elseif-else branches

Commonly a programmer wishes to take one of multiple (three or more) branches. An if-else can be extended 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 true, execution will reach the else branch, which then executes.

Range detection using if-elseif-else

Each expression only needs to indicate the upper range part; if execution reaches an expression, the lower range part is implicit from the previous expressions being false.

Multiple distinct if statements

Each if-statement is independent, and thus more than one branch can execute, in contrast to the multi-branch if-else arrangement.

default case

If no case matches, then the default case statements are executed.

Conditional expressions

If-else statements with the form shown below are so common that the language supports the shorthand notation shown. A conditional expression has three operands

Detecting ranges implicitly vs. explicitly

Likewise, a decreasing range without gaps has implicitly-known high-ends. In contrast, when gaps exist, the range's low and high ends must both be explicitly detected, using a logical operator.

bitwise operators

Logical AND is && and not just &, and logical OR is || and not just |. & and | represent

Detecting ranges with if-else statements

Programmers commonly use the sequential nature of the multi-branch if-else arrangement to detect ranges of numbers.

Null character

So that code can detect where a string ends, the compiler ends a string with a

String comparison: Relational

Strings are sometimes compared relationally (less than, greater than), as when sorting words alphabetically. A comparison begins at index 0 and compares each character until the evaluation results in false, or the end of a string is reached.

epsilon

The difference threshold indicating that floating-point numbers are equal Epsilon's value depends on the program's expected values, but 0.0001 is common.

String comparison: Equality

Two strings are commonly compared for equality. Equal strings have the same number of characters, and each corresponding character is identical.

Common error

When a branch has a single statement, the braces are optional, but good practice always uses the braces.

break

a case will cause the statements within the next case to be executed. Such "falling through" to the next case can be useful when multiple cases, such as cases 0, 1, and 2, should execute the same statements.

branch

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.

strcmp

function returns 0 if the strings are equal, and some non-zero value otherwise.

if-else

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

Boolean

is a type that has just two values: true or false.

Boolean

refers to a quantity that has only two possible values, true or false.

Accessing string characters

The notation some String[x] accesses the character at index x of a string.

Precedence rules

The order in which operators are evaluated in an expression

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, as discussed in a later section.

Short circuit evaluation

skips evaluating later operands if the result of the logical operator can already be determined.

Braces { }

sometimes redundantly called curly braces, represent a grouping, such as a grouping of statements. Note: { } are braces, [ ] are brackets. Good practice is to indent a branch's statements, using a consistent number of spaces. This material indents 3 spaces.

switch

statement can more clearly represent multi-branch behavior involving a variable being compared to constant values.

if

statement executes a group of statements if an expression is true. Braces surround the if branch's statements.

if-else

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

ternary operator

the "?" and ":" together are sometimes referred to as

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.

case

whose constant expression matches the value of the switch expression, executes that case's statements, and then jumps to the end.

relational operator

checks how one operand's value relates to another, like being greater than.

equality operator

checks whether two operands' values are the same (==) or different (!=). Note that equality is ==, not just =.

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.


Ensembles d'études connexes

PEDs Chapt 5 Growth and Development of the Preschooler

View Set

American Strengths and Weaknesses

View Set