Relational Operators Allow You To ________ Numbers.
Relational Operators
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. > < >= (greater than or equal to) <= (less than or equal to) == (equal to) != (not equal to). Relational operators have a left to right associativity.
What is Truth
Relationships are either true or false. In C++, true states are represented by the number 1 and false states are represented with the number 0. Therefore, if the expression in the if statement is equal to 0 it is false but if it is any other value, the expression is considered true by the if statement.
The if statement
The if statement can cause other statements to execute only under certain conditions. There is no semicolon after the expression but there is a semicolon after the statement. If you put a semicolon after the expression, the statement will always execute and the if statement is terminated.
If (expression)
statement;
Comparing floating point numbers
Avoid comparing floating point numbers - because of the way they are stored in memory, rounding errors might occur.
DONT CONFUSE == WITH =
If you say if (x=2), the statement will always execute because the expression x = 2 is always true unless you say x = 0 then it would never execute.
Expanding the if statement
The if statement can conditionally execute a block of statements enclosed in braces. You dont need braces if it just one statement, however. If you forget the braces, all statements except for the first will always be executed. The first statement will be conditionally executed.
If/else statement
The if/else statement will execute one group of statements if the expression is true, or another group of statements is false.
Nested if statements
To test more than one condition, an if statement can be nested inside another if statement (within the braces). It is important to indent each if statement so that you can understand what is going on,
The if/else if statement
Rather than have nested if else statements you can just use the else if statement.
Using the trailing else to catch errors
While having a trailing else is optional, you can use it to catch errors. For instance if none of the else if statements are true, you can have a cout message that there was an error.
The if/else if statement compared to a nested decision structure
Although you dont have to use the if/else if statement, it is reccomended to because the code can become very complicated if you use nested if statements.
Flags
A flag is a Boolean or integer variable that signals when a condition exists. You can have an if statement and set the flag equal to true or false if a certain condition is true. Then later in the program, you can have like a cout statement and say if(boolValue) cout << something;. You can also do it with integer variales if you make them 0 and nonzero.
Logical Operators
&& And, || OR, ! NOT (!true = false
Presedence and Assosiativity of Logical Operators
! first, && second, || third
Checking Numeric Ranges with Logical Operators
If (x >= 20 && x<=40)
expression
Menus
You can use nested if/else statements or the if/else if statment to create menu driven programs. A menu dirven program allows the user to determine the course of action by selecting it from a list of actions. Enter 1, 2 , 3 and then have else if statements to execute statements based on which number was entered.
Focus on Software Engineering: validating user input
If their input was invalid make a cout statement to inform them of this.
Comparing characters and strings
When two characters are compared, it is their ASCII values that are being compared. a has a lower ascii value than z but capital letters have a lower ascii value than lower case letters.
When two strings are compared, the one with the most characters or the
Switch Statement
switch() - within the parentheses you can have an expression whose value is any of the integer data types or a variable of any of the integer data types. There is no semicolon after this line. Then in braces you write case followed by a possible value of the swtich statement variable or expression followed by a colon. Then you have a statement with a semicolon followed by a break statement. If you dont include the break statement, all of the lines after the matching case will be executed. At the end you can have a default statement where you write default followed by a colon and then a statement.
Using switch in menu systems
The switch statemet is a natural mechanism for building menu systems.
More About Blocks and Variable Scope
Variables defined inside a set of braces have local scope or block scope. They may only use in the part of the program between their definition and the block's closing brace. You may define variables inside any block. (including if statements)
Variables with the same name
You can have variables with the same name so long as one as defined in an inner block and an outer block. This includes if statements. However, as long as the variable within the inner block is visible, the variable in the outer block will be hidden.