Showing posts with label statement. Show all posts
Showing posts with label statement. Show all posts
Monday, February 16, 2015
switch statement in C Part 2
Read: switch statement in C - Part 1
In the last tutorial I told you about the syntax and working of a program using switch keyword. Well in day to day programming we generally don’t use that syntax in C. This is because if we use the earlier method then it will execute all the subsequent statements after the condition turns true.
Syntax
switch(integer expression)
{
case constant 1: Statement 1;
break;
case constant 2; Statement 2;
break;
. . . . . .
. . . . . .
default: Statement 3;
}
Explanation of the above syntax
Well the whole syntax is almost same. But in the above syntax we have added break keyword after the cases. Remember we generally do not use continue keyword with switch.
Working of break keyword in switch statement
As you can see I have given break keyword after every case. Its working in switch case is almost same as in loops. If the compiler encounters break keyword in switch then it will take the control to the outside of switch block.
The perfect way to understand it is through a program.
Output

Explanation
Points to remember
In the last tutorial I told you about the syntax and working of a program using switch keyword. Well in day to day programming we generally don’t use that syntax in C. This is because if we use the earlier method then it will execute all the subsequent statements after the condition turns true.
switch statement in C
So we need a syntax that will execute only a certain set of statements when the condition turns true. So lets checkout the advance modification of switch statement with break keyword.Syntax
switch(integer expression)
{
case constant 1: Statement 1;
break;
case constant 2; Statement 2;
break;
. . . . . .
. . . . . .
default: Statement 3;
}
Explanation of the above syntax
Well the whole syntax is almost same. But in the above syntax we have added break keyword after the cases. Remember we generally do not use continue keyword with switch.
Working of break keyword in switch statement
As you can see I have given break keyword after every case. Its working in switch case is almost same as in loops. If the compiler encounters break keyword in switch then it will take the control to the outside of switch block.
![]() |
| Flowchart of switch statement in C - Image Source |
The perfect way to understand it is through a program.
#include<stdio.h>
void main()
{
int i=10;
switch(i)
{
case 1: printf("Hey its 1");
break;
case 10: printf("Hey its 10");
break;
case 7: printf("Hey its 7");
break;
default: printf("Hey its default");
}
}
Output

- In the beginning of the program I have declared an integer variable with value 10.
- After that I have written switch keyword with integer expression i. It means the compiler will check the value of i (which is 10) with all the cases.
- In the first case I have written integer constant ‘1’ which turns false. So compiler will skip that case.
- In the second case I have written integer constant ‘10’ which turns true. So compiler will execute the statements under that case.
- Now just after the printf() function I have written a break keyword. So it will take the control of the program outside the switch block.
Points to remember
- You can also write multiple statements under each case to execute them. And you will not need to give separate braces for them, as the switch statement executes all statements once the condition turns true.
- Remember default keyword is optional. Its completely depend on us whether we want it or not in our program.
- The order of cases and default in the switch block do no matter. You can write them in any order but you must take care of proper use of break after each case.
Friday, February 13, 2015
switch statement in C Part 1
It is quite common that in day to day life we have to choose one option from several choices. Say if you have to choose only one desert out of the menu. Suppose if I give you a task to write a program for one menu. Probably you will use if-else ladder to make that. Well its good for now. But after reading this tutorial, I would suggest you to go with switch statements rather than anything else.
Syntax
switch(integer expression)
{
case constant 1: do this;
case constant 2: do this;
case constant 3: do this;
default: do this;
}
Explanation of this syntax
integer expression: It is used to check if programmer wants to execute the set of statements under switch or not. It works similar to conditions but we can only give integer expressions insider. It means an expression which is a integer or which evolves an integer.
case: It is the keyword to show multiple conditions inside switch statement. We can have any number of cases.
constant 1,2 and 3: As its name suggests these values should be either integer constant or character constant. Each value must be unique.
do this: We can use any legal C programming statement at that place.
default: It is another keyword which is used to execute some default statements if all conditions fails inside switch.
Now lets apply the above knowledge in some program.
Output

Explanation
Now an obvious question which should hit your mind.
Why is it so?
Well this is not the demerit of this program. In fact it’s the speciality of switch statements. It means if one condition turns true then all the subsequent conditions will also get executed inside switch statement. This is because it does not check any condition after getting a true value with one condition.
Switch statements are used very frequently in C programming. So I would recommend you to go through this tutorial at least once and make some programs by using switch keyword. In the next tutorial I will tell you about the method by which we can stop executing all the statements inside switch.
switch statement in C
This is the decision control instruction which allows the programmer to build a menu type program, so that the user can choose one options from multiple choices. It is also called switch-case-default instructions too because these are three keywords involved in making this instruction. Now lets head on to the syntax of switch statement.Syntax
switch(integer expression)
{
case constant 1: do this;
case constant 2: do this;
case constant 3: do this;
default: do this;
}
Explanation of this syntax
integer expression: It is used to check if programmer wants to execute the set of statements under switch or not. It works similar to conditions but we can only give integer expressions insider. It means an expression which is a integer or which evolves an integer.
case: It is the keyword to show multiple conditions inside switch statement. We can have any number of cases.
constant 1,2 and 3: As its name suggests these values should be either integer constant or character constant. Each value must be unique.
do this: We can use any legal C programming statement at that place.
default: It is another keyword which is used to execute some default statements if all conditions fails inside switch.
Now lets apply the above knowledge in some program.
#include<stdio.h>
void main ()
{
int i=2;
switch(i)
{
case 0: printf("No Message");
case 1: printf("No Message");
case 2: printf("This will print.
");
case 3: printf("Even This will also print.
");
default: printf("Default will also print.");
}
}
Output

- The program starts with integer variable i with value 2 in it.
- After that I have written a switch keyword with one integer expression. i.e. switch(i). It means full switch statements will get executed if i=2 turns true.
- I think you should have an idea that case 2 will get executed.
- Now the turning point is after case 2, every statement get executed.
- Even the last expression under default is also get executed.
Now an obvious question which should hit your mind.
Why is it so?
Well this is not the demerit of this program. In fact it’s the speciality of switch statements. It means if one condition turns true then all the subsequent conditions will also get executed inside switch statement. This is because it does not check any condition after getting a true value with one condition.
Switch statements are used very frequently in C programming. So I would recommend you to go through this tutorial at least once and make some programs by using switch keyword. In the next tutorial I will tell you about the method by which we can stop executing all the statements inside switch.
Subscribe to:
Posts (Atom)
