C Programming How to Use switch Statement - learnit

Home Top Ad

Post Top Ad

Saturday, June 12, 2021

C Programming How to Use switch Statement

C Programming How to Use switch Statement

C switch Statement

C Programming

C programming is a general-purpose , procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers.


Why to Learn C Programming?

C programming language is a MUST for students and working professionals to become a great Software Engineer specially when they are working in Software Development Domain. I will list down some of the key advantages of learning C Programming


-Easy to learn

-Structured language

-It produces efficient programs

-It can handle low-level activities

-It can be compiled on a variety of computer platforms


In this tutorial, you will learn to create the switch statement in C programming

The switch statement allows us to execute one code block among many alternatives.

You can do the same thing with the if …else…if ladder. However, the syntax of the Switch statement is much easier to read and write.

Syntax of Switch ... Case

Switch (Expression)
{
Case contant1
//statements
Break;
Case Contant2
//statements
Break;
.
.
.
Default:
//default statements
}

How does the switch statement work?

The Expression is evaluated once and compared with the values of each Case label

If there is a match, the corresponding statements after the matching label are executed. For example, if the value of the expression is equal to Constant2, statements after Case Constant 2: are executed until Break is encountered.

Example
#include <stdio.h>
#include < conio.h>
int main()
{
int n,total,a,b;
h:
printf("Please Choose Option is Below\n");
printf("1) + \n");
printf("2) -\n");
printf("3)*\n");
printf("4)/ \n");
scanf("%d",&n);
switch(n)
{
case 1:
printf("please input Number A\n");
scanf("%d",&a);
printf("please input Number B\n");
scanf("%d",&b);
total=a+b;
printf("Total=%d\n",total);
break;
case 2:
printf("please input Number A\n");
scanf("%d",&a);
printf("please input Number B\n");
scanf("%d",&b);
total=a-b;
printf("Total=%d\n",total);
break;
case 3:
printf("please input Number A\n");
scanf("%d",&a);
printf("please input Number B\n");
scanf("%d",&b);
total=a*b;
printf("Total=%d\n",total);
break;
case 4:
printf("please input Number A\n");
scanf("%d",&a);
printf("please input Number B\n");
scanf("%d",&b);
total=a/b;
printf("Total=%d\n",total);
break;
defaule:
printf("please Choose Option againt");
goto h;
break;
}
}

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad