State the difference between
- if-else if ladder and switch…case.
- while and do while loop.
- = and == operators
- a constructor function and a member function of a class
- formal parameter and actual parameter
| IF-ELSE | SWITCH |
| if-else statement uses multiple statement for multiple choices | switch statement uses single expression for multiple choices. |
| Testing if-else statement test for equality as well as for logical expression | switch statement test only for equality. |
| if statement evaluates integer character pointer or floating-point type or boolean | type switch statement evaluates only character or integer value. |
| The use of break statement in switch is essential | there is no need of use of break in else if ladder. |
| WHILE | DO WHILE |
| Condition is checked first then statement(s) is executed | Statement(s) is executed atleast once thereafter condition is checked. |
| It might occur statement(s) is executed zero times If condition is false | At least once the statement(s) is executed. |
| while loop is entry controlled loop | do-while loop is exit controlled loop. |
| No semicolon at the end of while while(condition) | Semicolon at the end of do while while(condition); |
| If there is a single statement brackets are not required. | Brackets are always required. |
| Variable in condition is initialized before the execution of loop. | variable may be initialized before or within the loop. |
| while(condition) { statement(s); } | do { statement(s) ; } while(condition); |
| = | == |
| = sets a variable to the given value | and == compares the value stored in a variable with the given value. |
| x = 2 (now the value 2 is set to x) | x == 3 (checks if the value in x is 3; if so returns true else returns false) will return false as the value of x is 2. |
| constructor | member function |
| A Constructor is a block of code that initializes a newly created object | A Method is a collection of statements which returns/finds a value upon its execution. |
| Constructor should be of the same name as that of class | Method name should not be of the same name as that of class |
| A Constructor is invoked implicitly by the system | A Method is invoked by the programmer. |
| A Constructor is invoked when a object is created using the keyword new | A Method is invoked through method calls. |
| A Constructor can be used to initialize an object | A Method consists of Java code to be executed. |
| Constructor does not return any value | where the method may/may not return a value. |
| formal parameter | actual parameter |
| the parameters/arguments in a function declaration/definition | the parameters/arguments that are passed in a function call |
| int sum ( int p, int q) { return p+q ;} | int c = sum(a,b); or int c = sum(12,8); |