C Programming Conditional Operators or Statements Tutorial
Table of Contents
Learn C Programming Conditional Operators or Statements MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
Conditional Operators in C or Statements Tutorial
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 Operators in C or Relational operators
Note that there is no space between any individual single operators. = = is not equal to == operator for example.
SNO | Conditional Operator | Description |
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 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 Operators in C
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:
- There is a SPACE between ELSE and IF. so ELSEIF is wrong.
- You can use only IF statement without ELSE.
- You can use IF, ELSE IF statements without ELSE at the end.
- For single line statements, curly braces are not required.
- You need not use ELSE IF always. ELSE is useful to check second or higher condition after first IF.
- You can nest IF, ELSE IF and ELSE statements inside any one.
Conditional Operators in C Example
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
Nesting of IF, ELSE IF and ELSE Example 4:
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.
Ternary Operator ?: Operator
Syntax for Ternary Operator: (Condition) ? (True Part Expression) : (False Part Expression) ;
Example 1:
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.
Example 3: 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.
Example 4: Assignment Statements with Ternary Operator
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 26]
Conditional Operators in C MCQ
1) Choose a C Conditional Operator from the list.
A) ?:
B) 😕
C) :<
D) <:
Answer [=] A
Explanation:
?: = Question Mark Colon is also called C Ternary Operator.
2) What is the other name for C Language ?: Question Mark Colon Operator.?
A) Comparison Operator
B) If-Else Operator
C) Binary Operator
D) Ternary Operator
Answer [=] D
3) Choose a syntax for C Ternary Operator from the list.
A) condition ? expression1 : expression2
B) condition : expression1 ? expression2
C) condition ? expression1 < expression2
D) condition < expression1 ? expression2
Answer [=] A
Explanation:
If the condition is true, expression 1 is evaluated. If the condition is false, expression 2 is evaluated.
4) What is the output of the C statement.?
int main() { int a=0; a = 5<2 ? 4 : 3; printf("%d",a); return 0; }
A) 4
B) 3
C) 5
D) 2
Answer [=] B
Explanation:
5<2 is false. So 3 will be picked and assigned to the variable a.
5) What is the output of C Program.?
int main() { int a=0; a = printf("4"); printf("%d",a); return 0; }
A) 04
B) compiler error
C) 40
D) 41
Answer [=] D
Explanation:
a = printf(“4”);
First printf prints 4. printf() returns 1. Now the variable a=1; So 1 is printed next.
6) What is the output of the C Program.?
int main() { int a=0; a = 5>2 ? printf("4"): 3; printf("%d",a); return 0; }
A) compiler error
B) 14
C) 41
D) 0
Answer [=] C
Explanation:
5>2 is true. So expression1 i.e printf(“4) is executed printing 4. Function printf() returns 1. So a value is 1.
7) What is the output of the C Program.?
int main() { int a=0; a = (5>2) ? : 8; printf("%d",a); return 0; }
A) 0
B) 1
C) 8
D) compiler error
Answer [=] B
Explanation:
expression1 = empty
expression2 = 8
If no expression is specified, it will be treated as 1.
8) What is the output of C Program.?
int main() { int a=0, b; a = (5>2) ? b=6: b=8; printf("%d %d",a, b); return 0; }
A) 6 6
B) 0 6
C) 0 8
D) compiler error
Answer [=] D
Explanation:
Compiler error. a = (5>2) ? b=6: b=8; should be written as a = (5>2) ? b=6: (b=8);
main.c: In function ‘main’: main.c:14:23: error: lvalue required as left operand of assignment a = (5>2) ? b=6: b=8; ^
9) Choose a correct statement regarding C Comparison Operators.
A) (x == y) Is x really equal to y. (x != y) Is x not equal to y.
B) (x < y) Is x less than y (x > y) Is x greater than y
C) (x <= y) Is x less than or equal to y. (x >= y) Is x greater than or equal to y
D) All the above
Answer [=] D
10) Choose a statement to use C If Else statement.
A) else if is compulsory to use with if statement.
B) else is compulsory to use with if statement.
C) else or else if is optional with if statement.
D) None of the above
Answer [=] C
11) Choose a correct C Statement using IF Conditional Statement.
A)
if( condition ) { //statements; }
B)
if( condition ) { //statements; } else { //statements; }
C)
if( condition1 ) { //statements; } else if( condition2) { //statements; } else { //statements; }
D) All the above.
Answer [=] D
12) What is the output of the C Program.?
int main() { if( 4 > 5 ) { printf("Hurray..\n"); } printf("Yes"); return 0; }
A) Yes
B) Hurray.. Yes
C) Hurray..Yes
D) Compiler error
Answer [=] A
Explanation:
if condition fails. So control will not enter Hurray printf statement.
13) What is the output of the C Program.?
int main() { if( 4 > 5 ) printf("Hurray..\n"); printf("Yes"); return 0; }
A) Yes
B) Hurray.. Yes
C) Hurray..Yes
D) No Output
Answer [=] A
Explanation:
To include more than one statement inside If block, use { } braces. Otherwise, only first statement after if block is included. IF condition fails with false. So second if which is outside of If is executed.
14) What is the output of the C Program.?
int main() { if( 4 < 5 ) printf("Hurray..\n"); printf("Yes"); else printf("England") return 0; }
A) Hurray..Yes
B) Hurray.. Yes
C) Compiler error
D) None of the above
Answer [=] C
Explanation:
If block includes only Single Hurray printf statement without curly braces { }. So second Yes printf statement is not part of IF block. Else should immediately follow IF block. Otherwise, compiler throws errors. To compile well, use { } braces for two printf statements or remove second printf after IF.
15) What is the output of the C Program.?
int main() { if( 10 < 9 ) printf("Hurray..\n"); else if(4 > 2) printf("England"); return 0; }
A) England
B) Hurray..
C) Compiler error for missing else
D) None of the above
Answer [=] A
Explanation:
You can omit ELSE comfortably. Compiler will not complain above ELSE after IF or ELSE IF.
16) What is the output of C Program.?
int main() { if( 10 > 9 ) printf("Singapore\n"); else if(4%2 == 0) printf("England\n"); printf("Poland"); return 0; }
A) Singapore
B) Singapore Poland
C) Singapore England Poland
D) England Poland
Answer [=] B
Explanation:
Observe that Poland printf is not under ELSE IF as there are two statements without curly braces { }. IF condition is TRUE. So, ELSE IF will not be seen at all even though 4%2 == 0 is true.
17) What is the output of the C Program.?
int main() { if(-5) { printf("Germany\n"); } if(5) { printf("Texas\n"); } printf("ZING"); return 0; }
A) ZING
B) Texas ZING
C) Germany Texas ZING
D) Compiler error as a number can not be put as condition inside IF.
Answer [=] C
Explanation:
You can use any number inside IF as a condition.
Positive Number or Negative Number evaluates to true.
Number 0 Zero evaluates to false.
18) What is the output of the C Program.?
int main() { if(10.0) { printf("Texas\n"); } printf("ZING"); return 0; }
A) ZING
B) Texas ZING
C) Compiler error.
D) None of the above.
Answer [=] B
Explanation:
You can use either Integer or Real numbers. 0 or 0.0 evaluates to false condition.
19) What is the output of C Program.?
int main() { if("abc") { printf("India\n"); } if('c') { printf("Honey\n"); } printf("ZING"); return 0; }
A) ZING
B) Honey ZING
C) India ZING
D) India Honey ZING
Answer [=] D
Explanation:
“abc” is string and it returns an Integer Address Number. ‘c’ returns an ASCII number which is also a number. Any Non-Zero number gives TRUE condition.
20) What is the output of C Program.?
int main() { if(TRUE) { printf("India\n"); } if(true) { printf("Honey\n"); } printf("ZING"); return 0; }
A) India ZING
B) Honey ZING
C) India Honey ZING
D) Compiler error
Answer [=] D
Explanation:
There are no keywords (true) or (TRUE). These available in Java, JavaScript and other languages.