< previous page page_246 next page >

Page 246
12. The nested If structure below has five possible branches depending on the values read into char variables ch1, ch2, and ch3. To test the structure, you need five sets of data, each set using a different branch. Create the five test data sets.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
cin >> ch1 >> ch2 >> ch3;
if (ch1 == ch2)
    if (ch2 == ch3)
        cout << All initials are the same. << endl;
    else
        cout << First two are the same. << endl;
else if (ch2 == ch3)
    cout << Last two are the same. << endl;
else if (ch1 == ch3)
    cout << First and last are the same. << endl;
else
    cout << All initials are different. << endl;
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
a. Test data set 1: ch1 =____ ch2 =_____ ch3 =_____
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Test data set 2: ch1 =_____ ch2 =_____ ch3 =_____
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
c. Test data set 3: ch1 =_____ ch2 =_____ ch3 =_____
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
d. Test data set 4: ch1 =_____ ch2 =_____ ch3 =_____
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
e. Test data set 5: ch1 =_____ ch2 =_____ ch3 =_____
13. If x and y are Boolean variables, do the following two expressions test the same condition?
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
x != y
(x | | y) && ! (x && y)
14. The following If condition is made up of three relational expressions:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
if (i >= 10 && i <= 20 && i != 16)
    j =4;
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
If i contains the value 25 when this If statement is executed, which relational expression(s) will the computer evaluate? (Remember that C++ uses short-circuit evaluation.)
Programming Warm-up Exercises
1. Declare eligible to be a Boolean variable, and assign it the value TRUE.
2. Write a statement that sets the Boolean variable available to TRUE if numberOrdered is less than or equal to numberOnHand minus numberReserved.
3. Write a statement containing a logical expression that assigns TRUE to the Boolean variable isCandidate if satScore is greater than or equal to 1100, gpa is not less than 2.5, and age is greater than 15. Otherwise, isCandidate should be FALSE.
4. Given the declarations
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Boolean leftPage;
int     pageNumber;

 
< previous page page_246 next page >