answersLogoWhite

0


Best Answer

Indents are not necessary for simple if statements:

if (true) /* do something */ ;

else /* do something */;

However, when a statement is a compound statement, it's best to place the statement body on a separate line and indent it:

if (true) {

/* do something */ ; /* do something */ ; /* do something */ ;

} else {

/* do something */;

/* do something */;

}

The indents help the reader; separating the controlling expressions from the compound statements and thus exposing the structure of the if statement itself.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

In most programming languages - one notable exception is Python - the programming language itself, for example the compiler, DOESN'T CARE whether you use correct indentation or not. The end of an "if" is marked by an "endif" or something similar, or the statements affected by the "if" are enclosed in braces, or - as in the case of Pascal - in special keywords.The indentation is for the benefit of the programmer - to quickly notice which lines are affected by the "if" or by the "else".

In Python, the indentation itself specifies the extent of the "if" statement (as well as other programming constructs), so the indentation IS necessary, because that's how the language works.

If ever I give programming classes, I would not accept a program written without more or less decent indentation, since such programs tend to be unreadable.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why are the indents necessary after the if and else statement?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which of the following is a true statement regarding indents?

indents are measured from the edge of the page


Syntax of nested if in c?

If(condition) { if-else statement; } else { if-else statement; }


What is the scope of variables declared in an if then else statement is false path?

Only the true path in the If...Then...Else statement


Is it possible to print both if statement and else statement?

Yes int main (void) { puts ("if statement"); puts ("else statement"); return 0; }


How If and else run at a time?

The standard syntax is:if( conditional_expression )statement;[[else if( conditional_expression )statement;[else if...]]else statement;][] denotes optional components. Each statement may be a single statement, or may be multiple statements surrounded by braces {}.The if( conditional expression ) statement; is the only required component. In plain English, this reads: if the conditional expression is true, then execute the following statement, otherwise skip to the line following the statement.If the next line is an else statement, then the line reads: if the conditional expression is true, then execute the statement and skip over the else statement. But if the conditional expression is false, then skip over the statement and execute the else statement instead.if( conditional_expression )statement; // execute when conditional expression is trueelsestatement; // execute when conditional expression is falseThe statement following the else can be another ifstatement (a nested if):if( conditional_expression_1 )statement; // execute when conditional_expression_1 is true.else if( conditional_expression_2)statement; // execute when conditional_expression_1 is false and _2 is true.elsestatement; // execute when both _1 and _2 are both false.Note that if an else statement is used without a following if statement, it must appear after all other else if statements.


What is else if in C programming language?

Statements. Typical usage: if (<condition>) <statement>; else <statement>;


What is ladderized if else if conditional statement?

if(condition) { statements /* ... */ }


Why you use if and else statement in c language program?

There is no "elseif" statement in C. You can only use "else" and "if" separately. This is a good reason for switch/case/break.


What happens if statement must contain an?

The if statement must contain else.


What is the syntax of a conditional statement in JavaScript?

In JavaScript we have the following conditional statements:if statement - you would use this statement to execute some code only if a specified condition is trueif...else statement - you would use this statement to execute some code if the condition is true and another code if the condition is falseif...else if....else statement - you would use this statement to select one of many blocks of code to be executedswitch statement - you would use this statement to select one of many blocks of code to be executedFor example: If StatementUse the if statement to execute some code only if a specified condition is true. Syntaxif (condition) {code to be executed if condition is true}If...else StatementUse the if....else statement to execute some code if a condition is true and another code if the condition is not true. Syntaxif (condition) {code to be executed if condition is true}If...else if...else StatementUse the if....else if...else statement to select one of several blocks of code to be executed. Syntaxif (condition1) {code to be executed if condition1 is true}else if (condition2){code to be executed if condition2 is true}else{code to be executed if condition1 and condition2 are not true}else{code to be executed if condition is not true}


What is the difference between ternary operators and if-else statement?

ternary is a single statement operator while even the most primary form of if else contains an if and an else statement. ternary only returns a value but if else can be used to do a lot of other things like printing, assigning values or just returning true or false.


When to use else?

in a threaten statement