Switch case statements do a similar job like if else statements. The form is:
switch (variable)
{
case value1:
statement(s);
break;
case value2:
statement(s);
break;
default:
break;
}
Example:
int x = 0;
switch (x)
{
case 0:
cout<<"X is 0"<<endl; //you can have like this code for newline
break;
case 1:
cout<<"X is 1";
cout<<endl; //or you can separate like this.
break;
default:
cout<<"X is neither 0 nor 1";
break;
}
Remember that you can have multiple numbers of case statements not just two like the above example.
Next: C++ Loops
switch (variable)
{
case value1:
statement(s);
break;
case value2:
statement(s);
break;
default:
break;
}
Example:
int x = 0;
switch (x)
{
case 0:
cout<<"X is 0"<<endl; //you can have like this code for newline
break;
case 1:
cout<<"X is 1";
cout<<endl; //or you can separate like this.
break;
default:
cout<<"X is neither 0 nor 1";
break;
}
Remember that you can have multiple numbers of case statements not just two like the above example.
Next: C++ Loops
No comments:
Post a Comment