Question
Using C++ (please don't use somthing advanced or somthing new, I will post below the explanation from the class. we should try to not use
Using C++
(please don't use somthing advanced or somthing new, I will post below the explanation from the class. we should try to not use somthing that we did not learn yet.)
(the Exercise)
----------------------------------------------------------------------------------------------------
Exercise of If-Then-Else Statment:
first
Get a copy of the convert.cpp file by:
----------------------------------------
convert.cpp file
// Program Convert converts a temperature in Fahrenheit to // Celsius or a temperature in Celsius to Fahrenheit // depending on whether the user enters an F or a C.. #includeusing namespace std; int main () { char letter; // Place to store input letter int tempIn; // Temperature to be converted int tempOut; // Converted temperature cout <<"Input Menu" << endl << endl; cout <<"F: Convert from Fahrenheit to Celsius" << endl; cout <<"C: Convert from Celsius to Fahrenheit" << endl; cout <<"Type a C or an F, then press return." << endl << endl; cout << "Enter your option: "; cin >> letter; cout <<"Type an integer number: "; cin >> tempIn; if (letter == 'C') tempOut = (9 * tempIn / 5) + 32; else tempOut = 5 * (tempIn - 32) / 9; cout << endl << endl; cout << "Temperature to convert: " << tempIn << endl; cout << "Converted temperature: " << tempOut << endl; return 0; }
----------------------------------------------------------------------------------------------------------------------
This program demonstrates how to use If-Then-Else statement.
Compile and run the program. Use input values: (C 100), (F 32), (c 0), (d 0) and (F 0) to test the program.
Explain to youself why the last three sets of the input values yield the same output?
Fix the program to treat (C 100) and (c 100) the same, and treat (F 32) and (f 32) the same;
If the input letter is not C, c, F , or f, output "wrong letter". Do not ask for an integer input.
Use testing values: (C 100), (F 32), (c 0), (F 0) and (d) to run your program and show the results to your lab instructor.
end of question
-----------------------------------------------------------------------------------------------------------
addtion explantion from class note :
f Statement
The If statement allows the programmer to change the logical order of a program; that is, make the order in which the statements are executed differ from the order in which they are listed in the program.
If-Then Statement
The If-Then statement uses a Boolean expression to determine whether to execute a statement or to skip it. Here is the syntax template:
if (Expression) Statement
The expression in parentheses can be of any simple data type. Almost without exception, this will be a logical (Boolean) expression; if not, its value is implicitly coerced to type bool(nonzero value means true, zero value means false).
Now let's look at the following statement:
int number, sum; sum = 10; cout << "Please enter an integer value: " << endl; cin >> number; if (number < 0) number = 0; sum = sum + number; cout << "The sum is " << sum << endl;
The expression (number < 0) is evaluated. If the result is true, the statement number = 0; is executed. If the result is false, the statement is skipped. In either case, the next statement to be executed is sum = sum + number;.
If-Then-Else Statement
If-Then-Else statement uses a Boolean expression to determine which one of the two statements to execute. Here is the syntax template:
if (Expression) Statement_A else Statement_B
The expression in parentheses will be evaluated with the result of true or false. Here is an example:
cout << "You are "; if (age >= 65) cout << "a senior "; else cout << "not a senior "; cout << "citizen." << endl;
The characters "You are " are sent to the output stream. The expression age >= 65 is evaluated. If the result is true, the characters "a senior " are sent to the output stream. If the result is false, the characters "not a senior " is sent to the output stream. In either case, the next statement to be executed sends the the characters "citizen." to the output stream.
There is one thing to point out here: any statement in an IF or ELSE statement could be a block or a compound statement. If so, they must be enclosed in a pair of braces. Here is an example which also shows a compound expresssion:
if ( (age > 65) && (gender == 'f') ) { cout << "Quilting group meets:"; cout << "Thursday afternoons at 4:00 p.m."; }
Nested If Statement
An If-Then statemenet uses Boolean expression to determine whether to execute or skip a statemenet. An If-Then-Else statement uses a Boolean expression to determine which one of the two statements to execute. The statements to be executed or skipped could be simple statements or compound statements (blocks). They also can be an If Statement. An If statement within another If statement is called a nested If statement.
A look-ahead: You will soon be learning about the C++ SWITCH statement. For very complex if/else constructs, it is preferable to use the Switch instead.
The following example is a nested If statement.
cout << "You are "; if (age >= 65) cout << "a senior." << endl; else if (age >= 19) cout << "an adult." << endl; else if (age >= 13) cout << "a teenager." << endl; else cout << "a child." << endl; cout << "You are a great person." << endl;
The characters "You are " are sent to the output stream. The expression age >= 65 is evaluated. If the result is true, the characters "a senior." are sent to the output stream. If the result is false, the expression age >= 19 is evaluated. If the result is true,the characters "an adult." are sent to the output stream. If the result is false, the expression age >= 13 is evaluated. If the result is true,the characters "a teeneager." are sent to the output stream. If the result is false, the expression "a child. is sent to the output stream. In any case above, the next statement to be executed sends the the characters "You are a great person." to the output stream. Notice: once age has a value, only one statement is selected to be executed. If we add braces to the program segment, it would be easy to read. Let's look at it now:
cout << "You are "; if (age >= 65) { cout << "a senior." << endl; cout << "*****" << endl; } else if (age >= 19) { cout << "an adult." << endl; cout << "*****" << endl; } else if (age >= 13) { cout << "a teenager." << endl; cout << "*****" << endl; } else { cout << "a child." << endl; cout << "*****" << endl; } cout << "You are a great person." << endl;
Another Example
#include using namespace std; int main() { char grade; cout << "Please enter a letter grade (A, B, C, D, or F): " << endl; cin >> grade; if (grade == 'A') cout << "Great work. " << endl; else if (grade == 'B') cout << "Good work. " << endl; else if (grade == 'C') cout << "Passing work. " << endl; else if (grade == 'D' || grade == 'F') { cout << "Unsatisfictory work. " << endl; cout << "See your instructor." << endl; } else cout << grade << " is not a legal grade." << endl; cout << endl; return 0; } // end main
Boolean Data Type --- bool
In C++, data type bool is used to represent Boolean data. Each bool constant or variable contains one of two values: true or false. true and false are two C++ constants. true has the value 1. false has the value 0.
If a testing expression is not of bool type, it is coerced to bool type automaticaly when it is evaluated. A nonzero value is coerced to true and a zero value is coerced to false.
Boolean Expression
A Boolean expression can be a simple Boolean variable or constant, or it can be a more complex expression involving one or more relational and logical operators. Relational operators take two operands and test the relationship between them. The following table shows the relational operators and the corresponding C++ symbols.
Relational Operators C++ Symbol Description
== Equal to. != Not equal to. > Greater than. < Less than. >= Greater than or equal to. <= Less than or equal to. For example, The Boolean expression
number1 < number2
is evaluated to true if the value stored in number1 is less than the value stored in number2, and evaluated to false otherwise.
When a relational operator is applied between variables of type char, the assertion is in terms of where the two operands fall in the collating sequence of a particular character set. For example,
character1 < character2
is evaluated to true if the value stored in character1 comes before the character stored in character2 in the collating sequence of the machine on which the expression is being evaluated. You can think of collating sequence as being in alphabetic order to help you understand it. ASCII character set is widely used.
A simple Boolean expression is either a Boolean variable or a Boolean constant or an expression involving the relational operators that evaluates to either true or false. These simple Boolean expressions can be combined using logical operations defined on Boolean values. There are three Boolean operators: AND, OR and NOT. Here is a table showing how they are used in C++.
Logical Operator C++ Symbol Meaning
&& AND is a binary Boolean operator. If both operands are true, the result is true. Otherwise, the result is false. || OR is a binary Boolean operator. If at least one of the operands is true, the result is true. Otherwise, if both operands are false, the result is false. ! NOT is a unary Boolean operator. NOT changes value of its operand. If the operand is true, the result is false. If the operand is false, the result is true.
Precedence of Operators
If relational operators and Boolean operators are combined in the same expression in C++, the Boolean operator NOT (!) has the highest precedence, the relational operators have the next highest precedence, and the Boolean operators AND (&&) and OR (||) have the lowest. Expressions in parentheses are always evaluated first.
The following table summarizes the precedence of all the C++ operators we have seen so far.
Highest Precedence | ( ) | | ++x --x | | ! Unary + Unary - | | * / % | | + - | | << >> | | < <= > >= | | == != | | && | | || | | = | | x++ x-- | V Lowest Precedence
Operators in the same line in the table have the same precedence. If an expression contains several operators with the same precedence, most of the operators group from left to right. Some operators have different precedence based on where variables are in relation to them, for these an x represents the variable.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started