Conditional execution is helpful when deciding what should happen if something happen.
e.g if you have enough money then u can purchase something otherwise you can't. And in another case what amount of money you're having will decide what are you going to buy.
There are two conditional execution statements available in 'C'.
1. if …. else if ..... else...
1
2..switch (case)
{
case 1:
….......
break:
case 2:
…........
break:
default:
….........
break:
}
“switch case” statements are not so popular though they are much useful when dealing with multiple conditions. However they are not used frequently. “If......else..“ are the statements the most frequently used C statements.
Consider our odd or even program.
We have to check that the given number is odd or even. Here we are using the” if …. else …” statements.
we are using the” if …. else …” statements.
Code Glimpse
int main()
{
int n, a; /*for input and processing*/
printf("Enter the Number"); /*ask the user to enter the number*/
scanf("%d",&n); /* reads the user input and stores it in 'n' */
a=n%2; /* find the remainder for the expression n%2 and stores it in 'a'*/
if(a==0) /* checks whether a is '0' value if yes inner code-block is executed*/
{
printf("The number %d is Even",n); /* prints that the given number is even*/
}
else
{ printf("The number %d is odd",n); /* prints that the given number is odd*/
}
getch();
}
in this example the “if...else...” statements checks whether a reminder is left after the modulo(%) operation, and the remainder is stored in 'a'. then if a is 0 that is no remainder then the number is even otherwise(i.e. A is non zero value) the number is odd.
'''''''All the even numbers are divisible by 2 without any remainder. This logic is used in the above program''''''''
Switch case
Remember our first CP test. In that the calculator programming make use of “switch case” statements.
int main()
{
int a,b;
int option; /*for switch case operations*/
printf(“enter the numbers “);
scanf(“%d %d”,&a,&b);
/*other codes*/
…......................;
..........................;
.............................;
….....................................
/*switch case */
printf(“Enter your choice”); /*print the options in the screen to enable the user to choose his choice*/
printf(“\n 1. Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n”);
scanf(“%d”, &option); /*reads the user input and stores it in 'option' */
switch (option) /*switch operations are done based in the value of 'option'*/
{
case 1: /* code-block is executed if option is equal to '1' */
printf(“ Added sum value is %d”, (a+b));
break;
case 2: /* code-block is executed if option is equal to '1' */2
printf(“Difference between them is %d”,(a-b));
break;
case 3: /* code-block is executed if option is equal to '3' */
printf(“%d times %d is %4d”,a,b,(a*b));
break;
case 4: /* code-block is executed if option is equal to '4' */
printf(“%d divided by %d is %d”, a,b,a/b);
break;
default: /* code-block is executed if option is not equal to all those values */
printf(“you have entered a wrong choice”);
break;
}
getch();
}
I hope that you have understood the basic concepts of “if.....else” and “switch case” statements.
0 comments:
Post a Comment