June 23, 2021

TriStudy

TriStudy Provide Free Online Tutorials about Programming languages,Digital Marketing, web designing, online earning, web hosting, WordPress etc

C if-else Statements.

In this instructional exercise, you will find out about if articulation (counting if…else and settled if..else) in C programming with the assistance of models.

C if… else statement.

C if Statement.

The sentence structure of the if proclamation in C writing computer programs is:

on the off chance that (test articulation)

{

/proclamations to be executed if the test articulation is valid

}

How if proclamation functions?

The if proclamation assesses the test articulation inside the bracket (). On the off chance that the test articulation is assessed to genuine, proclamations inside the collection of if are executed. In the event that the test articulation is assessed to bogus, explanations inside the assemblage of if are not executed.

How if articulation functions in C programming.

To get familiar with when test articulation is assessed to genuine (non-zero worth) and bogus (0), check social and legitimate administrators.

Model 1: if explanation

/Program to show a number in the event that it is negative

#include

int primary() {

int number;

printf(“Enter a whole number: “);

scanf(“%d”, &number);

/genuine if number is under 0

in the event that (number < 0) {

printf(“You entered %d.\n”, number);

}

printf(“The if articulation is easy.”);

return 0;

}

Yield 1

Enter a whole number: – 2

You entered – 2.

The if articulation is simple.

At the point when the client enters – 2, the test articulation number<0 is assessed to genuine. Henceforth, You entered – 2 is shown on the screen.

Yield 2

Enter a number: 5

The if explanation is simple.

At the point when the client enters 5, the test articulation number<0 is assessed to bogus and the announcement inside the assemblage of if isn’t executed

C if…else Statement

The if statement may have an optional else block. The syntax of the if..else statement is:

if (test expression) {    // statements to be executed if the test expression is true}else {    // statements to be executed if the test expression is false}

How if…else statement works?

If the test expression is evaluated to true,

  • statements inside the body of if are executed.
  • statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,

  • statements inside the body of else are executed
  • statements inside the body of if are skipped from execution.

Example 2: if…else statement.

// Check whether an integer is odd or even#include <stdio.h>int main() {    int number;    printf("Enter an integer: ");    scanf("%d", &number);    // True if the remainder is 0    if  (number%2 == 0) {        printf("%d is an even integer.",number);    }    else {        printf("%d is an odd integer.",number);    }

Yield

Enter a whole number: 7

7 is an odd whole number.

At the point when the client enters 7, the test articulation number%2==0 is assessed to bogus. Thus, the announcement inside the assemblage of else is executed.

C if…else Ladder

The if…else articulation executes two unique codes relying on whether the test articulation is valid or bogus. Now and again, a decision must be produced using multiple potential outcomes.

The if…else stepping stool enables you to check between numerous test articulations and execute various proclamations.

Grammar of settled if…else proclamation.

on the off chance that (test expression1) {

/statement(s)

}

else if(test expression2) {

/statement(s)

}

else if (test expression3) {

/statement(s)

} else {

/statement(s)

}

Model 3: C if…else Ladder

/Program to relate two numbers utilizing =, > or < image

include

int fundamental() {

int number1, number2;

printf(“Enter two numbers: “);

scanf(“%d %d”, &number1, &number2);

/checks if the two numbers are equivalent.

if(number1 == number2) {

printf(“Result: %d = %d”,number1,number2);

} /checks if number1 is more noteworthy than number2.

else if (number1 > number2) {

printf(“Result: %d > %d”, number1, number2);

}

/checks if both test articulations are bogus

else {

printf(“Result: %d < %d”,number1, number2);

}

return 0;

}

Yield

Enter two whole numbers: 12

23

Result: 12 < 23

Settled if…else

It is conceivable to incorporate an if…else proclamation inside the body of another if…else articulation.

Model 4: Nested if…else

This program given beneath relates two whole numbers utilizing either <, > and = like the if…else stepping stool’s model. Be that as it may, we will utilize a settled if…else articulation to take care of this issue.

#include

int fundamental() {

int number1, number2;

printf(“Enter two numbers: “);

scanf(“%d %d”, &number1, &number2);

on the off chance that (number1 >= number2) {

on the off chance that (number1 == number2) {

printf(“Result: %d = %d”,number1,number2);

else {

}

printf(“Result: %d > %d”, number1, number2);

}

}

else {

printf(“Result: %d < %d”,number1, number2);

}

return 0;

}

On the off chance that the body of an if…else articulation has just a single explanation, you don’t have to utilize sections {}.

For instance, this code

in the event that (a > b) {

print(“Hello”);

}

print(“Hi”);

in the event that (a > b)

is equal to

print(“Hello”);

print(“Hi”);