




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
C++ provides the variable type char for characters. We want to know some simple things about character data: How do we represent a literal ...
Typology: Summaries
1 / 44
This page cannot be seen from the preview
Don't miss anything!





































http://people.sc.fsu.edu/∼jburkardt/isc/week lecture 09.pdf .......... ISC3313: Introduction to Scientific Computing with C++ Summer Semester 2011 .......... John Burkardt Department of Scientific Computing Florida State University
Last Modified: 07 June 2011
Introduction The CONTINUE Statement Character Data Reading Characters The SWITCH Statement ETAION SHRDLU Lab Exercise #
Read: Sections 4.6, 4. Next Class: The Math Library, Functions and Parameters Assignment: Today: in-class lab exercise # Thursday: Programming Assignment #3 is due.
Today we will quickly go over the continue statement, which is related to the break statement we saw last time. Instead of completely terminating a loop, the continue statement jumps to the end of the current set of statements, allowing the next cycle of the loop to begin. Then we will introduce the topic of how C++ stores and handles single characters. We will look at an example of how to read characters, one by one, from a file.
Introduction The CONTINUE Statement Character Data Reading Characters The SWITCH Statement ETAION SHRDLU Lab Exercise #
The continue statement is used inside a looping statement, such as for, while or do...while.
while ( condition ) { some statements; continue; more statements; }
Typically, it occurs somewhere in the middle of the statements being repeated. The effect of the continue statement is to skip over the statements which follow it in the loop, but to start the next cycle of the loop, rather than to terminate the loop (which is what the break statement does.)
The continue statement is another convenience. You can always do the same thing by using an if statement in the right way, but sometimes in big programs the continue statement makes it easier to see what is going on. Let us consider a computation in which, for any integer n between 1 and 20, we want to compute the product: product = (n − 1) ∗ (n − 2) ∗ ... ∗ (n − 20). As we have described it, product will be zero, because n is actually equal to 1, or 2 or one of the other integers. However, let’s suppose we actually want to compute this product by skipping the step where the factor is 0.
We can use a for statement to do the loop, and a continue statement to jump to the next step in a for statement just as it does for a while:
product = 1; for ( i = 1; i <= 20; i++ ) { if ( i == n ) { continue; } product = product * ( n - i ); }
If n is 7, we correctly skip the zero factor: product = 6 * 5 * 4 * 3 * 2 * 1 * (-1) * (-2) ... * (-13).
If you’re trying to alphabetize a list of names, you don’t compare numbers, but rather letters. C++ prefers to use the word characters rather than letters, because we want to think about messages that include not just ’A’, ’B’, ’C’, but also ’&’ and ’7’ and ’[’ and so on. C++ provides the variable type char for characters. We want to know some simple things about character data: How do we represent a literal character? Can we uppercase a character? Compute the next character? How do we read and write single characters? What about special characters? How is the computer storing characters?
The literal value of a character can be given using single quotes.
char capA = ’A’, lowA = ’a’, capB = ’B’; char c;
c = ’a’; if ( c == capA ) cout << c << " == " << capA << "\n"; if ( c == lowA ) cout << c << " == " << lowA << "\n"; if ( c == capB ) cout << c << " == " << lowB << "\n";
The single quote can be confusing, since text strings use double quotes. And so ’w’ is a character, but ”w” is a (very short) string. We’re not ready to worry about strings right now!
It’s frustrating to have to worry about whether a character is uppercase or lowercase. One thing you can do is use the functions toupper() or tolower() from . No matter whether the character c is lowercase or uppercase, for instance, toupper(c) returns an uppercase version of it.
using namespace std; int main () { char answer; cout << "Do you want help? (Y/N): "; cin >> answer; if ( toupper ( answer ) == ’Y’ ) { cout << "Here is some helpful information...\n"; ...and here we print stuff out. } return 0; }
If we want to alphabetize things, then we can use the comparisons < and <= and > and >= and == and !=. char capa = ’A’, capt = ’T’, lowb = ’b’, lowt = ’t’; cout << "Is A < b?: " << ( capa < lowb ) << "\n"; cout << "Is T < b?: " << ( capt < lowb ) << "\n"; cout << "Is t < b?: " << ( lowt < lowb ) << "\n";
Remember, ( capa < lowb ) is a logical variable, so you can print it, and it will come out as “0” for false, or “1” for true. If you insert << boolalpha before the printout, logical variables will print out as “true” or “false” instead. cout << "Is A < b?: " << boolalpha << ( capa < lowb ) << "\n";
All capital letters come before any lowercase letters, so: ’A’ < ’B’ < ’C’ < ... < ’Z’ < ’a’ < ’b’ < ’c’ < ... < ’z’.
average.cpp: The book likes examples that involve letter grades: char grade; float average; if ( 90 <= average ) { grade = ’A’; } else if ( 80 <= average ) { grade = ’B’; } else if ( 70 <= average ) { grade = ’C’; } else if ( 60 <= average ) { grade = ’D’; } else { grade = ’F’; }cout << "Your grade is " << grade << "\n";
Why can’t we assign a grade of B+ this way? What if we really want plus and minus grades?
Introduction The CONTINUE Statement Character Data Reading Characters The SWITCH Statement ETAION SHRDLU Lab Exercise #