Give the syntax and working of simple if Else.

04 c] Give the syntax and working of simple if Else.

Answer:-

“The if..else statement is an extension of the simple if statement”

Syntax:-

if (Expression)
{
(true-block;) Statement1;
}
else
{
(false-block;) Statement2;
}
Statement3;

➢ If the Expression is true (or non-zero) then Statement1 will be executed; otherwise, if it is false (or zero), then Statement2 will be executed.
➢ In this case, either true block or false block will be executed, but not both.
➢ This is illustrated in Figure 2. In both cases, the control is transferred subsequently to Statement3.

Flow chart of if-else statement:-

Flow chart of if-else statement

Example:

// Program to display a number if it is negative

#include <stdio.h>
int main()
 {
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }

    printf("The if statement is easy.");

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *