Characters and the SWITCH Statement, Summaries of Computer Programming

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

2022/2023

Uploaded on 03/01/2023

avni
avni 🇺🇸

4.7

(3)

229 documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Characters and the SWITCH Statement
http://people.sc.fsu.edu/jburkardt/isc/week05
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
1 / 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download Characters and the SWITCH Statement and more Summaries Computer Programming in PDF only on Docsity!

Characters and the SWITCH Statement

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

Characters and the SWITCH Statement

Introduction The CONTINUE Statement Character Data Reading Characters The SWITCH Statement ETAION SHRDLU Lab Exercise #

INTRO: Schedule

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.

INTRO: Today’s Topics

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.

Characters and the SWITCH Statement

Introduction The CONTINUE Statement Character Data Reading Characters The SWITCH Statement ETAION SHRDLU Lab Exercise #

CONTINUE: Skip to Next Loop Cycle

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.)

CONTINUE: Skip to Next Loop Cycle

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.

CONTINUE: Skip to Next Loop Cycle

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).

CHAR: a Single Character Data Type

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?

CHAR: Literal Values

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!

CHAR: Capitalizing a CHAR

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.

include

include

include <-- Need this at beginning!

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; }

CHAR: How Are CHAR’s Ordered?

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’.

CHAR: Using a CHAR Variable

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?

Characters and the SWITCH Statement

Introduction The CONTINUE Statement Character Data Reading Characters The SWITCH Statement ETAION SHRDLU Lab Exercise #