© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

8. What is the difference between while and do while?

Ans:

The difference between a "do ...while" loop and a "while {} " loop is that the while loop tests its condition before execution of the contents of the loop begins; the "do" loop tests its condition after it's been executed at least once. As noted above, if the test condition is false as the while loop is entered the block of code is never executed. If the test condition is false as the do while loop is entered the block of code is executed once. Since the condition is tested at the bottom of a do loop, its block of code is always executed at least once.

Ex - 1:  What is the output?
main()
{
            float i=1.5;
      switch(i)
            {
            case 1: printf("1");
                  case 2: printf("2");
                  default : printf("0");
      }
}
Answer:
Compiler Error: switch expression not integral

Ex - 2:  What is the output?
  main()
   {
            int i=10,j=20;
               j = i, j?(i,j)?i:j:j;
             printf("%d %d",i,j);
}
Ans:
10 10

Explanation:

            The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:
            if(i,j)
                 {
               if(i,j)
                 j = i;
            else
                j = j;             
            }
         else
            j = j;

Ex - 3:  What is the output?
main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}
Ans:
Compiler Error: Lvalue required.
Explanation:

As we know that increment operators return rvalues and  hence it cannot appear on the left hand side of an assignment operation.


SLogix Student Projects
bottom