Header Ads

Conditional operator:C Programming Language - Part 2.5

Conditional operator



Conditional Operator: One way to make a choice of two values ​​with one condition. It is written as follows

Condition? Expression2: Expression3

Here the condition is that one condition is that. If true, Expression1 will be selected. If the condition is wrong, Expression2 will be selected.

I think i = 5, then see the conditional operator below

Z = (i <8)? 10: 100;

Here the conditional operator for Z is written. Written here: Z = (i <8)? 10: 100; That means if the value of i is less than 8 then the value of Z will be 10. And if not, the value of z would be 100.

1
2
3
4
5
6
7
8
9
#include <stdio.h>
Int main ()
{
 Int x, y, result;
 Scanf ("% d% d", & x, & y);
 Result = (x> = y)? X: y;
 Printf ("max is% d", result);
 Return 0;
}
The same program, changing the condition, chose the smallest number in two numbers.

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
Int main ()
{
 Int x, y, result;
 Scanf ("% d% d", & x, & y);
 Result = (x <= y)? X: y;
 Printf ("min is% d", result);
 Return 0;
}
Although the same can be done in if -else or many other ways.



No comments

Theme images by sebastian-julian. Powered by Blogger.