If Else Block

Now you know about If statement. If statement executes the body of the If, if the condition becomes true. But what if when you want to execute the body when the condition becomes false? Here's come the idea of else block.

	if(condition){
		// If body
	}else{
		// Else Body
	}
	// Outside of if-else block.

The whole concept of the structure is same as If statement, except, the else block is execute if the condition evaluates to false. It means when the condition evaluates to true, the If Body is get executed and Else Body is get skipped. If the condition becomes false, the If Body gets skipped and Else Body get executed. So either one of them is get executed depending on the condition.

There are certain rules to remember, like, else part must immediately follow If body. It means you cannot have any statement in the middle of If statement and else statement.

	If(Condition){

	}
	Some statement ...
	else{

	}

The above code will output error message as we have some statement in the middle of If and Else part.

Like, If block, if there's only one statement in the else Body to be executed, you can omit the curly bracket.

	if(condition){

	}
	else
		Statement;

	// Or,

	if(condition)
		Statement;
	else{
		//Statements;
	}

	// Or,

	if(condition)
		Statement;
	else
		Statement;

The alternative approach to the If-Else statement would be like this -

	if(condition){

	}
	if(!condition){

	}

The above approach is alternative to the If-Else block. Here, the condition is same for both If statement. One advantage of the approach is that you can have any number of statements in the middle of these two if statements, as these two if statement acts as seperate individual statement. But If-Else block is a single individual structure and you can not mess up the structure by inserting any statement in between their blocks. Though the alternative approach looks little messy but there will come situation where you must have to take this approach for just because you can insert some statement in between them.

Here, else part is optional.