Switch And Go To Statements In C

In the previous tutorial on C programming tutorial for beginners, we learned about conditional if statements. Today we are going to learn about Switch and go to conditional statements with examples.

 

 

 

 

 

Switch Conditional Statement In C With Example:

Switch statement is used when number of alternatives increases. For them switch statements to test the value of a given variable or expression against a list of case values. Actually, this statement tests all the conditions each by each and then executes the operation.

 

 

 

Syntax:

switch (value or expression)

{

case p:

block-p;

break;

case q:

block-q

break;

case r:

block-r;

break;

.

.

.

.

default

default block;

break;

}

statement-x

 

Here, p,q,r,….. are the conditions. To use switch statements you must declare all the cases one by one with case every time. Also, you must break the operation after every case. If you don’t use a break after every case then your code will continue checking the next cases if any of the next cases got true then it will change the value again. I will clarify that in the example. Here, the default statement is similar to else as on the if…else statement.

Now see the example below :

 

 

 

 

Problem: Write a program using switch statement to check a lowercase alphabet whether it is vowel or consonant.

Code:

#include <stdio.h>
int main ()
{
char ch;
scanf(“%c”,&ch);
switch (ch)
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
printf(“%c is a voweln”,ch);
break;
default:
printf(“%c is a consonantn”,ch);
break;
}

return 0;
}

Input: i

Output: i is a vowel

Input: t

Output: t is a consonant

 

Now, remove the break from the case statement and then take a as input. Your output will be:

 

Switch and go to statements in c.

 

 

 

You see there shows a is a vowel also is a consonant. That is because a is true from the first case so it gives the output as a vowel. After that, as we didn’t break the operation. So, it will check the next conditions. As there isn’t have any case. So, it will execute for the default statement. For that the output gives a is a consonant in the last line of the output.

I think it is clear to you now.

If statements are better then switch statements. As there is no need to declare each condition one by one. We can use && or || for using multiple conditions into one if statement. Also after finding one condition true we do not have to use break operation. It will automatically terminate the operation. But we can use switch statements instead of if statements and vice versa. Especially, when we have to check one value or expression for multiple conditions and have there is a possibility to have the answer more than one then we should use a switch statement. For that, we should remove the default statement and declare all the possible cases without breaking the operation.

 

 

 

 

go to Conditional Statement In C With Example:

This conditional statement is also called jump statement. As it is jumps from one step to the other which was mentioned by this statement. See the syntax and example to get better idea about this.

 

Syntax:

go to label;

.

.

.

label:

statement;

 

Here label is the name of the statement to which the pointer is going to jump.

 

See the following problem for better understanding:

Problem: You are given two integer number a and b. Your task is if a is less than or equal to b then show the summation of a and b. Otherwise show the subtraction between a and b.

 

Code:

#include <stdio.h>
int main ()
{
int a,b,ans;
scanf(“%d%d”,&a,&b);
if(a<=b)
{
goto sum;
}
else
{
ans=a-b;
}
sum:
{
ans=a+b;
}
printf(“%dn”,ans);
return 0;
}

Input: 13 24

Output: 37

Input: 17 12

Output: 5

 

Here, first, a and b are taken as input. Then at first, we check that when a is smaller or equal to b then we jump to the sum statement and put the sum of a and b to ans variable. Otherwise, we put the subtraction between a and b to ans.

Remember that a go-to statement can’t operate without the help of other statements like the if…else statement. So, to use a go-to statement we must use any of the other conditional statements.

 

 

 

 

 

Among All Of The Conditional Statements Which One Is Better?

 

If you understand all of the conditional statements. Then you can easily say that if conditional statements are much better then switch and go to statements. Because switch and go to statements are much harder to understand, debug and code compare to if statements. But I am not forcing you to code only with if statements. If you find that switch or go to statement is easier for you then you can use them. But, in our future tutorials, I will use if…else or else if ladder when conditional statements are required.

So, stay well. Wear a mask to prevent yourself and your family from covid-19. Stay home. Stay safe. Take vaccine also encourage your family members to take the vaccine and follow the sanitizing rules. Thank you. Happy codding.

 

 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *