In this section we are going to provide The following Switch Case in C.
Previous | Home | Next |
Switch-case
The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switchcase-default, since these three keywords go together to make up the control statement. They most often appear as follows:
switch ( char expression ) { case constant A: do this ; case constant B : do this ; case constant C : do this ; default : do this ; }
The char expression following the keyword switch is any Cexpression that will yield an char value. It could be an char constant like a,b,c or A,B,C etc an expression that evaluates to an char. The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others. The “do this” lines in the above form of switch represent any valid C statement.
Examle
switch( i ) { case -1: a++; printf("%d",a); break; case 0 : b++; printf("%d",b); break; case 1 : c++; printf("%d",c); break; }
if - else statement
In the if else statement if is a single line condition which contain set of statement with in the parenthesis and if if block condition is true then the control is not move on to the else .else block has no condition for check.
NOTE: Expression will be assumed to be true if their evaluated value is non-zero.if statements take the following form:
if (expression) statement; *************or if (expression) { Block of statements; } if (expression) { Block of statements; } else { Block of statements; } ************* or if (expression) { Block of statements; } else if(expression) { Block of statements; } else { Block of statements; }
const
const means that its not changeable, so the data that is declared with const as must not be give other value in any way during the run of a program.
const are of two type:
-
Primary Constant contain
- Integer Constant
- Real Constant
- Character Constant
-
Secondary Constant
- Array Constant
- Structure Constant
- Union Constant
- Enum Constant
Volatile
Variable can change their value any time during the program compiler should not make any assumption .
about which point of time the variable change its value . Normally compiler will assume that at the run time variable will behave like constant at runtime. This may a cause of error when checking a register value repeatedly. Because register variable may change its value any time. So for these kind of variables should be declared 'volatile' and it be checked each time appears in the code with out any assumption.
void
Void is empty data types normally used as return types in C functions declare that no value will be return by the function. Another used of void is to declare the pointer in C where it is not sure what data type is addressed by the pointer.
Example
void main( ) { }
Previous | Home | Next |