If Else

LOOPS

  1. Why we use Loops and its types.
  2. See Answer Ans: Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

  3. Is there any diff. b/w while() and for()loop?
  4. See Answer Ans: Both for loop and while loop are used to excute one or more lines of code certain number of times. The main differences are as follows.
        Syntax:
        While loop:     while(condtion) {
        //statements to excute.
        }
        For loop:     for(intialization; condition; Increment or decrement){
        // statements to be excuted.
        }

  5. Diff. b/w while() and do-while()loop?
  6. See Answer Ans: While Loop: Condition is checked first and also known as entry-control loop. Since condition is checked first, statements may or may not get executed.
    Do-while Loop: Condition is checked last and also known as exit-control loop. Since condition is checked later, the body satements will execute at least once.

  7. What happen if we use ; at the end of loop?
    • eg:
      for(i=1;i<=2;i++);
      {
      printf("%d",i);
      }
    • See Answer Ans: The loop will execute without executing any body statement and answer is: 3.

  8. What is Palindrome Number?
  9. See Answer Ans: when a number remains same after the reverse is known as palindrome no. eg. 121

  10. How to check no. is Palindrome or not?
  11. See Answer Ans: First calculate the reverse of a number the check weather it matches with original number or not. if it matches then the no is palindrome otherwise not.
    eg. Rev=0;
    N1=N;
    while(N!=0){
    R=N/10;
    Rev=Rev*10+R; N=N/10;
    }
    if(N1==Rev){
    cout<<"Palindrome";
    else
    cout<<"not Palindrome";

  12. Explain Break Statement
  13. See Answer Ans: When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement.

  14. Explain Continue Statement
  15. See Answer Ans: The continue statement in C programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue statement causes the conditional test and increment portions of the loop to execute.

  16. How to create infinite for() loop?
  17. See Answer Ans: for(;;)
       cout<<"hello";

  18. Program to check whether no. is Prime or not?
  19. See Answer Ans:

  20. When to use nested loops?
  21. See Answer Ans: Nested loops are used where you have to repeat a set of statement repeatedly.