In this tutorial, we are going to learn about Switch Statement in the C programming language. We have already seen if, if-else, nested if-else, condition, etc. If you haven’t read those tutorials then first go and read those tutorials then read them.
Topics that cover today.
- C switch statement
- C Switch Syntax
- Flow chart of C Switch Statement
- Example of C switch statements
So let’s go with our today’s topic.
C Switch Statement
The C switch statement is used to execute a code block among many alternatives statements. The Switch statement allows the variable to be tested with the statement( Values) also known as Case written in the switch statement and if the statement is verified(true) with a statement then the code written between that statement will execute and then the rest of the code will start executing.
The same thing is also done by the if-else condition. But the syntax of the switch condition is easier to read and understandable for the programmers.
C Switch Statements Syntax.
{
case constant-expression1 :
statements;
break; /* The break function is optional */
case constant-expression1 :
statements;
break;
.
.
.
.
default:
default statements;
}
Flow chart of the Switch Statement
Example of Switch Statement
Program to find whether a number is even, odd or negative.
int main()
{
int number;
printf("Enter a number: ");
scanf("%d",&number);
switch( number%2 )
{
case 0:
printf("%d is even.", number);
break;
case 1:
printf("%d is odd.", number);
break;
default:
printf("Sorry...Number is Negative");
}
return 0;
}
Output:-
People Also Read : Nested IF-Else in C Programming
People Also Read : C Instruction With Example in C Language.
It’s amazing and helpful article for all sort of readers. please share more article. you must read if you are live in Australia I really appreciate to you. I also rerecommended to you lean too...
ReplyDeletelearn more
learn more
learn more
learn more