Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the tenary operator in C with 3 numbers
#1
The tenary operator was discussed here for some time ago, a bit complicated in Basic. It is quite useful for simple comparisons.

The program uses the tenary operator in C with a triple comparison. It also works with four numbers, but that gets a bit confusing. If one only want to compare two numbers, simply enter 0 for the third number. -- Both files in the same directory.

Code: (Select All)

'Aufruf des tenaeren Operators in C - 8. Aug. 2024

Option _Explicit

Declare Library "D:\Lab\QuickBasic64\Extern-nicht-Basic\Basic-ruft-C\btenaerDreifach"
  Function btenaerDreifach& (ByVal Zahl1 As Long, Byval Zahl2 As Long, Byval Zahl3 As Long)
End Declare

Dim As Long zahl1, zahl2, zahl3

Locate 3, 3
Print "Dreifachen tenaeren Operator in C aufrufen"
Locate 4, 3
Print "=========================================="

Locate 6, 3
Input "Zahl 1: ", zahl1

Locate 7, 3
Input "Zahl 2: ", zahl2

Locate 8, 3
Input "Zahl 3: ", zahl3

Locate 10, 3
Print Using "Die groesste Zahl ist: ####"; btenaerDreifach&(zahl1&, zahl2&, zahl3&)

End

Save the C program as btenaerDreifach.h
Code: (Select All)

//Dreifachen tenaeren Operator von Basic aufrufen - 7. Aug. 2024

#include <stdio.h>
#include <stdlib.h>

long btenaerDreifach(long zahl1, long zahl2, long zahl3)
{
long max;

max = (zahl1 > zahl2 ? ((zahl1 > zahl3) ? zahl1 : zahl3) : zahl2);

return(max);
}
Reply
#2
For those of you who missed out watching reruns of Hogan's Heroes, the tenary operator is basically saying...

if a then b otherwise c 

Which looks to me like the BASIC equivalent of our if/then/else statements.

But then again, I could be channeling Sergent Schultz, in which case, "I know nothing, nothing!"

Pete Big Grin
Fake News + Phony Politicians = Real Problems

Reply
#3
Pete
the difference is that you can assign the ternary expression to a variable, you can basically do the same with if then else, but sometimes you can simplify things by using the ternary expression, but I confess that the ternary operator looks weird unless you use it a lot
Reply
#4
I look at it as shorthand...

C=(A>B)?A:B; 

meaning:

If A > B Then C = A Else C = B

Pete
Fake News + Phony Politicians = Real Problems

Reply
#5
(08-08-2024, 08:53 PM)Pete Wrote: I kook at it as shorthand...

C=(A>B)?A:B; 

meaning:

If A > B Then C = A Else C = B

Pete
I do not think so!

Code: (Select All)

a = 5
b = 4
c = 7

If a > b Then
  c = a
Else
  c = b
End If

Print c

There c would have to be the largest number, right?  Huh
Reply
#6
You can't assign c.

Pete
Fake News + Phony Politicians = Real Problems

Reply
#7
(08-09-2024, 01:05 AM)Pete Wrote: You can't assign c.

Pete

Must be under this new, modern grading system.  A, B, D, E, F  ??
Reply
#8
This one was fun...

Code: (Select All)
Age = 30

Beverage = Age >= 21 ? "Beer" : "Juice";

Results: Beer

So you don't assign beverage, it is determined by the assigned value of age.

Pete
Fake News + Phony Politicians = Real Problems

Reply
#9
https://en.wikipedia.org/wiki/Ternary

spell it right.

(not being a jerk. but i thought the word started with "ter" which got me to look it up.)

https://en.wikipedia.org/wiki/Ternary_co...l_operator
Reply
#10
(08-10-2024, 04:39 PM)hsiangch_ong Wrote: https://en.wikipedia.org/wiki/Ternary
spell it right.
(not being a jerk. but i thought the word started with "ter" which got me to look it up.)
https://en.wikipedia.org/wiki/Ternary_co...l_operator

I think the Kernel was using the 8th instance of the function as in...

I'm tenary the eighth I am
Tenary the 8th I am I am
I got called from the mainframe next door
It's been booted seven times before
And every call was a tenary, it couldn't call a driver or a cam
I'm the eighth old function tenary
Tenary the eighth I am

Pete Big Grin
Fake News + Phony Politicians = Real Problems

Reply




Users browsing this thread: 1 Guest(s)