Branching in #C : Switch case and it's uses in program(module 20)
In this section we will discuss on our next topic on branching that is switch statements. Using switch statements we can escape from the loop by only executing a particular element.
Do you have similar website/ Product?
Show in this page just for only
$2 (for a month)
0/60
0/180
This is the 20th module on learning C with us. Earlier we discussed on the basic introduction and structure of C, preprocessor it's features, intermediate and executable codes. compilation and execution process of a C program. And keywords and identifiers, data types, variables and constants, scope of variables, operators and expressions in C. Type casting in #C. Introduction to input and output functions and reading a character in #C. Unformatted and formatted input functions. Branching statements like if, if-else, nested if-else.
In this section we will discuss on our next topic on branching that is switch statements. Using switch statements we can escape from the loop by only executing a particular element.
Switch statement :-
It is used to execute a particular group of statements to be chooses from several available options. The selection is based on the current value of an expression with the switch statement.
The syntx is :
switch(expression)
{
case value 1:
s1;
break;
case value 2 :
s2;
break;
default :
sn;
}
All the option are embedded in the two bracs {}. Within the block each group is written after the label case followed by the value of the expression and a colon. Each group ends with 'break' statement. The last may be labelled 'default'. This is to avoid error and to execute the group of statements in default if the value of the expression does not match value1, value2......
Examples :-
main()
{
int marks, index;
char grade[10];
printf("enter your marks");
scanf("%d",&marks);
index=marks/10;
switch(index)
{
case 10 :
case 9:
case 8:
case 7:
case 6 :
case 5 : grade="first"; break;
case 4: grade="second"; break;
default : grade="fail"; break;
}
}
In next module we will continue our discussions on the next topic on loops like do, do-while and for. For more information regarding #C please be updated with us only on Www.newsandstory.com as we provide all the information on all the topics covered in #C.
CONTINUE READING
technology
programming
coding
learning
Dalpat I
Content writer