0
Hello, can you explain me what the switch is...?? and how is that working...???
2 ответов
+ 3
A switch statement is a better alternative to using many, many if statements. A switch statement takes in a parameter, which is any variable, and inside the switch statement will be many cases, each case representing what that variable is. Example:
int x = 5;
switch(x)
{
case 3:
//if x = 3
cout << "x = 3";
break;
case 5:
//if x = 5
cout << "x = 5";
break;
default:
//if x is none of the cases above (optional)
cout << "x = ?";
break;
}
That code alone should explain it pretty well.
break; is something you should put at the end of EACH case. Break is what will exit the switch statement after it has found it's case (if any). Without breaks in the code above, the output would be "x = 5x = ?", as it is continuing on with the rest of the switch until it reaches the end.
+ 1
cool