switch(expression)
{
case constant1:
statements 1;
break;
case constant2:
statements 2;
break;
…………………..
default:
statements n;
break;
}
In the above, “switch”, “case”, “break” and “default” are keywords. Out of which “switch” and “case” are the compulsory keywords whereas “break” and “default” is optional keywords.#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a number");
scanf("%d",&n);
switch(n)
{
case 0: printf("\n Zero");
break;
case 1: printf("\n One");
break;
case 2: printf("\n Two");
break;
case 3: printf("\n Three");
break;
case 4: printf("\n Four");
break;
case 5: printf("\n Five");
break;
case 6: printf("\n Six");
break;
case 7: printf("\n Seven");
break;
case 8: printf("\n Eight");
break;
case 9: printf("\n Nine");
break;
default: printf("Given number is not single digit number");
}
getch();
}
OutputEnter a number5Five Q4. Write a program to check if the student has passed or failed in the exam.A4. /*Show result of student using Marks */#include <stdio.h>#include <conio.h>int main(){ int marks,i; /* Read student marks */printf("Enter the marks (in percentage ) of Student:");scanf("%d",&marks); /* display result */if(marks < 40) { for(i=0;i<40;i++)printf("_");printf("\n\n");printf("Result = Failed\n"); for(i=0;i<40;i++)printf("_");printf("\n\n"); } else {for(i=0;i<40;i++)printf("_");printf("\n\n");printf("Result = Passed\n"); for(i=0;i<40;i++)printf("_");printf("\n\n"); } system("pause"); return 0;} Outputbreak;
When “break” statement used inside loops then execution of “break” statement causes the immediate termination of loop and compiler will execute next statement after the loop statement. 2. Goto statement: “goto” statement is used to transfer the flow of program to any part of the program. The syntax of “go to” statement is as followsgoto label name;
In the above syntax “goto” is keyword which is used to transfer the flow of execution to the label specified after it. Label is an identifier that is used to mark the target statement to which the control is transferred. The target statement must be labeled and the syntax is as followslabel name statement;
In the above syntax label name can be anything but it should be same as that of name of the label specified in “goto” statement, a colon must follow the label. Each labeled statement within the function must have a unique label, i.e., no two statements can have the same label.3. Continue statement: It is used mainly with “switch” and “loop” statements. The execution of “continue” instruction, skip the remaining iteration of the loop and transfer he flow of program to the loop control instruction. Syntax of “continue” statement is as followscontinue;
4. Return statement: The “return” statement terminates the execution of the current function and return control to the calling function. The “return” statement immediately ends the function, ignoring everything else in the function after it is called. It immediately returns to the calling function and continue from the point that it is called. Return also able to "return" a value which can be use in the calling function.Q6. Explain while and do-while statements.A6. while statement:The while loop in C is most fundamental loop statement. It repeats a statement or block while its controlling expression is true.The general form is:while(condition) { // body of loop }The condition can be any boolean expression. The body of the loop will be executed if the conditional expression is true.Here is more practical example.The output of the program is:tick 10 tick 9 tick 8 tick 7 tick 6 tick 5 tick 4 tick 3 tick 2 tick 1do-while statementThe do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its general form is:do{ // body of loop} while(condition);Each iteration of the do-while loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of C’s loops, condition must be a Boolean expression.The program presented in previous while statement can be re-written using do-while as://example program to illustrate do-while looping#include <stdio.h> int main (){ int n = 10; do {printf("tick %d", n);printf("\n"); n--;}while (n > 0);}Q7. Write a program to find the sum of first 10 natural numbers using while statement.A7. #include <stdio.h>int main() { int n, i, sum = 0;printf("Enter a positive integer: ");scanf("%d", &n);i = 1; while (i<= n) { sum += i; ++i; } printf("Sum = %d", sum); return 0;} Enter a positive integer: 100Sum = 5050 Q8. Explain for loop. How is it advantageous?A8. Here is the general form of the traditional for statement:for(initialization; condition; iteration) { // body }The program from previous example can be re-written using for loop as://example program to illustrate for looping#include <stdio.h>int main (){ int n;for(n = 10; n>0 ; n--){printf("tick %d",n);printf("\n"); }}The output of the program is same as output from program in while loop.There can be more than one statement in initialization and iteration section. They must be separated with comma.//example program to illustrate more than one statement using the comma // for looping#include <stdio.h> int main (){ int a, b; for (a = 1, b = 4; a < b; a++, b--) {printf("a = %d \n", a);printf("b = %d \n", b); }}The output of the program is:a = 1 b = 4 a = 2 b = 3Here, the initialization portion sets the values of both a and b. The two comma separated statements in the iteration portion are executed each time the loop repeats.Q9. Write a program to find the factorial of a number using for loop.A9. #include<stdio.h>int main() { int i,fact=1,number; printf("Enter a number: "); scanf("%d",&number); for(i=1;i<=number;i++){ fact=fact*i; } printf("Factorial of %d is: %d",number,fact); return 0;} Output: Enter a number: 5Factorial of 5 is: 120Q10. Write a note on nested loops.A10. Loops can be nested as per requirement. Here is example of nesting the loops.// nested loops #include <stdio.h> int main (){ int i, j; for (i = 0; i< 8; i++) { for (j = i; j < 8; j++)printf(".");printf("\n"); }}Here, two for loops are nested. The number times inner loop iterates depends on the value of i in outer loop.The output of the program is:........ ....... ...... ..... .... ...