Tag Archives: if statement

C++ Conditional Statements (if else)

Relational Operators

a. Use operators to make comparisons

i. > greater than
ii. < less than
iii. >= greater than or equal to
iv. <= less than or equal to
v. == equal to
vi. != not equal to

b. Boolean expressions are true or false

i. 12 > 5  true
ii. If x=10, then x==10 is true

c. Expressions can be assigned to a variable

i. true typecasted to an int is 1, false typecasted to an int is 0
ii. = denotes assignment, == denotes comparison

The if Statement

a. Used to make decisions on what code to execute

i. if the condition is false, skip the block of code
ii. Multiple statements can be contained within the {} of the if statement

b. Syntax: if (expression) statement;

i. Do not place a semicolon after (expression)
ii. Place statement; on a separate line after expression, indented

c. To evaluate:

i. If expression returns true, execute the statement
ii. Be careful testing floats and doubles for equality
iii. If expression returns false, skip the statement
iv. In C++ 0 is false, any other value is true

d. Curly braces { } create a block of code: Use these curly braces to execute more than one statement as part of an if statement:

Syntax: if (score > 90) { grade = ‘A’; cout << “Good Job!\n”; }

The if/else statement

a. Provides two possible paths of execution
b. Performs the code inside the if statement if the expression is true, otherwise performs the code inside the else block
c. Syntax:

if (expression) statement 1;
     // or block; execute this if expression is true
else statement 2; // or block; execute this if expression is false

Nested if Statements

a. An if statement that is nested inside another if statement
b. Can be used to test more than one condition
c. Syntax:

 

Nested If Statement

 

The if/else if Statement

a. Tests a series of conditions until one is found to be true

b. Often simpler than using nested if/else statements
c. Can be used to model thought processes

i. If it is winter, wear a coat
ii. Else, if it is fall, wear a jacket
iii. Else, wear sunglasses

d. Syntax:

if (expression 1) statement 1; // or block else
if (expression 2) statement 2; // or block // other
else if else if (expression) statement; // or block

Flags

a. Variables that signal a condition
b. Usually implemented as a bool variable
c. Can also be an integer
i. Like if statements, 0 is considered false and any other nonzero value is considered true
d. Must be assigned an initial value before it can be used

Logical Operators

a. Used to create relational expressions from other relational expressions
b. Operators:

i. && / AND – new relational expression is true if both expressions are true
ii. || / OR – new relational expression is true if either expression is true
iii. ! / NOT – reverses the value of an expression – true becomes false, and false becomes true

c. Syntax (int x = 12, y = 5, z = -4):

i. (x > y) && (y > z) –> true
ii. (x <= z) || (y != z) –> true
iii. !(x >= z) –> false

d. in C++ ! has highest precedence, followed by &&, then || (order of operations)

e. If the value of an expression can be determined by evaluating just the sub-expression on left side of a logical operator, then the sub-expression on the right side will not be evaluated (short circuit evaluation)

i. E.g. int x, y; x = 0; (x && y)  false Will not give an error because it did not check the value of y, x is already false

Examples

Use to test if value falls inside a range: if (grade >= 0 && grade <= 100) cout << “Valid grade”;
if (grade <= 0 || grade >= 100) cout << “Invalid grade”;

Comparing Characters and Strings

a. Characters compared using their corresponding ASCII values
i. ‘A’ < ‘B’ is true because the ASCII value of A (65) is less than the ASCII value of B (66)
ii. Lowercase letters have higher ASCII codes than uppercase letters ‘a’ > ‘Z’ is true
b. Strings are also compared using their ASCII values
i. The characters in each string must match before they are equal (case-sensitive)
ii. “Mary” < “Mary Jane” is true “Mary” <= “Mark” is false

The Conditional Operator

a. Shorthand version of if/else statement
b. Syntax: expr1 ? expr2 : expr3; If expr1 is true, execute expr2, else execute expr3

The switch Statement

a. Shorthand version of if/else if statements with the same condition
b. Syntax:

switch (expression) //integer {

case exp1: statement1;
break;
case exp2: statement2; break;
case exp3: statement3; break; …
case expn: statementn; break;
default: statementn+1;

}