C - If-Else

  • The if-else statement is used to carry out a logical test and then take one of two possible action , depending on the outcome of the test (i.e. whether the outcome is true or false ).

  • The else portion of if-else statement is optional. Thus , in its simplest general form,the statement can be written as

  • The expression must be placed in parentheses, as shown. In this form , the statement will be executed if the expression has nonzero value (i.e. if the expression is true ).If the expression has a value of zero (i.e. if the expression if false),then statement will be ignored.

  • The statement can be either simple or compound.In practice, it is often compound statement which may include other control statements.
  • The general form of if statement which include else clause is
    if(expression)statement1 else statement 2

    If the expression has a non Zero value (i.e. if the expression is true),then statement 1 will be executed . otherwise(i.e. if expression is false), statement 2 will be executed.
    consider an example ,

void main() {
int a=5,b=10;
	printf("sum is 15");
	printf("sum is not 15");

output :

sum is 15

  • Print Page