2. A switch statement allows a variable to be
tested for equality against a list of values.
Each value is called a case, and the variable
being switched on is checked for each switch
case.
10-Feb-20created by:-sumit
3. 10-Feb-20
created by:-sumit
The syntax for a switch statement in C programming
language is as follows −
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s); break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
4. You can have any number of case statements within a
switch. Each case is followed by the value to be compared to
and a colon.
The constant-expression for a case must be the same data type as
the variable in the switch, and it must be a constant or a literal
When the variable being switched on is equal to a case, the
statements following that case will execute until
a break statement is reached.
A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is
needed in the default case.
10-Feb-20
created by:-sumit
6. 1. #include <stdio.h>
2. int main ()
3. {
4. char grade = 'B';
5. switch(grade)
6. {
7. case 'A' :
8. printf("Excellent!n"
);
9. break;
10. case 'B' :
11. case 'C' :
10-Feb-20created by:-sumit
12.printf("Well donen" );
13.break;
14.case 'D' :
15.printf("You passedn" );
16. break;
17.default :
18.printf("Invalid graden" );
}
19.printf("Your grade is %cn",
20.grade );
21. return 0;
}
Output:-Well done
Your grade is B
7. The break statement in C programming has the following two
usages −
When a break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the next
statement following the loop.
It can be used to terminate a case in the switch statement (covered in the
next chapter).
If you are using nested loops, the break statement will stop the
execution of the innermost loop and start executing the next line of code
after the block.
10-Feb-20created by:-sumit
9. #include <stdio.h>
int main ()
{
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %dn", a);
a++;
if( a > 15)
{
/* terminate the loop using break statement
*/
break;
}
} return 0;
10-Feb-20created by:-sumit
Out out
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
10. This is basically the compilation process of a C program.
The following diagram will show how a C Source Code can be executed.
10-Feb-20created by:-sumit
11. C Code − This is the code that you have written. This
code is sent to the Preprocessors section.
10-Feb-20created by:-sumit
Preprocessing − In this section the preprocessor files are
attached with our code. We have use different header files
like stdio.h, math.h etc. These files are attached with the C
Source code and the final C Source generates. (‘#include’,
‘#define’ These are Preprocessor Directives.)
12. 10-Feb-20created by:-sumit
Compiler − After generating the preprocessed source code, it moves
to the compiler and the compiler generates the assembly level code
after compiling the whole program.
Assembler − This part takes the assembly level language
from compiler and generates the Object code, this code is
quite similar to the machine code (set of binary digits).
13. 10-Feb-20created by:-sumit
Linker − Linker is another important part of the compilation
process. It takes the object code and link it with other library
files, these library files are not the part of our code, but it helps
to execute the total program. After linking the Linker
generates the final Machine code which is ready to execute.
Loader − A program, will not be executed until it is not
loaded in to Primary memory. Loader helps to load the
machine code to RAM and helps to execute it. While
executing the program is named as Process. So process is
(Program in execution).