Chapter # 4 Class X
Chapter#4 Control Structure Q#1: What is the purpose of “Default” statements in C++? Ans: The purpose of the default in the switch case is to provide an option in case the conditions specified to trigger the execution of code within the other options does not occur so that the program will not crash. Consider this portion of code with switch case statements. switch(expression) { case “expression1”: /// Code body corresponding to case expression1 case “expression2″: /// Code body corresponding to case expression2 default: //This body is evaluated if none of the case statement are equivalent to the expression. } Q#2: Can we use while loop in place of for loop? If yes, then how? Ans: YES. But in most of the cases it’s advisable to use the loops appropriately. For example, in the below give code, both the loops perform the exact same operation (finding sum of first n numbers) 1. FOR LOOP: for(i=1 ; i<=n ; ++i) ...