Skip to content

Conditional Operator

this is one and only  ternary operator of C lang.  it can be used as an alternative to if…else statement involved with assignment of same variable.

Example:
#include<stdio.h>

void main()
{
    int a,b;
    a=12;

    /*if(a>10)
    {
        b=2;
    }
    else
    {
        b=4;
    }*/
    b=a>10?2:4;

    printf(“a=%d”,a);
    printf(“\nb=%d”,b);

}


>>>Nested if
one if statement under the control of another if.  We will use this when we want to execute a block of code when two conditions are returned True.

Inner if appears insides the True part of outer if.


//Example of nested if
void main()
{
   int num=12;
   
   if(num>0)
   {
       if(num%2==0)
       {
           printf(“%d is even natural number”,num);
       }
   }
   printf(“\nRest of the program”);

}

Practice)
Write C Program to determine whether the given number is multiple of both 5 and 10.


//Example of nested if
void main()
{
   int num=12;
   
   if((num>0)&&(num%2==0))
   {
      printf(“%d is even natural number”,num);
     
   }
   printf(“\nRest of the program”);
}




there can be other statements too inside outer if along with ‘inner if’

//Example of nested if
void main()
{
   int num=12;

   if(num>0)
   {
       printf(“%d is natural number”,num);
       if(num%2==0)
       {
           printf(“\n%d is even number too”,num);
       }
       printf(“\nRest of the code in outer if”);
   }

   printf(“\nRest of the program”);

}


else part can be used for both outer and inner if.

//Example of nested if
void main()
{
   int num=12;

   if(num>0)
   {
       printf(“%d is natural number”,num);
       if(num%2==0)
       {
           printf(“\n%d is even number too”,num);
       }
       else
       {
           printf(“%d is odd number too”,num);
       }
       printf(“\nRest of the code in outer if”);
   }
   else
   {
       printf(“%d is not natural number”,num);
   }

   printf(“\nRest of the program”);

}
 
Theory Paper 20M
Multiple Choice questions

Practical Paper 20M
Model 1:
Fill the code in the blanks
Q1)
#include<______________>

void main()
{
__________;
x=10;
printf(“x=____”,x);
}

Q2)
#include<stdio.h>

void main()
{
int x;
x=10;
//write code to display x value
}

Model2: Correc the mistakes in the code
#Include<studio.h>

void Main[]
{
Printf{‘Hello’};
}

Model 3:  Re Arrange the jumbled Code

{}
printf(“x=%d”,x);
int x;
void main()
x=12;
#include<stdio.h>

Model 4:
Write C Program …….