Sunday, March 31, 2019

Basics of Programming in C (part-4)


Hello Everyone!


Welcome to my 5th post. First of all, I should thank my family and my friends for supporting me and encouraging me.

In the previous post we have discussed Conditional statements.
Basics of Programming in C (part-3):

In this post we are going to discuss about switch case statements. This post will take less than 5 minutes to completely read and to understand it.

Switch case

Switch case changes the direction of flow of control with respect to the input. The condition in switch statement is compared with case statements in the switch block.when the switch condition and case condition both are equal, the statements in that specific case block are executed. Using default statement is optional but it is preferred to handle errors when switch condition does not matches with any of the case conditions.

Therefore, the statements in the default block executes only if the switch condition does not matches with any of the case conditions.

Using break statement is optional.

Note: If there is no break statement in case blocks then after executing the necessary  case block instead of exiting the switch block compiler executes all other case block which are followed by the necessary case block. We should use break statement in each case block in order to prevent the flow of control in entering into next case block. 




Flow chart:





















Syntax :

 switch(choice_variable)
{
          case case_variable-1:
          Statements-A;
          break;

          case case_variable-2:
          Statements-B;
          break;
          .
          .
          .
          case case_variable-n:
          Statements;
          break;
           
          default:
          Statements;
}


Example:
Code:


Output:



That’s all for this post. In the next post we’ll discuss about  for loops.

Share it with your co-geeks and help me to improve by giving feedback in the comments section below.

Thank you.

 ABOUT ME






















MYSELF SUJITH SAGAR, B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.


No comments:

Post a Comment

How Can You Test and Revert a Failover in Azure Cache for Redis Premium with Geo-Replication?

If you're using Azure Cache for Redis Premium with geo-replication across different regions, knowing how to test failover and revert it ...