If-Else Block Statements in One Line in C
One-line statement code for the If-else block
Here's something interesting I came across while learning control flow in C program; a shorter method for writing the If-else statements.
But, how's this possible?
It's done using the _Conditional Operator, which is also called the ternary operator.
The conditional operator is represented by the symbols "?" (question mark) and ":" (colon).
But how does this represent a decision making process in C? It's quite easy...
You might have been running away from the C program but it's such an interesting program you might want to reconsider.
Or is the term "low-level C" deceiving you? Or probably scared of pointers?
Let's dive deeper into this conditional operator.
Here's a basic syntax of how the ternary operator works...
variable = expression_1? expression_2 : expression_3;
Hint: Don't forget to add the semicolon at the end of the statement in C...
Let's continue to follow while i break down the syntax below:
The three above listed expressions are the operands, with expression1 containing the **__condition** while expression_2, expression_3 are the statements.
expression_1 is a boolean condition majorly containing the relational operators e.g (p > 3), >,<,>= ...
If expression_1 is true, then expression_2 gets executed...If expression_2 isn't executed, then expression_3 gets executed.
From the above explanation, you can easily deduce that expression_2 is the if statement that gets executed and expression_3 is the else statement.
I guess you're loving the C program now?
Here's an example to illustrate more on the conditional operator.
#include <stdio.h>
/*
* author - Jiresimon 12/9/21
* main - program checks if the variable p is greater or lesser.
* return - 0
*/
int main(void){
int p,q; //declaring the variables
p = 8;
//if-else statement
if p > 7
q = 1;
else
q = 0;
printf("The value of q is %i",q);
return (0);
}
#Output
The value of q is 1
Here's how the conditional operator "?" & ":" eliminates those lines to keep it simple...
#include <stdio.h>
/*
* author - Jiresimon 12/9/21
* main - program checks if the variable p is greater or lesser.
* return - 0
*/
int main(void){
int p,q; //declaring the variables
p = 8;
q = p > 7? 1 : 0; //variable = expression_1? expression_2 : expression_3
printf("The value of q is %i",q);
return (0);
}
Another example to round up with this interesting topic...
#include <stdio.h>
int main(void){
int score; //declaring the variable
printf("Enter your exam score:\n>>> ");
scanf("%i", &score); //take input from the user
(score>=80)? printf("You had a distinction") : printf("Repeat the exam");
return (0);
}
Enter your exam score:
>>> 90
You had a distinction
#Another input
Enter your exam score:
>>> 70
Repeat the exam
Conclusion
The conditional operator is a one-line if-else statement in C whereas the if-else statement is a block of statements. The conditional operator won't be able to work for multiple statements because only three operands are allowed; expression_1, expression_2, expression_3
The conditional operator should be used where necessary but doesn't completely replace the if-else block in C.