answersLogoWhite

0

Use an else if immediately after the body of an ifwhen you have more than one expression to evaluate. Expressions are evaluated in sequence (top to bottom) and the first evaluation that is true (non-zero) will be the one that executes the statements in its body. Any and all of the remaining expressions are ignored, even if they would have evaluated true.

In the following example, x, y and z are random integers that could be 0 or 1. We evaluate them one by one looking for the first that is non-zero. The comments show what we know to be true about X, Y, and Z at that point, based upon the evaluations that came before.

if( x=1 )

{

// X is definitely 1, but Y and Z might also be 1.

// do something..

}

else if( y=1 )

{

// X is not 1, but Y is definitely 1. Z might also be 1.

// do something else..

}

else if( z = 1 )

{

// Neither X nor Y is 1, but Z definitely is 1.

// do something else..

}

else

{

// X, Y and Z are all 0.

// when all else fails...

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

If you give semicolon after if?

You mean this: if;Syntax error.Or you mean: if (condition);Perfectly legal; if the condition is true, nothing happens. If the condition is false, the else-statement is executed (if there is an else-statement).


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.


Give you a prejudicial rhetorical statement?

Give you a prejudicial rhetorical statement?


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.