Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Conditional Operator in C or Statements Tutorial

Conditional Operator in C

Table of Contents

    • Conditional Operator in C
  • List of popular Conditional Operator in C or Relational operators
    • Conditional Operator in C
    • Conditional Operator
    • Observations on usage of IF, ELSE IF and ELSE:
    • Example 1:
    • Need for Braces { } Example 2:
    • Example 3:
    •  
    • Example 4: Nesting of IF, ELSE IF and ELSE
  • Conditional Operator in C examples
    • Ternary Operator ?: Operator
    • Example
    • Example 2: Nesting of Ternary Operator
    • Assignment Statements with Ternary Operator
    • Assignment Statements with Ternary Operator Example 4
  • Conditional Operator in C MCQ
    • 8) What is the Priority of C Logical Operators.? NOT (!), AND (&&) and OR (||)

Learn Conditional Operator in C MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests

Conditional Operator in C

In real world, solution to any problem relies on the decision we make. Decision is nothing but a condition. C language has conditional operators or statements to deal with conditions or to make decisions. A condition is as simple as an Equals To comparison. Conditional operators are also called Relational Operators in c language.

If a statement satisfies a particular condition, one set of statements are executed. Otherwise another set of instructions are executed. Here, keep in mind that the number of conditions are more than 2 in most of the situation.

Result of a conditional statement is either true or false. Any Non Zero value is always treated as TRUE. Zero is treated as false.

We shall explain Ternary Conditional Operator ?: ( Question Mark Colon ) at the end of this tutorial.

List of popular Conditional Operator in C or Relational operators

Note that there is no space between any individual single operators. = =  is not equal to == operator for example.

SNOConditional Operator in CDescription
1.<Less than
2.<=Less than or equal to
3.>Greater than
4.>=Greater than or equal to
5.==Equals to
6.!=Not Equal to

Conditional Operator in C

Conditional statements in C language are implemented using IF, ELSE IF and ELSE statements. Note that these keywords should be written in lower case always.

Various combinations of if-else statements in c programming language are as below.

if(condition1)
{
  //statements1;
}

//----------------------

if(condition1)
{
  //statements1;
}
else
{
  //statements2;
}
//----------------------

if(condition1)
{
  //statements1;
}
else if(condition2)
{
  //statements2;
}
//---------------------

if(condition1)
{
  //statements1;
}
else if(condition2)
{
  //statements2;
}
else
{
  //statements3;
}

Conditional Operator

Note: It is a good practice to keep opening and closing Braces { } for each IF block or ELSE block for clear understanding and to avoid mistakes. It is a must to use braces if more than 1 statement is present inside an IF or ELSE block. We used the word “block” to represent block of code under single IF or ELSE.

Observations on usage of IF, ELSE IF and ELSE:

  1. There is a SPACE between ELSE and IF. so ELSEIF is wrong.
  2. You can use only IF statement without ELSE.
  3. You can use IF, ELSE IF statements without ELSE at the end.
  4. For single line statements, curly braces are not required.
  5. You need not use ELSE IF always. ELSE is useful to check second or higher condition after first IF.
  6. You can nest IF, ELSE IF and ELSE statements inside any one.

Example 1:

int main()
{
  int a=10;
  if(a>10)
    printf("YES");
  else
    printf("NO");
  return 9;
}
//output: NO
//single statement need not be put inside braces{ }

Need for Braces { } Example 2:

int main()
{
  int a=10;
  if(a>10)
    printf("YES");
    printf("-ANT");
  else
    printf("NO");
  return 9;
}
//Output: error
// to put more than one statement inside
// if or else block, use braces { }
// if(a>10)
// { printf("YES");
//   printf("-ANT");
// }

Example 3:

int main()
{
  int a=8;
  if(a==9)
    printf("YES");
  else if(a != 8)
    printf("NO");
  else
    printf("ANT");
  return 9;
}
//output:
//ANT

 

Example 4: Nesting of IF, ELSE IF and ELSE

int main()
{
  int category=9;
  if(category < 10)
  {
    if(category > 5)
    {
       printf("SUPERVISOR and above");
    }
    else
    {
      printf("UP to CLERK cadre.");
    }
  }
  else
  {
    printf("MANAGER and above.");
  }
  return 9;
}
//output:
// category=9;
//output= SUPERVISOR and above.
//
//category=10;
//output= MANAGER and above.

Conditional Operator in C examples

Ternary Operator ?: Operator

Syntax for Ternary Operator: (Condition) ? (True Part Expression) : (False Part Expression) ;

Example

int main()
{
  int a=20;
  (a >= 20)? printf("YES"): printf("NO");
  
  return 0;
}
//output: YES

You can keep printf statements in true and false parts of a Ternary Operator. Semicolon at the end is compulsory as it will tell the compiler about end of a statement.

Example 2: Nesting of Ternary Operator

int main()
{
  int a=20;
  (a >= 20)? (a!20 ? printf("YES"):printf("CARROT")): printf("ANT");
  
  return 0;
}
//output: CARROT

Nesting of Ternary operator is achieved through proper use of Parantheses ( ) to separate True Part Expression and False Part Expression.

Assignment Statements with Ternary Operator

int main()
{
  int a=20;
  int c = a > 10 ? 5 : 8;
  printf("%d", c);
  
  return 0;
}
//output: 5

We have not used any parantheses in the above assignment operation using ternary operator.

Assignment Statements with Ternary Operator Example 4

int main()
{
  int a=20, b=0;
  int c = a > 10 ? 5 : b=8;
  printf("%d", c);
  
  return 0;
}
//output: ERROR
//correct statement (b=8) with parantheses

In the example above we are making two assignments, one to c variable and another to b variable at the end. Ternaray operator should be used carefully with proper parantheses to separate code parts. Adding a parantheses ( ) to b=8 solves the problem.

[WpProQuiz 27]

Conditional Operator in C MCQ

1) What is the output of C Program.?

int main()
{
    int x=1;
    float y = 1.0;
    if(x == y)
    {
        printf("Polo\n");
    }
    if( 1 == 1.0)
    {
        printf("Golf\n");
    }

    if( 1.0 == 1.0f )
    {
         printf("Boxing\n");
    }
    return 0;
}

A) No Output

B) Boxing

C) Golf Boxing

D) Polo Golf Boxing

Answer [=] D

Explanation:

Integer is promoted to float or double automatically before comparison. So all are equal.

1 == 1.0 == 1.0f

2) What is the output of C Program.?

int main()
{
    int a=9;
    if(a=8)
    {
        printf("Kangaroo\n");
    }
    printf("Eggs\n");

    return 0;
}

A) No output

B) Eggs

C) Kangaroo Eggs

D) Compiler error

Answer [=] C

Explanation:

a=8 is an assignment not comparison. IF( Non Zero) is always TRUE.

3) What is the output of C Program.?

int main()
{
    int a=9;
    if(a==5);
    {
        printf("Kangaroo\n");
    }
    printf("Eggs\n");

    return 0;
}

A) Eggs

B) Kangaroo Eggs

C) No output

D) Compiler error

Answer [=] B

Explanation:

Notice a Semicolon at the end of IF(a==5);. So IF block ends without any statements. Also, { } is not part of IF now. So it is executed without checking for any condition.

4) What is the output of C Program.?

int main()
{
    int a=9;
    if(a==9);
    {
        printf("Ostrich\n");
    }
    elseif(a==8)
    {
        printf("Eggs\n");
    }
    
    printf("White");

    return 0;
}

A) White

B) Ostrich White

C) No Ouput

D) Compiler error

Answer [=] D

Explanation:

Notice IFELSE statement. There should be one SPACE between IF and ELSE.

5) What is the output of C Program.?

int main()
{
    int a=9;
    if(a==9)
    {
        printf("Ostrich\n");
    }
    else;
    {
        printf("Eggs\n");
    }
    
    printf("White");

    return 0;
}

A) White

B) Ostrich White

C) Ostrich Eggs White

D) Compiler Error

Answer [=] C

Explanation:

Notice a Semicolon (;) at the end of ELSE. So ELSE is terminated immediately. Eggs Printf block is not part of ELSE and is always called.

6) What is the output of C Program.?

int main()
{
    int a=9, b=5, c=8;
    a=b=c=10;
    if(a==9)
    {
        printf("Ostrich\n");
    }
    else
    {
        printf("Eggs\n");
    }
    
    printf("White");

    return 0;
}

A) Ostrich Eggs White

B) Ostrich White

C) Eggs White

D) Compiler error as you can not assign to more than two variables at once.

Answer [=] C

7) What is the output of C Program.?

int main()
{
    int a=9, b=5, c=8;

    if(!(a==9))
    {
        printf("Bear\n");
    }
    else
    {
        printf("Elephant\n");
    }
    
    printf("Fox");

    return 0;
}

A) Bear Fox

B) Elephant Fox

C) Fox

D) Compiler error

Answer [=] B

Explanation:

Logical Not Operator ( ! ) changes true to false and false to true. So IF(false) is not executed.

8) What is the Priority of C Logical Operators.? NOT (!), AND (&&) and OR (||)

A) NOT (!) > AND (&&)  > OR (||)

B) NOT (!) > AND (&&) = OR (||)

C) AND (&&) > OR (||) > NOT (!)

D) AND (&&) = OR (||) > NOT (!)

Answer [=] A

Explanation:

Logical NOT Operator in C has the highest priority.

9) What is the output of C Program.?

int main()
{
    int a=9, b;
    
    b = (a==9) ? (printf("CAT\n");printf("DOG\n")) : (printf("FOX"));

    return 0;
}

A) CAT DOG

B) FOX

C) CAT DOG FOX

D) Compiler error

Answer [=] D

Explanation:

You can not put more than 1 statement inside expression1 or expression2 inside Ternary Operator.

(Condition)?(expression1):(expression2)

10) What is the output of C Program.?

int main()
{
    int a=9, b=6;
    if(a==9 && b==6)
    {
        printf("Hockey");
    }
    else
    {
        printf("Cricket");
    }
    

    return 0;
}

A) Cricket Football

B) Hockey Football

C) Football

D) Compiler error

Answer [=] B

Explanation:

== operator has more priority than &&. Logical && AND operator returns true only if both expressions are true.

11) What is the output of C Program.?

int main()
{
    int a=9, b=6;
    if(a!=9 || b==6)
    {
        printf("Hockey\n");
    }
    else
    {
        printf("Cricket\n");
    }
    
    printf("Football");

    return 0;
}

A) Cricket Football

B) Hockey Football

C) Football

D) Compiler error

Answer [=] B

Explanation:

Logical OR || operator returns true if any one expression is true.

12) Choose a correct C Operator Priority.? Items in one group ( ) has same priority.

A) ( ! ) < (*, /, %) < (+, -) < ( <, <=, >, >=) < (==, !=) < (&&) < (||) < (=)

B) (( ! ) , (*, /, %) , (+, -)) < ( <, <=, >, >=) < (==, !=) < (&&) < (||) < (=)

C) ( ! ) > (*, /, %) > (+, -) > ( <, <=, >, >=) > (==, !=) > (&&) > (||) > (=)

D) (( ! ) , (*, /, %) , (+, -)) > ( <, <=, >, >=) > (==, !=) > (&&) > (||) > (=)

Answer [=] C

Explanation:

( ! Logical NOT ) > (*, /, % Arithmetic) > (+, – Arithmetic) > ( <, <=, >, >= Relational) > (==, != Relational) > (&& Logical AND) > (|| Logical OR) > (= Assignment).

13) What is the output of C Program.?

int main()
{
    int a=5, b=8;
    
    if( a==5 && (b=9) )
    {
        printf("Gorilla Glass=");
    }
    printf("%d %d", a, b);

    return 0;
}

A) 5 8

B) 5 9

C) Gorilla Glass=5 8

D) Gorilla Glass=5 9

Answer [=] D

Explanation:

In IF( a==5 && (b=9) ), && Operator checks both expressions for true or Non Zero value. So after checking a==5, b=9 is checked. Here b==9 is checking, but b=9 is assignment. Any NON-Zero value is true only. So b=9 now.

14) What is the output of C Program.?

int main()
{
    int a=5, b=8;
    
    if( a==5 || (b=9) )
    {
        printf("Gorilla Glass=");
    }
    printf("%d %d", a, b);

    return 0;
}

A) 5 8

B) 5 9

C) Gorilla Glass=5 8

D) Gorilla Glass=5 9

Answer [=] C

Explanation:

Logical OR || checks wants only one TRUE condition. First expression (a==5) or even (a=5) is true. So second expression (b=9) is not evaluated and assignment not done. So b=8 only.

15) Choose a correct C Statement.

A) Nesting of ? : operator is possible.

B)

int main()
{
    int a=5, b=8;
    
    if( a>=5 || (b=9) )
    {
        printf("Gorilla Glass");
    }

    return 0;
}
//OUTPUT: Gorilla Glass

C)

int main()
{
    int a=5, b=8;
    
    if( a >= 5 && b <= 9 )
    {
        printf("Gorilla Glass");
    }

    return 0;
}
//OUTPUT: Gorilla Glass

D) All the above

Answer [=] D

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.