Decision making : if-else,else if ladder Errors

Mistake 1 : Assignment Operator & Comparison Operators
  1. Assignment Operator (=) is used for assigning value to variable
  2. Comparison Operator (==) checks whether LHS and RHS entries are same or not
  3. In if statement Use "==" instead of "="
if(var = 10)
{
   //Statement 1
   //Statement 2
   //Statement 3
   //Statement n
}
instead of write this .....
if(var == 10)
{
   //Statement 1
   //Statement 2
   //Statement 3
   //Statement n
}

Mistake 2 : Use of Semicolon
  1. Using Semicolon at the end of if statement
  2. Don't Use Semicolon.
if(var == 10);

Mistake 3 : Else without using if
  1. Error Message : Misplaced Else
  2. Else must have corresponding if
Statement1 ;
Statement2 ;
..
   else
     {
     Statement 3;
     Statement 4;
     .
     .
     } 
}

Mistake 4 : Opening and Closing Braces
  1. If multiple Statements within if must be wrapped in Pair of braces.
  2. Error Message : Misplaced else
  3. Statement1 and statement2 must be wrapped inside if
  4. Here only Statement1 is the part of if
Statement1 ;
  if(condition)
     Statement1 ;
     Statement2 ;
   else
     {
     Statement 3;
     Statement 4;
     } 
Write it as -
if(condition)
     {
     Statement1 ;
     Statement2 ;
     }
   else
     {
     Statement 3;
     Statement 4;
     } 


0 comments

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email