google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Control Statement in C

Control Statement in C

0
Control Statement in C


Decision-making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Shown below is the general form of a typical decision-making structure found in most of the programming languages



C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.

if statement

Statement execute set of command like when condition is true and its syntax is

Syntax

if (condition)

{

Statement;

}


The statement is executed only when condition is true. If the if statement body is consists of several statement then better to use pair of curly braces. Here in case condition is false then compiler skip the line within the if block.



//Write a Program to Find Number is Greater than 10.

 

#include <stdio.h>

 

void main()

{

    int n;

    printf("Enter a Number : ");

    scanf("%d",&n);

    if(n>10)

    {

        printf("The number is greater than 10\n");

        printf(" The Number is =%d",n);

    }

    else

    {

       printf("The value is less than 10");

    }

}

Output:

 

 

Enter a number: 12

 

The number is greater than 10

The Number is =12



if…..else ... Statement

it is bidirectional conditional control statement that contains one condition & two possible action. Condition may be true or false, where non-zero value regarded as true & zero value regarded as false. If condition are satisfy true, then a single or block of statement executed otherwise another single or block of statement is executed.


Syntax


if (condition)

{

Statement1;

Statement2;

}

else

{

Statement1;

Statement2;

}


Else statement cannot be used without if or no multiple else statement are allowed within one if statement. It means there must be a if statement with in an else statement.






Example:-

/* To check a number is eve or odd */

#include<stdio.h>

 

void main()

{

int n;

printf (“enter a number:”);

sacnf (“%d”, &n);

If (n%2==0)

printf (“even number”);

else

printf(“odd number”);

}

 

Output: 

 

enter a number:121

 

odd number

 


Nesting of if …else

When there are another if else statement in if-block or else-block, then it is called nesting of if-else statement.


Syntax is :-

if(condition)

{

if(condition)

{

Statement 1

}

else

{

Statement 2

}

}

else

{

if(condition)

{

Statement 3

}

else

{

Statement 4

}

}


Example

/* Write a Program to Find Greatest Number of Three Numbers

 

#include <stdio.h>

 

void main()

{

    int a,b,c;

    printf("Enter a Value of a,b,c");

    scanf("%d%d%d",&a,&b,&c);

    if(a>b)

    {

        if(a>c)

        {

            printf("A is the greatest Number");

        }

        else

        {

            printf("C is the greatest Number");

        }

    }

    else

    {

       if(b>c)

       {

           printf("B is the greatest Number");

       }

       else

       {

           printf("C is the Greatest Number");

       }

    }

}

Output

 

Enter a Value of a,b,c56

36

59

C is the greatest Number


if….else LADDER

In this type of nesting there is an if else statement in every else part except the last part. If condition is false control pass to block where condition is again checked with its if statement.


Syntax is :-

if (condition)
{
Statement1;
}
else if (condition)
{
statement2;
}
else if (condition)
{
statement3;
}
else
{
statement4;
}


This process continue until there is no if statement in the last block. if one of the condition is satisfy the condition other nested “else if” would not executed. But it has disadvantage over if else statement that, in if else statement whenever the condition is true, other condition are not checked. While in this case, all condition are checked.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top