Computer Concept & Programming In C - Unit III - 7

Q.9. What is the advantage of switch statement over if-else statement? Write a program in C using switch statement to find the value of Y for a given value of N between 1 to 4:                                                                                                         (AKTU. 2011 - 12)
Ans. The advantage of using switch-case statement above if-else statement is that switch-case aviod checking unnecessary conditions where as if-else checks unnecessary conditions.
Program: Using switch case
# include<stdio.h>
#include<conio.h>
Void main ( )
{
   int N;
   float a, b, x, y;
   clrscr ( );
     printf (“\n Enter the value of a, b, & x”);
   scanf (“%f %f %f ”, &a, &b, &x);
   printf (“\n Enter value of N from 1 to 4”);
   printf (“\n Enter any other value to exit”);
   scanf (“%d”, &N);
  switch (N)
 {
case 1: y = ((a*x) + b)*((a*x) + b);
break;
case 2: y = ((a*x*x) + (b*b*b));
break;
case 3: y = ((-1.0*a*x) + b);
break;
case 4: y = ((a*a) + x);
break;
default: printf (“\n Exit”);
y = 0;
break;
     }
printf (“\n y = %f”,y);
getch( );
}

Q.10     What do you understand by looping? Explain the use of while, do and for loops.
Ans.      Loops: -
Uses of while, do, and for loops
Looping also called iteration is used in programming to perform the same set of operations (statements) over and over until certain specified conditions are met.
Three statements in C are designed for looping:

  • The for statement.
  • The while statement.
  • The do-while statement.

The for Loops: -
The for loop construct is used to repeat a statement or a block of statements a specified number of times. The construct includes the initialization of the counter, the condition, and the increment. Its general form is:
for (counter initialization; condition_increment)
Statement(s);
Consider the following example.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int counter;
clrscr ( );
for (counter = 1; counter < = 10; counter + +)
printf (“%d”, counter);
getch ( );
}
The output of the following program is
1     2      3      4      5      6      7      8      9      10
In this example the variable “counter” started with an initial value 1 according to the assignment: (counter = 1). The second expression in the parenthesis determines the number of repetitions of the loop.
(……..; counter < = 10; ……) It is read as: “as long as the value of counter is less than or equal to 10”. The third part is the counter incrementing: (……;……; counter + +). This statement is same as counter = counter + 1;
which means “increment the counter by one”. The printf statement displays the counter values from “1” to “10”.
The loop block: -
You may use more than one statement in the for loop enclosed in one block as the following example:
# include <stdio.h>
# include <conio.h>
void main ( )
{
int counter;
clrscr ( );
for (counter = 10; counter > = 1; counter - -)
{
printf (“*”);
printf (“%d”, counter);
}
getch ( );
}
The output is: *10 *9 *8 *7 *6 *5 *4 *3 *2 *1
You can also increment or decrement the counter by any value by using traditional assignment statements like:
Counter = counter + 2;
Counter = counter – 2;
Multiple loop variables: -
You can use more than one variable within a for loop for example
# include <stdio.h>
# include <conio.h>
void main ( )
{
int i, j;
for (i = 0, j = 1; i <8; i++, j++)
printf (“\n%d + %d = %d”, i, j, i + j);
getch ( );
}
This statement executes as follows.
0 + 1 = 1
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
4 + 5 = 9
5 + 6 = 11
6 + 7 = 13
7 + 8 = 15
At i = 8 the loop breaks.