if else statements in c

If is a conditional statement , with which you may take one or more test conditions. There are 3 types

  • If statement
  • If-else statement
  • If else-if ladder
  • nested if-else

1.if statement

only if statement has only one condition ,if the condition fails it skips to next statement.

syntax:

if(expression)
{ //code to be executed
}

flowchart:

if statement in c
#include
#include
main()
{
int number=0;
printf("enter your age:");
scanf("%d",&number);
if(number>=18){
printf("you are allowed to enter this website",number);
}
getch();
}

2.If-else Statement

if -else is used for both true and false for a condition.

syntax:
if (expression)
sattement1;
else
statement2;

flowchart:

keywords in c

here, if the condition is true it executers statement1 and skips statement2.if the condition is false it executes statement2 and skips satement1. using else part is not mandatory,lets see one example

#include
#include
main()
{
int number=0;
printf("enter your age:");
scanf("%d",&number);
if(number>=18){
printf("you are allowed to enter this website",number);
}
else
{
printf("you are not allowed to enter this website");
}
getch();
}

here, in this example ,the condition is ,the age of the person should be either 18 or more to enter the website. That means if the test expression given (a>=18) is true,it displays first message”you are allowed to enter this website” ,it it is false ,then the second message is displayed “you are not allowed to enter this website”.

3. If else-if ladder Statement

if else-if statement is used to work on multiple conditions .if the respective conditions are true then their statements are executed ,if they are false it skips to next condition

syntax:
if(condition1){
//code to be executed if condition1 is true
}
else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
Else{
//code to be executed if all the conditions are false
}
#include
#include
main(){
int number=0;
printf("enter a number from 1,2,3 :");
scanf("%d",&number);
if(number==1){
printf("number is equals to 1");
}
else if(number==2){
printf("number is equal to 2");
}
else if(number==3){
printf("number is equal to 3");
}
else{
printf("number is not equal to 1, 2 or 3");
}
getch();
}

4.nested if-else

writing if-else in another if-else is called nested if-else

if(condition1) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}

until the condition 1 is true condition2 executes again and again ,when it is false else will be executed

example:

#include
int main()
{
int n1, n2, n3;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1>=n2)
{
if(n1>=n3)
printf("%d is the largest number.", n1);
else
printf("%d is the largest number.", n3);
}
else
{
if(n2>=n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.",n3);
}
return 0;
}
//codeskulls

output:

Enter three numbers: 10 20 30
30 is the largest numbe