if-else (Decision making & branching )


if-else 

Condition is the commands that are used to check the decision 
 

When we want to make a program in the condition we can apply 2 method  

1) decision making and branching  

2) decision making and looping  

 

 

A) Decision making and branching:- We can do decision making and branch in 4 different methods they are  

1) if the statement  

2) switch statement  

3) condition operation statement  

4) goto statement  

 

I) if statement: we can make a relational and conditional operator program with if statement and they are used in 4 different ways. In if statement zero (0) is considered as false and one (1) is considered as true.  If there is more than a line statement then we have to use curly brackets while making a decision. 

  - simple if 

  - if-else  

  - nested if-else  

  - else if a ladder   

 

  • if statement:- when the condition is TRUE it displays the function or information on the screen.  simple if statement does not consist of else part. 

 

Syntax  

If (condition){ 

 

Statement(s); 

 

} 

 

Flowchart   

Program   

QUESTION:- write a program to print 'even number' if user input is even number with if statement. (use necessary operators ) 

 

#include <stdio.h>
#include <conio.h>
void main()
{
    int a;
    printf("enter a value of a: ");
    scanf("%d", &a);
    if (a % 2 == 0)
    {
        printf("even number!\n");
    }
    getch();
}

output  

enter a value of a: 4 

even number! 

 

 

QUESTION:- if statement with not operator. 

#include <stdio.h>
#include <conio.h>
void main()
{
    int a;
    printf("enter a value of a: ");
    scanf("%d", &a);
    if (!(10 > a)) // use of not operator
    {
        printf("are u being confused?\nread below lines...\n");
    }
    printf("user input value of a is %d"a);
    getch();
}

output  

enter a value of a: 23 

are u being confused? 

read below lines... 

the user input value of a is 23 

 

In the above program, we have used a not-operator which means if the input value is true, not operator will make it false and vice versa. In the program we have checked a user input value is not greater than 10, which means if we input more than 10 the condition will be true and print the statement given after if statement, but if we input less than 10 it will execute an else part (which is marked with red color).  

Follow our page for regular updates.

0 Comments