Lesson 5
(The multi-branching command switch)
Hi and welcome back!
In this lesson we are going to learn very useful command switch. C language has another way to bring the multi-branching: command switch. Command switch uses an integer expression to calculate where to jump and several case designation as a destination ( picture 1 ).
Brackets ( { } ) limiting block which contains case codes and their associated programming commands.
Break
Mostly blocks of commands which follow case tags in ending is the command break (picture 1 ). The break points continue program execution at the statement following switch commands or commands reps.
The break statement breaks only one level. Thus when used in embedded loop or switch commands in program execution continues in the next outer level.
switch ( n ){
case 1: operation;
break;
If you don't put break on the end, program will continue execution on the next block.
example :
switch ( n ) {
case 1 : operation 1;
.
.
.
case 2: operation 2;
.
.
.
case 3: operation 3;
}
In this example you don't see break. If you write switch command like this first it's going to do operation 1, then operation 2 and operation 3.
You don't want this to happen so you put break on the end of case 1 and case 2. In case 3 you don't need to put break because that's the last operation ( in this case ). Putting break on end completely changes the situation ( if operation 1 is done program will skip operation 2 and 3).
With switch command you can make calculator !
Open your turbo c or whatever you are using and save program like calculator.c
So let's try to make it.
follow my steps :
printf("Number 2 = ");
scanf("%d", &num2);
![]() |
| Picture 1 (Switch) |
Brackets ( { } ) limiting block which contains case codes and their associated programming commands.
Break
Mostly blocks of commands which follow case tags in ending is the command break (picture 1 ). The break points continue program execution at the statement following switch commands or commands reps.
The break statement breaks only one level. Thus when used in embedded loop or switch commands in program execution continues in the next outer level.
switch ( n ){
case 1: operation;
break;
If you don't put break on the end, program will continue execution on the next block.
example :
switch ( n ) {
case 1 : operation 1;
.
.
.
case 2: operation 2;
.
.
.
case 3: operation 3;
}
In this example you don't see break. If you write switch command like this first it's going to do operation 1, then operation 2 and operation 3.
You don't want this to happen so you put break on the end of case 1 and case 2. In case 3 you don't need to put break because that's the last operation ( in this case ). Putting break on end completely changes the situation ( if operation 1 is done program will skip operation 2 and 3).
With switch command you can make calculator !
Open your turbo c or whatever you are using and save program like calculator.c
So let's try to make it.
follow my steps :
- So first thing you need to do is to include header files.
- Open the starting "block" of program ( int main() { and close it } ).
- We are going to need two variables for our numbers. I will name them num1 and num2, because they are integer numbers we have to type int num1, num2 ;
- Ext thing we need is operation ( + , - , / , * ). Type int op.
- Now user has to enter numbers and operation, so we have to type :
printf("Number 1 = ");
scanf("%d", &num1);
printf("Number 2 = ");
scanf("%d", &num2);
printf("operation = ");
op=getch(); - Now we need switch.
switch(op){
case"+": printf(" num1 + num2 = %d", num1+num2);
break;
case"-": printf(" num1 - num2 = %d", num1-num2);
break;
case"*": printf(" num1 * num2 = %d", num1*num2);
break;
case"/": printf(" num1 / num2 = %l", num1/num2);
}
-This is how it looks like in code.
example :
#include <stdio.h>
int main()
{
int num1,num2,op;
float result;
clrscr();
printf("Number 1 = ");
scanf("%d", &num1);printf("Number 2 = ");
scanf("%d", &num2);
printf("operation = ");
op=getch();
switch(op){
case"+": printf(" num1 + num2 = %d", num1+num2);
break;
case"-": printf(" num1 - num2 = %d", num1-num2);
break;
case"*": printf(" num1 * num2 = %d", num1*num2);
break;
case"/": printf(" num1 / num2 = %l", num1/num2);
case"+": printf(" num1 + num2 = %d", num1+num2);
break;
case"-": printf(" num1 - num2 = %d", num1-num2);
break;
case"*": printf(" num1 * num2 = %d", num1*num2);
break;
case"/": printf(" num1 / num2 = %l", num1/num2);
}
getch();
}
Okay that's it for this lesson come back to learn more about C.
I will be posting more lessons about C programming, so follow me, comment if there is something you would change and i will see you next time :) .
If something doesn't work or you need help just email me on: sashans89@gmail.com and i will help you.
Byee.
Byee.



No comments:
Post a Comment