Back to Study material
PPS

Unit – 3Conditional Branching And Loops Q1. Explain conditional branching?A1. In conditional branching decision point is based on the run time logic. Conditional branching makes use of both selection logic and iteration logic. In conditional branching flow of program is transfer when the specified condition is satisfied.

111

Fig. Flowchart for conditional branching Q2. Write a program to check if the number is greater than 10.A2. #include <stdio.h>void main(){    int num; printf("Enter the number\n");scanf("%d, &num);printf("num = %d);    if (num>10)    {printf("num is the greater than 10 \n");        }        else        {printf("num is not greaterthan 10 \n");        }    }    else if (num2 > num3)printf("num2 is the greatest among three \n");    elseprintf("num3 is the greatest among three \n");}Q3. Explain switch statement?A3. This statement permits the programmer to choose one option out of several options depending on one condition. When the “switch” statement is executed, the expression in the switch statement is evaluated and the control is transferred directly to the group of statements whose “case” label value matches with the value of the expression. Syntax for switch statement is as follows:

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.
  • “switch” keyword is used to start switch statement with conditional expression.
  • “case” is the compulsory keyword which labeled with a constant value. If the value of expression matches with the case value then statement followed by “case” statement is executed.
  • “break” is the optional keyword in switch statement. The execution of “break” statement causes the transfer of flow of execution outside the “switch” statements scope. Absence of “break” statement causes the execution of all the following “case” statements without concerning value of the expression.
  • “default” is the optional keyword in “switch” statement. When the value of expression is not match with the any of the “case” statement then the statement following “default” keyword is executed.
  • Example: Program to spell user entered single digit number in English is as follows

    #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;} Output

    .Q5. Ellaborate on unconditional branching?A5. In unconditional branching statements are executed without concerning the condition. Unconditional statements allow programmer to transfer the flow of execution to any part of the program. In C language, there are four types of unconditional statements:
  • Break statement: It is the unconditional jump instruction. It is used mainly with “switch” and loop statements. The execution of “break” instruction, transfer the flow of program outside the loop or “switch” statement Syntax of “break” statement is as follows                                                                     
  •                                                             break;

     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 follows              

                      goto                            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 follows

    label 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 follows

    continue;

    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:........ ....... ...... ..... .... ...