How to Use C++ Switch Statements: Examples and Tips for Beginners

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Switch
Click For Summary

Discussion Overview

The discussion revolves around the implementation of switch statements in C++, specifically focusing on how to handle character inputs for both lowercase and uppercase letters. Participants seek clarification on syntax and structure, particularly regarding the use of fall-through and handling multiple cases.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant requests a switch statement that checks for 'a' or 'A' and 'b' or 'B', and prints corresponding outputs.
  • Another participant expresses confusion about whether to use case statements or if statements.
  • A suggestion is made to use a switch statement structure, including fall-through for 'a' and 'A'.
  • Some participants seek clarification on how to handle both lowercase and uppercase letters within the switch statement.
  • There is a request for further explanation on what to include in the code for handling 'a' or 'A'.
  • A later reply clarifies that the placeholder "" should be replaced with the code that prints "Alpha".

Areas of Agreement / Disagreement

Participants generally agree on the use of switch statements but express differing levels of understanding regarding the implementation details, particularly with case handling for different letter cases. The discussion remains unresolved as participants continue to seek clarification.

Contextual Notes

Some participants may be missing foundational knowledge about switch statements and fall-through behavior, which could affect their understanding of the task.

ineedhelpnow
Messages
649
Reaction score
0
Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If 'b' or 'B', print "Beta". For any other character, print "Unknown". Use fall-through as appropriate. End with newline.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   char origLetter = '?';

   origLetter = 'a';
   <STUDENT CODE>

   return 0;
}

Just do the <STUDENT CODE> part.
i don't get it. do i do case or if?
 
Technology news on Phys.org
ineedhelpnow said:
Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If 'b' or 'B', print "Beta". For any other character, print "Unknown". Use fall-through as appropriate. End with newline.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   char origLetter = '?';

   origLetter = 'a';
   <STUDENT CODE>

   return 0;
}

Just do the <STUDENT CODE> part.
i don't get it. do i do case or if?

Do "switch".

That is:
Code:
switch (<expression>)
{
    case <constant>: // Fall through
    case <constant>: <statements>; break;
    case <constant>: <statements>; break;
    default: <statements>; 
}

Oh. And don't spell it like [m]defualt[/m]. It will take you ages to figure that out.
Especially since it is completely legal, syntactically, as a label. :eek:
 
could you be a little more specific? i don't really understand how to do it since i have like lowercase and uppercase alpha and beta
 
ineedhelpnow said:
could you be a little more specific? i don't really understand how to do it since i have like lowercase and uppercase alpha and beta

So it's like:
Code:
    switch (origLetter)
    {
        case 'a': // Fall through
        case 'A': <handle 'A' or 'a' statements>; break;
        ...
    }
 
ok so could you explain the <handle 'a' or 'A'...> part. I am not familiar with it
 
ineedhelpnow said:
could you explain the <handle 'a' or 'A'...> part. I am not familiar with it
"<handle 'a' or 'A'...>" means that you should replace it with the code according to your instructions, namely: If 'a' or 'A', print "Alpha". That is, you should replace "<handle 'a' or 'A'...>" in ILS's code with the line that prints the string "Alpha".

For more about the [m]switch[/m] statement in C and C++, see here and here or, better, read your textbook or lecture notes.
 

Similar threads

Replies
89
Views
6K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
73
Views
6K
  • · Replies 4 ·
Replies
4
Views
14K
  • · Replies 6 ·
Replies
6
Views
1K