兰州理工大学研究生VC++课件3

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

COMPARING VALUES
• Unless you want to make decisions on a whim, you need a mechanism for comparing things. • This involves some new operators called relational operators.
• This is illustrated in Figure.
• A simple example of an if statement is:
if('A' == letter)
cout << "The first capital, alphabetically speaking.";
• You could extend this example to change the value of letter if it contains the value 'A':
• • • • • • • •
// Ex03-01.cpp // A nested if demonstration #include <iostream> using std::cin; using std::cout; using std::endl; int main() { char letter(0); // Store input in here cout << endl << "Enter a letter: "; // Prompt for the input cin >> letter; // then read a character if (letter >= 'A') // Test for 'A' or larger { if (letter <= 'Z') // Test for 'Z' or smaller { cout << endl << "You entered a capital letter." << endl; return 0; } } if (letter >= 'a') // Test for 'a' or larger { if (letter <= 'z') // Test for 'z' or smaller { cout << endl << "You entered a lowercase letter." << endl; return 0; } } cout << endl << "You did not enter a letter." << endl; return 0; }
• Suppose you have created integer variables i and j with the values 10 and –5, respectively.
• • • • • • The expressions, i>j i != j j > -8 i <= j + 15 all return the value true.
• }
• There is a standard library function to convert letters to uppercase, so you don’t normally need to program this yourself. • It has the name toupper() and appears in the ctype standard header file. • You will see more about standard library facilities when you getຫໍສະໝຸດ Baiduto look at how functions are written.
if(letter <= 'Z') // Test for 'Z' or smaller { cout << endl << "You entered a capital letter." << endl; letter += 'a' - 'A'; // Convert to lowercase return 0;
• Each operator compares the values of two operands and returns one of the two possible values of type bool: true if the comparison is true, or false if it is not. • You can see how this works by having a look at a few simple examples. • The operands can be variables, literals, or expressions.
• • • • Take a look at the following expressions: -1 < y true j < (10 - i) false 2.0*x >= (3 + y) true
The if Statement
• The basic if statement allows your program to execute a single statement — or a block of statements enclosed within braces — if a given condition expression evaluates to true, or to skip the statement or block of statements if the condition evaluates to false.
3.Decision and Loops
201405
WHAT YOU WILL LEARN IN THIS CHAPTER:
• How to compare data values • How to alter the sequence of program execution based on the result • How to apply logical operators and expressions • How to deal with multiple choice situations • How to write and use loops in your programs
• Let’s consider some slightly more complicated comparisons with variables defined by the statements: • int i = -10, j = 20; • double x = 1.5, y = -0.25E-10;
The Extended if Statement
• • • • • • • • // Ex3_02.cpp // Using the extended if #include <iostream> using std::cin; using std::cout; using std::endl; int main() {
long number(0L); // Store input here cout << endl << "Enter an integer number less than 2 billion: "; cin >> number; if(number % 2L) // Test remainder after division by 2 cout << endl // Here if remainder 1 << "Your number is odd." << endl; else cout << endl // Here if remainder 0 << "Your number is even." << endl; return 0;
• You could easily arrange to change uppercase to lowercase by adding just one extra statement to the if, checking for uppercase: • if(letter >= 'A') // Test for 'A' or larger
if('A' == letter) { cout << "The first capital, alphabetically speaking."; letter = 'a'; } The block of statements that is controlled by the if statement is delimited by the curly braces.
• Because all information in your computer is ultimately represented by numerical , comparing numerical values is the essence of all decision making.
• You have six operators for comparing two values:
Using Nested ifs
• The if statement that follows the input checks whether the character entered is 'A' or larger. • The ASCII codes for lowercase letters (97 to 122) are greater than those for uppercase letters (65 to 90).
• • • • • • • • •
Assume that you have defined the following variables: char first = 'A', last = 'Z'; Here are some examples of comparisons using these: first == 65 first < last 'E' <= first first != last All four expressions compare ASCII code values. The first expression returns true because first was initialized with 'A', which is the equivalent of decimal 65. • The second expression checks whether the value of first, which is 'A', is less than the value of last, which is 'Z'.
Nested if Statements
• The statement to be executed when the condition in an if statement is true can also be an if. • This arrangement is called a nested if. • The condition for the inner if is only tested if the condition for the outer if is true. • An if that is nested inside another can also contain a nested if. • You can generally continue nesting ifs one inside the other like this for as long as you still know what youare doing.
相关文档
最新文档