In trying to solve one problem, I ran into another when trying to implement a switch statement. A compile error was given as "transfer of control bypasses initialization of:". I search for what that was. I am expressing what I understood of the explanation I found as best I can.
The "switch" statement is similar to the "goto" statement in that control of the program goes from the current statement to the one labeled in that first statement. When the jump to the second statement results in moving from where a local variable is not in scope to where that local variable is in scope but not initialized, then an error has occurred. The solution is to enclose the case in braces.
For my code below, I don't understand how and where a variable changes scope nor how and why the braces solves the problem. I can only think it has to do with the function I call "AddLast" but I don't understand how.
The bracket on line 2 is not needed. The switch itself needs brackets but the case statements themselves only need brackets if you are using conditionals with more than one statement like:
switch(x)
{
case 1:
//declaration of a variable in this case statement
//this variable has scope until the end of the switch statement
int a =4;
//do some code
break;
case 2:
//variable a stiil in scope
//BUT a will only have been initialized (got initial value)
// if case 1 was carried out
// and we can't do both case 1 and case 2
a = ??? ; //do something with a
break;
default:
break;
}
Putting brackets like you did - will create a local scope
and the variable won't be seen outside that local scope/case statement
1 2 3 4 5 6 7 8
case 1:
{ //local scope - this varaible a will be limited
//to this particular case statement
int a =4;
//do some code
break;
}
I am still not able to understand the question . can some one please explain me
@guestgulkan you have the answer . perhaps you can explain me better what the problem is .
TS, I never said to do that. I merely said that the only brackets you need inside a case statement are if you have a condition that requires several statements.
For example:
1 2 3 4 5 6 7 8 9 10
switch(x)
{
case 1: if ( x == 1)
{
y = 2;
z = 3;
}
break;
default:
}
If you don't have an if or a loop with more than one consequent (the statements that follow) then you don't need brackets.
For example, this doesn't need brackets:
1 2 3 4 5 6 7 8 9 10 11 12 13
switch(x)
{
case 1: if ( x == 1)
y = 2;
elseif (x == 2)
z = 3;
a = 1;
b = 2;
while ( x != 1)
z = 1;
break;
default:
}
I know it looks weird and all but it just goes to show that you only need brackets when your condition or predicate has more than one statement or consequence.
Learning about scope is another thing. It seems like you already understand the basic scope though. Don't declare variables inside brackets unless you plan on them getting destroyed when that bottom bracket gets read by the compiler.
This is why I love Object Oriented programming. But you can learn about that later. haha
There is no other case other then case 1 : also there is no default case :
I don't understand y you need switch case statement you can also do it using if else condition .
Try this code. Then uncomment out lines 14 and 18. You will see that when a local variable is declared within a switch case, the case needs to be within a {...} scope.
Thank you VERY, VERY much for your clear answer. I think I understand what was happening with my code. The variables S_name and S_gpa are the culprits. Without the braces, they "spill over" into other cases that use the same variables, hence the scope violation. With the braces, there is no "spill over".
I agree that IceThatJaw misunderstood my question in his post of 10:39 pm and continues to misunderstand my question in his subsequent posts.
For his and bluecoder's benefit I am including more case of the code in question. It was part of a linked list program. I had "if" statements to choose from adding a node at the head of the list, adding a node at the tail of the list, adding a node in the middle of the list, and deleting a node. I wanted to change the "if" statements to a "switch" statement. That's when I ran into the problem I asked about.
int main()
{
SList student_list;
char select = '0';
while (select != 7) {
cout << "Choose your desired operation.";
cout << "\nEnter a 1 to add a student to the end of the list.";
cout << "\nEnter a 2 to add a student to the start of the list.";
cout << "\nEnter a 3 to add a student to some other point in the list.";
cout << "\nEnter a 4 to delete a student from the list.";
cout << "\nEnter a 5 to display the list.";
cout << "\nEnter a 6 to see a count of the list items.";
cout << "\nEnter a 7 to exit the program" << endl;
cin >> select;
switch (select) {
case'1': {
string S_name;
double S_gpa;
cout << "Enter Student's name." << endl;
cin >> S_name;
cout << "Enter Student's grade point average." << endl;
cin >> S_gpa;
student_list.AddLast(S_name, S_gpa);
break;
}
case'2': {
string S_name;
double S_gpa;
cout << "Enter Student's name." << endl;
cin >> S_name;
cout << "Enter Student's grade point average." << endl;
cin >> S_gpa;
student_list.AddFirst(S_name, S_gpa);
break;
}
case'3': {
int k;
cout << "Enter the position number." << endl;
cin >> k;
string S_name;
double S_gpa;
cout << "Enter Student's name." << endl;
cin >> S_name;
cout << "Enter Student's grade point average." << endl;
cin >> S_gpa;
student_list.AddAfter(S_name, S_gpa, k);
break;
}
...
I am inclined to mark this thread as solved. I will leave it open longer if ITJ and BC want to reply to this post.