If else statement


    if-else statement 


When the first statement is true it executes the first block of the condition but if the condition is false another block of code will execute. In the condition part, you can use relational and conditional operators. We are not allowed to keep semicolons after condition, if we use it will terminate if condition. 

 

Syntax   

If (condition) 

{ 

True block statement(s); 

} 

else  

{ 

False block statement(s); 

} 

 

Flowchart  

Program  

// if else statement.  

#include <stdio.h>
#include <conio.h>
void main()
{
    int a;
    printf("enter a value of a: ");
    scanf("%d", &a);
    // True part
    if (100 > a)
    {
        printf("first block is executing\n");
    }
    // False part
    else
    {
        printf("second block is executing\n");
    }
    getch();
}

output  

enter a value of a: 102 

the second block is executing  

 

QUESTION:- if else statement with not operator to check either a user can vote or not. 

#include <stdio.h>
#include <conio.h>
void main()
{
    int a;
    printf("Enter your age: ");
    scanf("%d", &a);
    if (!(a < 18)) // use of
    {
        printf("you can vote!\n");
    }
    else
    {
        printf("you can't vote!\n");
    }
    getch();
}

output  

Enter your age: 14 

you can't vote! 

Follow our page for regular updates.

0 Comments