Hello Everyone!Now we will discuss about the switch statement.
It is a kind of Selective Flow statement that tests the equality of a variable or expression with a list of Values,also called cases,and execute the cases which satisfies the condition till there is a break or end of switch statement.There can also be a default case which executes when no other case matches with the variable.For example:-
int main()
{
int n;
cout<<"Enter Your Marks in C++ out of 10=";
cin>>n;
switch(n)
{
 case 10:cout<<"Grade=O";break;
 case 9:cout<<"Grade=A+";break;
 case 8:cout<<"Grade=A";break;
 case 7:cout<<"Grade=B+";break;
 case 6:cout<<"Grade=B";break;
 case 5:cout<<"Grade=C";break;
 case 4:cout<<"Grade=D";break;
 case 3:
 case 2:
 case 1:cout<<"Grade=F";break;
default:cout<<"Absent";
}
return 0;
}
In the above program,we ask for an integer n i.e. marks of the student in c++ test.We Allocated Different Grades for Different marks in the switch case as per its syntax.
But there is one thing to notice that cases 1,2,3 will give the same output as there is no break after case 3 and case 2 and if you think there is no need of break after case one as it is the last case,after executing it the switch case ends,But if you don't do so,it will also executes the output for default value.
Here are some outputs:-





These are some of the outputs of the above program.
That's it for today.Next time we will discuss if-else statements.So,till then Keep Coding.
For any queries and doubts,please write in the Comments.

Comments

Popular posts from this blog