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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Switch
AI Thread Summary
The discussion focuses on writing a switch statement in C++ to evaluate a character variable, `origLetter`. The requirements specify that if `origLetter` is 'a' or 'A', the program should print "Alpha"; if it is 'b' or 'B', it should print "Beta"; and for any other character, it should print "Unknown". Participants clarify that the switch statement is the appropriate structure to use, emphasizing the need for fall-through behavior for the cases of 'a' and 'A'. They also highlight the importance of correctly spelling "default" in the switch statement. The conversation includes guidance on how to implement the print statements for the specified cases, with a suggestion to replace placeholder text with the actual print commands. Overall, the thread serves as a tutorial on using switch statements for character evaluation in C++.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
22
Views
3K
Replies
25
Views
2K
Replies
40
Views
3K
Replies
5
Views
3K
Replies
4
Views
14K
Replies
6
Views
1K
Back
Top