760 likes | 931 Views
TK 1914 : C++ Programming. Control Structures I (Selection). OBJECTIVES. In this chapter you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions
E N D
TK 1914 : C++ Programming Control Structures I (Selection)
OBJECTIVES In this chapter you will: • Learn about control structures • Examine relational and logical operators • Explore how to form and evaluate logical (Boolean) expressions • Discover how to use the selection control structures if, if...else, and switch in a program FTSM :: TK1914, 20112012
INTRODUCTION • A computer can process: • In sequence • Selectively (branch) - making a choice • Repetitively (iteratively) - looping • Some statements are executed only if certain conditions are met • A condition is represented by a logical (Boolean) expression that can be true or false • A condition is met if it evaluates to true FTSM :: TK1914, 20112012
We have seen sequencing in C++. • How do we translate selection and repetition structures into C++? FTSM :: TK1914, 20112012
SELECTION STRUCTURES IN C++ • Selection structures can be translated into C++ using if statements. • Four forms of selection structures will be looked at: • One-Way • Two-Way • Nested • Switch FTSM :: TK1914, 20112012
if STATEMENT : ONE-WAY SELECTION • The syntax of one-way selection is: if(expression) statement • statement is executed only if the value of expression is true • expression is usually a logical expression. • if is a reserved word • statement must be a single C++ statement. FTSM :: TK1914, 20112012
if STATEMENT : ONE-WAY SELECTION FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.1 • Refer prog03.1.cpp. • Examine the source code. Focus on the if statements. • What do you think the output would be for the following input? • Price per unit: RM25 Quantity: 4 • Price per unit: RM5.50 Quantity: 5 • Run the program with the above input. Is the output generated the same as expected? • Draw a flowchart representing the algorithm implemented in the program. FTSM :: TK1914, 20112012
if-else STATEMENT: TWO-WAY SELECTION • Two-way selection takes the form: if (expression) statement1 else statement2 • If expression is true, statement1 is executed. Otherwise, statement2 is executed • else is a reserved word • statement1 and statement2 must both be single C++ statements. FTSM :: TK1914, 20112012
if-else STATEMENT: TWO-WAY SELECTION FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.2 • Refer prog03.2.cpp. • Examine the source code. Focus on the if statements. • What do you think the output would be for the following input? • Number of adults: 2 Number of children: 2 Ticket type: First class • Number of adults: 2 Number of children: 2 Ticket type: Economy • Run the program with the above input. Is the output generated the same as expected? • Draw a flowchart representing the algorithm implemented in the program. FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.2 • Observe that prog03.2.cpp includes the following declarations: constfloat PRICE_FIRST_CLASS = 30.00; constfloat PRICE_ECONOMY = 5.00; The identifiers PRICE_FIRST_CLASS and PRICE_ECONOMY are called constants. Their values cannot be changed during program execution. FTSM :: TK1914, 20112012
COMPOUND STATEMENTS • Consider the following structure: input num true num = 0? print warning false num ← 10 result ← sum / num FTSM :: TK1914, 20112012
COMPOUND STATEMENTS • Note that there are two statements to be executed if num is equal to 0. • Is it correct to translate the above to C++ as follows? cin >> num; if (num == 0) cout << "Invalid value!"; num = 10; result = sum / num; • Answer: No. According to C++ syntax, the statement num = 10; is actually not part of the if statement. FTSM :: TK1914, 20112012
COMPOUND STATEMENTS • In this case, we need to use a compound statement to group those statements into a single statement. • A compound statement is simply a set of single statements enclosed within curly brackets. { statement_1; … statement_n; } • Remember that a compound statement is a single C++ statement. FTSM :: TK1914, 20112012
compound statement • Example: cin >> num; if (num == 0) { cout << "Invalid value!"; num = 10; } result = sum / num; FTSM :: TK1914, 20112012
NESTED IF • Consider the syntax of a one-way if statement: if(expression) statement • You might ask: Could statement be another if statement? Example: if(weight > 100) if (weight <= 1000) price = 0.20 * weight; What does it mean? FTSM :: TK1914, 20112012
NESTED IF • Example: FTSM :: TK1914, 20112012
NESTED IF • Can also be written as follows (remember that the compiler ignores unnecessary whitespace characters): FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.3 • Examine the following code: How is this code different from that in the next slide? FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.4 • The following bands are defined: • Band 1: 75 and above • Band 2: 51 - 74 • Band 3: 50 and below • Refer prog03.3a.cpp. • Examine the source code. Focus on the if statements. • What do you think the output would be for the following input? • 80 • 60 • 40 • Run the program with the above input. Is the output generated as expected? FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.4 (CONT) • Refer prog03.3b.cpp. • Examine the source code. How is it different from the previous program? • What do you think the output would be for the following input? • 80 • 60 • 40 • Run the program with the above input. Is the output generated as expected? • Draw a flowchart representing the algorithm implemented in the program. FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.4 (CONT) • Refer prog03.3c.cpp. • Examine the source code. How is it different from the first program? • What do you think the output would be for the following input? • 80 • 60 • 40 • Run the program with the above input. Compare the output with that of the previous program. • Draw a flowchart representing the algorithm implemented in the program. • C++ always associates an else with the closest unpaired if. FTSM :: TK1914, 20112012
First, evaluate the switch expression Execute the statements inside the switch structure starting from the case which matches the value of the expression If there is no match, start from the default case switch STRUCTURE • switchstructure: alternate to if-else. • Example: switch(code) { case 1: cout << "one"; case 2: cout << "two"; default: cout << "Error"; } FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.5 • Refer prog03.4.cpp. • Examine the source code. Focus on the switch statement. • Observe that there are return statements in the switch statement. • What do you think will happen if they are executed? • Why do you think they are there for? • What do you think the output would be for the following input? • 2 adults, 2 children, normal first-class ticket • 2 adults, 2 children, economy ticket FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.5 (CONT) • Refer prog03.4.cpp. • Run the program with the input data given above. Is the output as expected? Explain. • Draw a flowchart representing the algorithm implemented in the program. FTSM :: TK1914, 20112012
switch STRUCTURE • Expression value can be only integral • Its value determines which statement is selected for execution • A particular case value should appear only once • One or more statements may follow a case label • Braces are not needed to turn multiple statements into a single compound statement. • The default case is optional in a switch statement. FTSM :: TK1914, 20112012
THE break STATEMENT • The break statement may be used to jump out of a switch statement. FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.6 • Refer prog03.5.cpp. • Examine the source code. Focus on the use of break statements in the switch statement. • What do you think the output would be for the following input? • 2 adults, 2 children, normal first-class ticket • 2 adults, 2 children, economy ticket • Run the program with the input data given above. Is the output as expected? • Draw a flowchart representing the algorithm implemented in the program. FTSM :: TK1914, 20112012
switch Statement Rules • When value of the expression is matched against a case value, • Statements execute until break statement is found or the end of switch structure is reached • If value of the expression does not match any of the case values • Statements following the default label execute If no default label, and if no match, the entire switch statement is skipped • A break statement causes an immediate exit from the switch structure FTSM :: TK1914, 20112012
RELATIONAL OPERATOR • Relational operators: • Allow comparisons • Require two operands (binary) • Return 1 if expression is true, 0 otherwise • Comparing values of different data types may produce unpredictable results • For example, 8 < '5' should not be done • Any nonzero value is treated as true FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.7 • Refer prog03.6a.cpp • Examine the source code. • What do you think the output would be for the following input? • 47 • 67 • Compile the program. Are there any syntax errors? • Run the program. Is the output as expected? Explain. FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.7 (CONT.) • Refer prog03.6b.cpp • Examine the source code. Compare it with the previous program. • Run the program with the same input data as for the previous program. Is the output as expected? FTSM :: TK1914, 20112012
COMPARING string TYPES • Relational operators can be applied to strings • Strings are compared character by character, starting with the first character • Comparison continues until either a mismatch is found or all characters are found equal • If two strings of different lengths are compared and the comparison is equal to the last character of the shorter string • The shorter string is less than the larger string FTSM :: TK1914, 20112012
string COMPARISON EXAMPLE • Suppose we have the following declarations: string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill"; FTSM :: TK1914, 20112012
CLASS ACTIVITY 3.8 • Refer prog03.7.cpp • Examine the source code. What do you think the program does? • What do you think the output would be for the following input? • Jamal Omar Selamat Ahmad Ali Wali • Run the program. Is the output what you expected? FTSM :: TK1914, 20112012
LOGICAL (BOOLEAN) OPERATORS • Logical (Boolean) operators enable you to combine logical expressions • Three logical (Boolean) operators: ! (not) && (and) ||(or) • Logical operators take logical values as operands and yield logical values as results • ! is unary whereas && and || are binary operators • Putting ! in front of a logical expression reverses its value FTSM :: TK1914, 20112012