C if Statement, If Else Statement & Else If Statement
C if statement, if else statement and else if statement enable programmers to selectively execute certain statements, thus, adding decision making capability to the c language.
if Statement
The general form of if statement is given below:
if(condition)
{
statement_1;
statement_2;
.
.
.
statement_n;
}
Above, condition is any valid logical or relational condition and statement_1, statement_2, …., statement_n are all valid c statements.
All the statements written under the curly braces of if statement are executed only if the condition is evaluated to be TRUE, otherwise, all the statements will be ignored by the compiler.
The following example illustrates use of if statement:
if(a>1 && a<10 { printf("a is greater than 1 and less than 10\n"); } printf("Value of a=%d",a);
In the above example, if a=5 i.e. a is greater than 1 and less than 10, then only the printf() statement written under if statement will be executed along with the printf() statement written outside if statement. Otherwise, if a is not greater than 1 and less than 10, then only the value of a will be printed.
if else Statement
The if else statement is basically the extended form of if statement.
Syntax
if(condition)
{
statement_block_1;
}
else
{
statement_block_2;
}
In if else statement, if the condition is evaluated to be TRUE, then statement_block_1 will be executed, otherwise, statement_block_2 will be executed.
Thus, we can say that in else if statement, either of two statement blocks, one is always executed.
For example,
/* C program to check if a number entered by user is even or odd using if statement*/ #include<stdio.h> main() { int a; printf("Enter a number\n"); scanf("%d",&a); if(a%2==0) printf("The entered number is even\n"); else printf("The entered number is odd\n"); getch(); }
Nested if Statement
If the body of either if or else or both in if else statement includes another if else statement, it is said to be nested if.
Syntax
if(condition_1)
{
if(condition_2)
statement;
else
statement;
}
else
statement;
else if Statement
The else if statement is an extended form of if else statement from two valued logic decision to an n number of logic decision.
Syntax
if(condition_1)
{
statement_block_1;
}
else if(condition_2)
{
statement_block_2;
}
.
.
.
else
{
statement_block_n;
}
Working
In else if statement, if any condition is evaluated to be TRUE, then the statement block connected with that condition will be executed and the compiler will comes out of else if statement.
If no condition is TRUE, then statement block associated with else will be executed.
Else If Statement Example
Consider the following program which checks if a character entered by user is uppercase, lowercase, digit or special character using else if statement:
/* C program to check if a character entered by user is uppercase, lowercase, digit or special character using else if statement */ #include<stdio.h> main() { char ch; printf("Enter a charactern"); scanf("%c",&ch); if(ch>='A' && ch<='Z') printf("Character is a uppercase\n"); else if(ch>='a' && ch<='z') printf("Character is a lowercase\n"); else if(ch>='0' && ch<='9'); printf("Character is a digit\n"); else printf("Character is a special character\n"); return 0; }
5 Comments
at ·
Fixed.
at ·
undefined symbol ‘c’ in main
at ·
fixed…
at ·
it is a best method .excellent
at ·
wat is the use of # include