Quick links: C-Tricks HR Interview Que. Code Snippets C Programs C++ Programs Java Programs PHP Examples

Home » C programming language

C Interview Questions & Answers (Page -6)

Conditional & Control Statements (if, switch, goto, looping)


62) Difference b/w Entry controlled and Exit controlled loop?

In Entry Controlled Loop, loop body is checked after checking the test condition i.e. condition is checked first after that loop body will execute while in Exit Controlled Loop, loop body will be executed first after that loop’s test condition is checked.

Entry Controlled Loops are : for, while

Exit Controlled Loop is : do while

Consider the example [while loop]:

    int main()
    {
	    int loop;
	
	    loop=1;		//initialisation of loop counter
	    while(loop<=10)	// loop condition
	    {
		    printf("%d\n",loop);
		    ++loop; // loop counter increment
	    }
	    return 0;
    }

Consider the example [for loop]:

    int main()
    {
	    int loop;
	    // initialisation, loop test condition & increment of loop counter
	    // with in for body
	    for(loop=1; loop<=10; ++loop)
	    {
		    printf("%d\n",loop);
	    }
	    return 0;
    }

Consider the example [do...while loop]:

    int main()
    {
        int loop;
        loop=1; // initialisation of loop counter
        do
        {
	        printf("%d\n",loop);
	        ++loop; // loop counter increment
        }while(loop<=10); // loop test condition
        return 0;
    }

In all above examples, for and while loop checks the condition first, but do while checks the condition after the execution of loop body.




63) Difference b/w sentinel control loop & counter control Loop?

In Counter Controlled loop, we know that exactly how many times loop body will be executed while in Sentinel Controlled loop we don’t know about the loop recurrence, Execution of loop is based on condition not counter.

Consider the example:

    /*****Counter Control loop*****/
    for(i=0;i<10;i++)
    {
        //Body of the loop
    }
    

    /*****Sentinel Control loop*****/
    while( n>0)
    {
        n=n/10;
    }  

64) Is nested loop possible?

Yes, it is possible. We can use loop with in the loop any number of times.

Consider the example:

    for(i=1;i<=2;i++)
    {
        for(j=1;j<=10;j++)
        {
            printf("%d  ",j);
        }
        printf("\n");
    }
    1 2 3 4 5 6 7 8 9 10
    1 2 3 4 5 6 7 8 9 10

65) What is fall down property?

In C Programming Language, switch case statement follows the fall down property. It means when case block is executed and break statement is not used after the block statements, then it will execute next case or default statements until break not reached or switch not finished.

Consider the example:

    int i=2;
    switch(i)
    {
	    case 1:
		    printf("\none");
	    break;
	    case 2:
		    printf("\ntwo");
	    case 3:
		    printf("\nthree");
	    case 4:
		    printf("\nfour");
	    case 5:	
		    printf("\nFive");
	    break;
	    default:
		    printf("\nWrong Choice");
    } 
    Two	
    Three
    Four
    Five

Here 3, 4, 5 case are un-necessary are calling during fall down property.

66) Which loop statement is executed at least once even loop test condition if false?

while loop executes once even loop test condition if false. Since do while is an exit controlled loop and in this type of loop, loop body execute once than loop test condition checks.

67) Can we use single statement in loop body without using curly braces "{...}"?

Yes, when loop body has single statement, than we can escape curly braces "{...}".

Consider the example:

    for(i=1;i<=10;i++)
		    printf("\n%d ",i);

68) What are the jumping statements in C Language and how these work?

In C Programming, some special statements are used to transfer program control to one location to other location. There are following jumping statements are used in c:

  1. goto
  2. break
  3. continue
  4. return

1) goto

goto statement is used to jump program's control from one location to define label.

2) break

break statement is used in switch and loop statements, it is used to break the execution of the switch and loop's body and transfer control after the loop/switch statement.

3) continue

continue is used in looping statement, it transfer program's control in the starting of the loop's body.

4) return

Generally return statement is used in function's body, it transfers program's control from called to calling function.

69) What is infinite loop?

A loop which is never finished is known as infinite loop, it means the looping condition is always true, so that loop never terminate. Infinite loop is very useful in embedded systems.

70) Which loop is good for programming?

It depends on the programming need, we can not say which is best to use. Where condition checking is required before body execution, just use for or while. And when condition is not required to check before body execution, just use do while.

71) Can we use continue statement without using loop?

No, continue statement can only be used within the loops only, it can be any loop while, do while or for. If we use continue statement without using loop , there will be a compiler error "misplaced continue".

72) Can goto statement transfer program control from one function to another function ?

No, As we know that goto statement can transfer the program's control to specified label, But it has a limitation, as it can transfer program's control within a function only. Outside the function, control can not be transferred by goto statement.