[#5] – Control Structures – Questions

C-plus-plus-programming-questions-techhyme

In C++, the programs include statements that are executed one after the other in a given sequence is called sequential execution of the program. But there may be some situations where the programmer requires to alter the normal flow of execution of program or to perform the same operations a number of times.

Index:

For this purpose, C++ provides control structures which transfers control from one part of the program to some another part of the program.

There are various control statements supported by C++ which are broadly categorized as follows:

  1. Decision Control Statements
  2. Loop Control Statements
  3. Jump Statements

Key Points To Remember –

  1. The various control statements supported by C++ are decision control statements and loop control statements. The decision control statements execute certain set of statements depending upon the test condition. The loop control statements execute a single statement or a block of statements repeatedly depending upon the certain conditions.
  2. if, if-else, if-else-if ladder and switch statements are decision control statements. The simple if statement executes statement(s) only if condition is TRUE, otherwise, it follows the next statement.
  3. The if-else statement, execute a statement(s) following the if keyword when a condition is TRUE and execute a different block of statement(s) following the else keyboard when the condition is FALSE.
  4. The if-else-if ladder is used for checking multiple conditions, and different set of statement are executed corresponding to every condition.
  5. The while, do-while and for loops control statements. The for loop is normally used when the number of iterations are known in advance where as in the while and do-while loop the number of iterations are not known in advance.
  6. In the do-while loop, the test condition is evaluated at the end of the loop instead of at the beginning as in the case of while and for loop. So in case of do-while, the body of the loop always executes atleast once even if the test condition evaluates to FALSE during the first iteration.
  7. The break, continue and switch statements cause the program control to branch to a location other than where it normally would go. The break statement transfers control to the next statement outside the loop. The continue statement transfers control to the beginning of the loop. The switch statement is a multiway decision making statement that selects one of several alternatives depending upon the value of a single variable.
  8. goto is a unconditional control statement that cause the control to jump to different location in the program without checking any condition.
  9. The exit() function terminates the program without executing any statement following it.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. What are the various if statements available in C++ ?
    • Simple-if
    • if-else statement
    • Nested if-else
    • else-if ladder
  2. What is the difference between if (i= =j) and if (i=j) ?
    • In the first if statement values of i and j variables are compared but in the second if statement, the value of j is assigned to i and if it is non-zero the result is TRUE otherwise FALSE.
  3. What will be the output of the given statements ?
    if ( i<10);
    cout<<“i is less than 10”;

    • Irrespective of the fact that whether condition evaluates to TRUE or FALSE the message i is less than10 is displayed in each case. This is because if ended with semicolon (;), is a single statement
  4. Name the entry-controlled and exit-controlled loop ?
    • Entry controlled loops : while, for
    • Exit controlled loop : do-while
  5. Which loop’s body is executed atleast once ?
    • do-while loop as body is executed atleast once before the condition is tested.
  6. Write two infinite loops using for loop.
    • for (;;) {…}
    • for (exp1;;exp3) {…}
    • for (;;exp3) {…}
    • for (exp1;;) {…}
  7. What is a null statement ?
    • It is sometimes necessary to cause a delay in a program. Therefore, a loop is made to execute a number of times without producing any results. This is known as Null Statement. It is written as for statement followed by a semicolon.
      for (i=1000;i>0;i–);
      thus it continues 1000 times without producing any output.
  8. Can two case labels have same value ?
    • No
  9. Can a case label in a switch statement have floating point variable or floating point constant ?
    • No
  10. When is if-else-if ladder preferred over switch statement ?
    • We use if else-if ladder when conditions controlling the selection process involve multiple variables. for example,
      if (p<0) //………
      else if (q>1.05) // ……..
      else if (! finish) // ………
      This sequence cannot be recoded with switch statement because all conditions involve different variables and different types.

Other Similar Questions – 

  1. Why there is need of control statements in C++ ?
  2.  What are the various control statements available in C++ ?
  3. What are the various type of if statements available in C++ ?
  4. Explain nested if-else statement ?
  5. Define looping. What are the different type of loop statements available in C++ ?
  6. What is for loop ? Why is it preferred over other types of loop ?
  7. What is while loop ? How is it different from for loop ?
  8. Explain the concept of nesting of loops ?
  9. What is do-while loop ? How is it different from while loop ?
  10. Describe the applications of different loops ?
  11. Explain the differences:
    • break and continue
    • conditional operator (?:) and if-else statement ?
  12. Explain switch statement ? compare it with the use of nested if-else statement.
  13. Define goto statement ? Why its use is discouraged ?
  14. Write ‘True’ or ‘False’ against the following statements ?
    • A semicolon after the while loop will generate a compiler error.
    • Only one conditional expression is allowed in the for loop.
    • for (;;){…} represents an infinite for loop.
    • break and continue are jump statements
    • The break statement enables you to jump immediately to the statement following the end of any enclosing statement block.
    • The continue statement causes the loop to start a new iteration and terminate the current and future iterations of all inner loops.
    • In a switch statement . the cases can be arranged in any order according to the choice of the programmer.
    • The case labels in a switch statement can be an expression or a variable.
    • We cannot use float type constants in case of switch statement.
    • Every if statement must have a corresponding else.
    • The result of logical expression cannot be assigned to an int variable.
    • The expression !(b<0) is true only if b is a positive integer.
You may also like:

Related Posts

Leave a Reply