• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Yamauchi

Banned
Do any of you know of a good guide to matrix manipulation using either C++ or Java (preferably the latter)? I am in my final year of a CS degree and, looking at careercup.com, it seems that an awful lot of interview questions deal with matrices.

Any help would be greatly appreciated.
 

Water

Member
Do any of you know of a good guide to matrix manipulation using either C++ or Java (preferably the latter)? I am in my final year of a CS degree and, looking at careercup.com, it seems that an awful lot of interview questions deal with matrices.
What kind of matrix manipulation?
Neither language has matrix handling in standard libraries so to do anything serious you really want to use 3rd party libs, different ones depending on the computation. I find it unlikely that someone will ask you to roll your own matrix operation implementations in an interview.
 

Yamauchi

Banned
What kind of matrix manipulation?
Neither language has matrix handling in standard libraries so to do anything serious you really want to use 3rd party libs, different ones depending on the computation. I find it unlikely that someone will ask you to roll your own matrix operation implementations in an interview.
I should have been more precise. I meant 2D arrays.
 

NotBacon

Member
What's wrong with an ide? I'm a newbie programmer who just moved from Windows to Linux since I reformatted and VS was comfortable to me. I just want an experience similar to that of VS to ease me into using Linux.

I installed QT creator based on a suggestion in the thread. What makes it inferior to Eclipse?

I guess I'm biased as a proponent of terminal coding, but I started with an ide too and it is good way to start, but I suggest playing with vim and the command line soon too to get more exposure to the nitty-gritty.
 

Relix

he's Virgin Tight™
Anyone here with Cassandra + .NET C# experience? i am a bit lost :p At everything! I got the driver installed and followed a guide for at least a basic app but I am a bit lost right now. Can't wrap my head around it.
 

Chris R

Member
Hey guys, I'm having some trouble with SQL on PostgreSQL.

I have two tables, one is called 'staff' and one is called 'branch'.

The staff tables contains an attribute (int) called 'salary' and 'staffno' and the branch table contains an attribute 'city'.

I want to increase the salary of all staff at the London branch by 10% but I'm having some trouble. I've attempted it and here's what I've got but it looks too wrong and won't work:
Code:
UPDATE a SET a.salary = a.salary * 1.1
FROM staff a, branch b
WHERE b.city='London' AND b.staffno=a.staffno;

Any ideas? Thanks.

Firstly, your relation is setup in the wrong way. You want each staff member to have a branch number. Secondly I guess Postgresql has issues with aliases in updates??? I banged my head against the keyboard for 10+ mins just now before I got it to work.

http://sqlfiddle.com/#!12/3e4d4/3
 

hateradio

The Most Dangerous Yes Man
Do you load an external image, or do you create/manipulate it via code? You're currently creating a bitmap with 16000x16000 pixels, which is with a supposed color depth of 32 around 1GByte large. Maybe you could split up the large bitmap into several smaller one?
An external image is loaded. The image is actually larger than the target, which is 16000px.

Code:
Image source = Image.FromFile(path, true); // large image

Bitmap img = new Bitmap(size, size, PixelFormat.Format24bppRgb); // size = 16000

Graphics g = Graphics.FromImage(img);

g.DrawImage(source, [new dimmensions]); // Memory

I thought about making doing it in chunks, but we'll see. The image does have to be a whole though.
 
Okay I have to ask, why are you using an ide?
And if you HAVE to, why not eclipse?
I learned programming with no IDE (in Java) and it was fine, but messing around with the command line all the time does get tedious (especially on Windows, urgh) so an IDE makes your life a lot easier, click on the Run button and the code runs. Debugging from the command line is not a lot of fun either.
 
I think you should examine your own perspective before espousing about disconnection from reality. There are a massive number of jobs out there where you may not have to ever touch linux in any capacity. The reality is that every job is different, and radicalism is always wrong. Saying that your environment or experience is true everywhere, for everyone is a great demonstration of a reality disconnect. That includes your view that linux is everywhere and ubiquitous, and that visual studio is not a good reason to use windows. You're wrong.

My response was not "Linux is everything and Visual Studio is shit", it was "moka isn't saying that you need a supercomputer to have a use case for Unix/Linux". Nor did I say that my experience or environment was true in any other sense than a real world example of the aforementioned response. Ubiquitous was not the intended meaning of "everywhere" in my response. In the same way that McDonalds are everywhere, but not every fast food restaurant (or building for that matter) is a McDonalds.

Not taking a vehement stance against one environment over another is Career Survival 101 in this field. Especially not when the only arguments you can muster are "it's shit, I don't like it and it's too hard".
 
Ooh god why did i decided to make an concurrent game engine just spent the last 3 hours looking for an area which deadlocked my code. And when found it was so obvious i had to facepalm. And code base is still dam small.
 

Slavik81

Member
I've been working with git submodules and though I'm only a beginner, they seem a little difficult to use. The concept still seems like a good one to me, though. Reading through "Why your company shouldn't use Git submodules" I just get the feeling that this is not something I should be suggesting as a way forward. It feels too complicated.

I've heard repo mentioned as a possible solution more than a few times. Any anyone worked with it? Does it integrate nicely with github?
 

usea

Member
My response was not "Linux is everything and Visual Studio is shit", it was "moka isn't saying that you need a supercomputer to have a use case for Unix/Linux". Nor did I say that my experience or environment was true in any other sense than a real world example of the aforementioned response. Ubiquitous was not the intended meaning of "everywhere" in my response. In the same way that McDonalds are everywhere, but not every fast food restaurant (or building for that matter) is a McDonalds.

Not taking a vehement stance against one environment over another is Career Survival 101 in this field. Especially not when the only arguments you can muster are "it's shit, I don't like it and it's too hard".
Sorry about that. I dramatically misunderstood your post and went off when I shouldn't have. Apologies. I definitely took your post to mean linux was everywhere in the sense that if you didn't know it, you weren't going to get a job, etc.

I thought the guy who said linux is shit was joking, trying to provide a counter-point to the post that said "if you don't understand linux you're a shitty programmer." Both are ridiculous. Even when I say javascript is shit, it's in jest. I guess I misunderstood. Like you said, taking extreme/religious stances like that makes no sense. There are always tradeoffs, pros and cons, etc with everything. There are always extenuating circumstances. Everybody's experience is different. There is no right and wrong (except tabs vs spaces).

Sorry again.
 

leroidys

Member
Because tabs is the correct way right?

t51i70W.jpg
 

Massa

Member
For indenting, correct. Using spaces for indenting makes zero sense. For aligning, only spaces make sense. But I hate aligning anyway so whatever.

Using spaces for indenting is the only way to guarantee consistency in a codebase worked by more than one person.
 

usea

Member
Using spaces for indenting is the only way to guarantee consistency in a codebase worked by more than one person.
I strongly disagree, for any reasonable definition of consistency.

The tab character literally indents by 1 unit. That's what it does. If you want to indent, using a tab does it perfectly. If you want to move text over by 4 spaces, then a tab won't work. Because I might have my tab width set to 2, while yours is set to 4. But that's not an indent, that's a move-over-by-4-spaces.

If your whole team is cool with defining indent as move-over-by-X-spaces then that's fine. It's sub-optimal, but it's fine.

Tab as indenting is completely consistent. It moves over the line by 1 unit of indent, which each viewer is allowed to configure however they please. When I change how I view a tab, it doesn't change the actual text. It doesn't enforce my viewing habits on other members of my team. The same can't be said for spaces, which is restrictive, takes up more room in the file, and doesn't do the job as well.

I can't help but think that anybody who thinks spaces are superior for indenting doesn't understand how the whole thing works.
 

Massa

Member
The problem with tabs is that how they're displayed is not consistent between different editors. So that "1 unit" could be 4 spaces on your environment and 8 for a different editor used by a different person.

With that in mind, if someone uses spaces and they add a block of code using 4 spaces for indentation you wouldn't even notice it, but anyone that uses a different rule will see an inconsistent mess when they open the file. It's just much easier to ignore them altogether, using spaces guarantees the code looks correct no matter where and who is viewing it.
 

usea

Member
The problem with tabs is that how they're displayed is not consistent between different editors. So that "1 unit" could be 4 spaces on your environment and 8 for a different editor used by a different person.
This is an advantage, not a problem. It's the whole point! What if I use a font where spaces are really large? Why would I want 4 spaces as indents, when 2 looks better on my screen. You present this as a problem, but there's nothing wrong with it. I can view my text however I want. It doesn't affect anybody else.

With that in mind, if someone uses spaces and they add a block of code using 4 spaces for indentation you wouldn't even notice it, but anyone that uses a different rule will see an inconsistent mess when they open the file. It's just much easier to ignore them altogether, using spaces guarantees the code looks correct no matter where and who is viewing it.
If somebody indents with spaces and it messes things up because the rest of the team uses tabs, the problem is the person who committed a file that indented with spaces. Tabs aren't the problem. Anyway this would be caught by style cop, or when I loaded the file locally I would get a warning that somebody indented with the wrong character.

Spaces don't guarantee it looks correct, unless your definition of correct is "has the same number of spaces." But that definition isn't useful. What if spaces in my font are a different size?

You shouldn't try to ram indent width down your team's throat. There's no benefit, and it causes problems. Just use a tab, which encodes "1 level of indent" into the file. And your editor will display whatever you want 1 indent to be.
 

tokkun

Member
I think the thing to keep in mind is that it is a lot easier to guarantee that spaces-only is used consistently than it is with tabs.

With spaces-only, you can disable tab characters in your editor. It's simple to implement checking into your linter; just raise an error on any tab character.

With tabs, it is more difficult. You can't eliminate spaces from the file, and you need spaces to do alignment.

I suspect this is the primary reason that you see spaces-only used at most large companies.
 

usea

Member
Guys I was just making a joke.
So was I. Obviously it doesn't matter which one you use, as long as a team is consistent about it. You should use whatever your team is already using when you join.

But that doesn't mean there are real benefits for using one over the other :)
 
So was I. Obviously it doesn't matter which one you use, as long as a team is consistent about it. You should use whatever your team is already using when you join.

But that doesn't mean there are real benefits for using one over the other :)
You have some pretty intense jokes usea lol.

So why hasn't somebody made a plugin that handles conversion of tab presses into a designated amount of space characters? I understand this can already be done on any basic editor but what about added features like auto detection of indents for editing between environments and such? Then tab guys get their one press, space guys get their alignment and companies get their consistency. It doesn't seem like a real easy task but it appears to be important enough to tackle.
 

usea

Member
You have some pretty intense jokes usea lol.

So why hasn't somebody made a plugin that handles conversion of tab presses into a designated amount of space characters? I understand this can already be done on any basic editor but what about added features like auto detection of indents for editing between environments and such? Then tab guys get their one press, space guys get their alignment and companies get their consistency. It doesn't seem like a real easy task but it appears to be important enough to tackle.
Tab characters should be in the file.

There are no downsides, only benefits.
 

Sharp

Member
So after reexposure to ML and some hunting for studies, I've come to the conclusion that object orientation is well and truly useless. The majority of studies seem to show it has no bearing on productivity, it has plenty of theoretical issues (Liskov substitution principle is undecidable), and pretty much everything it does well can be done better by strict message passing, (polymorphic) algebraic datatypes, and interface abstraction. Bonus points for a well-developed macro system. That being said: what modern language can I actually use that has all those things and isn't highly specialized? A.k.a. not ML or Haskell--not that I don't like writing compilers. Rust looks promising but the compiler crashed the very first time I tried using it, so I am thinking it is not ready for prime time.
 

usea

Member
So after reexposure to ML and some hunting for studies, I've come to the conclusion that object orientation is well and truly useless. The majority of studies seem to show it has no bearing on productivity, it has plenty of theoretical issues (Liskov substitution principle is undecidable), and pretty much everything it does well can be done better by strict message passing, (polymorphic) algebraic datatypes, and interface abstraction. Bonus points for a well-developed macro system. That being said: what modern language can I actually use that has all those things and isn't highly specialized? A.k.a. not ML or Haskell--not that I don't like writing compilers. Rust looks promising but the compiler crashed the very first time I tried using it, so I am thinking it is not ready for prime time.
Yeah Rust is not ready for prime time quite yet. It's on version 0.8, and the syntax is still changing a bit. Version 1.0 (which promises no more language changes, etc) is probably 6-9 months away.

Are you on OSX? There's currently a bug with compiling it there. Also Windows is basically a crapshoot and requires mingw even at runtime, last I looked. It needs a lot of attention. I run an Ubuntu VM largely for rust. If you hit up the irc channel or the issues list on github, you could probably find a solution.

Rust is extremely promising I think. I'm really looking forward to it. Its creator is an OCaml developer.

What's wrong with Haskell? No side effects too restricting? Scala, while very object-oriented, is all right. It sees use in production at many big companies, too. There's also F# and Clojure. Or Nemerle, but that's "optional OO" as well.

No language is perfect. At the end of the day you just want to get things done without hating life. If you use a subset of a language, you can usually avoid parts you don't like.
 

Sharp

Member
Yeah Rust is not ready for prime time quite yet. It's on version 0.8, and the syntax is still changing a bit. Version 1.0 (which promises no more language changes, etc) is probably 6-9 months away.
I'll watch with interest, it looks closest to what I'm looking for.
Are you on OSX? There's currently a bug with compiling it there. Also Windows is basically a crapshoot and requires mingw even at runtime, last I looked. It needs a lot of attention. I run an Ubuntu VM largely for rust. If you hit up the irc channel or the issues list on github, you could probably find a solution.
OS X, but yeah, I'd be fine with using a VM for Rust. To be clear, though, it compiled fine--Rust's compiler crashed while trying to compile my program :p
Rust is extremely promising I think. I'm really looking forward to it. Its creator is an OCaml developer.
I know. It looks like one of the better designed languages ever, although I'm sad that it looks like it categorically won't / can't be supporting TCO.
What's wrong with Haskell? No side effects too restricting?
No side effects is fine, the problem is lack of library support unless you're doing math.
Scala, while very object-oriented, is all right. It sees use in production at many big companies, too.
Yeah, I've looked at Scala in the past and I've gotten us to switch over at work where we were previously using Java. I can't get over the "functional Java" feeling, though, and as you said it is pretty object-oriented.
There's also F# and Clojure.
Not working on MS platforms at the moment. Clojure is a LISP dialect, right? I like LISP but I have trouble convincing other people to use it for large projects.
Or Nemerle, but that's "optional OO" as well.
Never checked out Nemerle, I will. Thanks!

Edit: Ah, also .NET? I guess I could use it on Mono...
No language is perfect. At the end of the day you just want to get things done without hating life. If you use a subset of a language, you can usually avoid parts you don't like.
Heh, yeah, I'm well aware of that. I'm not looking for perfection--I tend to think the best languages are domain specific. But I am looking for a really good "go-to" language at the moment, which is why I asked. Thanks for all the suggestions--seems like you're fairly up to date on the available options.
 

usea

Member
I don't know anybody who uses Nemerle. If I was looking for a functional language on the CLR I'd do F#. I just thought it was worth mentioning.

Yeah Clojure is a LISP on the JVM. It's fairly popular, but I don't like lisps personally. I'd be more inclined to use Scala.

I don't feel like I know that much about programming languages. I'm just getting into functional programming (doing the scala course on coursera) and I'm reading Programming Language Pragmatics (book). I have zero clue how to write even the most basic compiler, since I skipped that class in college. I've written toy programs in a lot of languages, but I've only written programs of a significant size in Java, C#, C++, and javascript. Most of the languages I've used have been for tiny experiments or toy programs.
 

Sharp

Member
Yeah, I get where you're coming from. I'm writing a compiler right now so ML is a bit of a godsend, which led me to reevaluate some of this stuff--not that I was ever super enamored of OOP, but I'm starting to feel like it's actively getting in the way. I guess my question was more sort of born of frustration that I can't do the things I can in ML in almost any mainstream language, when it is precisely those things that I actually need. E.g., destructuring of objects via pattern matching--and I'm not just talking about the usual Java/C++ suspects here, even languages like Python and Ruby don't really support it well at all. And while mutable state is fine when efficiency is on the line, I *hate* how most nonfunctional modern languages assume it by default--hell, in some cases (Java) I can't even declare a local immutable, as far as I can tell. Anyway, this is starting to turn rantlike so I'll end this here :p
 

Cindres

Vied for a tag related to cocks, so here it is.
Okay I have to ask, why are you using an ide?
And if you HAVE to, why not eclipse?

IDEs just make life easier, I don't get why people would punish themselves with a text editor when an IDE can catch problems before even hitting build.

As for tabs, if you're working with a team and you're concerned about it just make sure you're all using the same space indenting, I'm of the opinion that tabs are easier to read than single spaces.
 
Yeah, I get where you're coming from. I'm writing a compiler right now so ML is a bit of a godsend, which led me to reevaluate some of this stuff--not that I was ever super enamored of OOP, but I'm starting to feel like it's actively getting in the way. I guess my question was more sort of born of frustration that I can't do the things I can in ML in almost any mainstream language, when it is precisely those things that I actually need. E.g., destructuring of objects via pattern matching--and I'm not just talking about the usual Java/C++ suspects here, even languages like Python and Ruby don't really support it well at all. And while mutable state is fine when efficiency is on the line, I *hate* how most nonfunctional modern languages assume it by default--hell, in some cases (Java) I can't even declare a local immutable, as far as I can tell. Anyway, this is starting to turn rantlike so I'll end this here :p
You can declare a local variable as final, but it's rarely used:
Code:
public class Test {
	public static void main(String[] args) {
		final int t = 5;
		t = 12; // Compiler error
	}
}
 

Sharp

Member
You can declare a local variable as final, but it's rarely used:
Code:
public class Test {
	public static void main(String[] args) {
		final int t = 5;
		t = 12; // Compiler error
	}
}
That only works with primitives. Otherwise, you can still modify all state associated with the variable. Contrast this with a const struct in C.

(You might argue that this is because in Java all objects are really pointers, but you can *also* declare the value of a pointer constant in C, and in fact every level of indirection can be constant or nonconstant, which is not the case in Java).
 
That only works with primitives. Otherwise, you can still modify all state associated with the variable. Contrast this with a const struct in C.

(You might argue that this is because in Java all objects are really pointers, but you can *also* declare the value of a pointer constant in C, and in fact every level of indirection can be constant or nonconstant, which is not the case in Java).

Ah, yes, you're right.
 
What's up all. Been lurking this thread for a while and wanted to come in and say hi and ask a few questions. I've been developing in java for the past 5 years, working on inhouse applications and creating some new ones as well. Well, an opportunity presented itself here at my current company to try something new and I jumped on it. So instead of dealing with Java, I want to try my hands at webwork. Namely, javascript and nodejs. HTML and Bootstrap among others. Currently I'm setting up my ubuntu enviornment, going through tutorials, examples, etc. It is pretty exciting doing something new but at the same time, intimidating.

Should be fun.
 
<rant>

I would have thought that if you're in a third year university computer science course you would at least have SOME understanding of how to program, or at least of programming concepts. It's more than halfway through the semester, and this guy in my group is still struggling with stuff like defining a function in Javascript, or creating tags in HTML. Really frustrating. (My favorite question, just a week or two ago, was "is our server Node.JS or HTML?")

</rant>
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Code:
<!DOCTYPE html>
<html>
     <head>
          <meta charset="UTF-8">
          <title>Nitpicking ahoy!</title>
     </head>
     <body>
          <p>Your rants aren't W3C compiliant.</p>
     </body>
</html>
 

CronoShot

Member
Got a (probably) stupid question. I'm trying to enter in user input into each row of a 2 dimensional character array, and it's not working so far.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUM_WORDS 10
#define LENGTH 20

void sort(char words[][LENGTH], int num);

int main(void)
{
    char words[NUM_WORDS][LENGTH] = {'\0'};
    int i = 0;
    
    do
    {
    for(i = 0; i < NUM_WORDS; i++)
    {
        printf("Enter a word: ");
        scanf("%s", words[i]);
    }
    }while(words[i] != 0 || i < NUM_WORDS);
    
    printf("Your sorted list is: %s", words);
    
    return 0;
}

It's not supposed to do anything yet outside of scanning in user input and printing it back, but my stupid self can't even get that to work. I'm getting this error:

Code:
warning: format specifies
      type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
    printf("Your sorted list is: %s", words);
                                 ~~   ^~~~~

on the line

Code:
printf("Your sorted list is: %s", words);

And I don't really know why. At least the update to Terminal in Mavericks is nice...
 
Code:
<!DOCTYPE html>
<html>
     <head>
          <meta charset="UTF-8">
          <title>Nitpicking ahoy!</title>
     </head>
     <body>
          <p>Your rants aren't W3C compiliant.</p>
     </body>
</html>
Ok, this was hilarious. SenseiJinx I feel your pain. I just got done with a program and one of the guys in our group didn't understand linked lists and thought we should be storing stuff in arrays. All I'll say is I haven't been in any entry level classes for a while.
 
Hey programming-GAF!

I'm taking a beginners class in python(0 knowledge from before) and so far it's been going pretty good.

Now I've got a problem though, today we got an assignment to create a phone book and one of the things you should be able to do is add one or multiple aliases to a contact and also change the number of the contact by using that alias.

So when executing the code it should look like:

Code:
> add keanu 12345
> lookup keanu
12345
> alias keanu neo
> lookup neo
12345
> change neo 54321
> lookup keanu
54321

The issue I'm having here is the alias part of it, not really sure how to do that. Anyone got any idea on how to implement it?
 

usea

Member
Hey programming-GAF!

I'm taking a beginners class in python(0 knowledge from before) and so far it's been going pretty good.

Now I've got a problem though, today we got an assignment to create a phone book and one of the things you should be able to do is add one or multiple aliases to a contact and also change the number of the contact by using that alias.

So when executing the code it should look like:

Code:
> add keanu 12345
> lookup keanu
12345
> alias keanu neo
> lookup neo
12345
> change neo 54321
> lookup keanu
54321

The issue I'm having here is the alias part of it, not really sure how to do that. Anyone got any idea on how to implement it?

I don't know python, but there's probably a bunch of ways to do this. I don't know what kind of stuff you've been doing in class either. Usually with assignments like this, the teacher has a particular solution in mind.

My first thought on a way to accomplish that would be two have two maps. Maps are sometimes called dictionaries. One map is for the the names and values you've added with the "add" command. ie "keanu -> 12345".
Then you'd have a second map for aliases, added with the alias command. ie: neo -> keanu.

Then, when you do commands such as "lookup" or "change" it would first look in the name/value map. If it didn't find a matching entry there, it would then look in the alias map. If it found a match in the alias map, it'd then look up the value of the alias in the name map.

There are questions like "what happens if you try to add an alias that already exists as a name, or add a name that's already an alias?" But if your assignment didn't specify, then it probably doesn't matter.
 

Ya no

Member
Hey GAF,

I'm learning java and have a pretty quick and simple question regarding conditional operators.

int num1 = 43;
int num2 = 23;
int num3 = 3;
int smallest;

smallest = (num1 <= num2) && (num1 <= num3) ? num1 : (num2 <= num3)? num2 : num3;

I had to write code to set the smallest of three integers to a separate variable using conditional/ternary operators instead of if-else. My question is how would this be read in plain english? I think I will get a better understanding of what's going on if I know how it would be read.

Is the ? essentially saying "do this" and the : saying "else do this/ else-if"
 

Sharp

Member
The best way to solve the above problem is actually with a reference chain. Basically, maintain just one map, from names to NameEntrys. There should be two types of NameEntrys: REFs, which contain a string (the name of another NameEntry to which it is aliased), and VALs, which contain a number (the ID of the name). When you alias a name you simply add a REF entry containing the name to which it is aliased to the dictionary.

To look up or change a name, you proceed as follows:
1. Grab the NameEntry associated with the name in dictionary.
2. If the NameEntry is a REF, set the name to the name of the REF and repeat step 1.
3. Otherwise, it is a VAL, so return (or change) the code.

This way, you can alias an alias, unlike the method proposed above. The problems it is asking about also disappear because there is no distinction between a name and an alias :)

(There is the possibility of the formation of what are called reference cycles under this scenario, where a chain of references is self-referential. You can make sure that a cycle can never form by disallowing the creation of aliases to names that don't exist yet).
 

phoenixyz

Member
Hey GAF,

I'm learning java and have a pretty quick and simple question regarding conditional operators.



I had to write code to set the smallest of three integers to a separate variable using conditional/ternary operators instead of if-else. My question is how would this be read in plain english? I think I will get a better understanding of what's going on if I know how it would be read.

Is the ? essentially saying "do this" and the : saying "else do this/ else-if"

Essentially

variable = a ? b : c

is

if(a)
variable = b
else
variable = c

in your example there's another ?: in the else clause.
 
Hey GAF,

I'm learning java and have a pretty quick and simple question regarding conditional operators.



I had to write code to set the smallest of three integers to a separate variable using conditional/ternary operators instead of if-else. My question is how would this be read in plain english? I think I will get a better understanding of what's going on if I know how it would be read.

Is the ? essentially saying "do this" and the : saying "else do this/ else-if"

smallest = (num1 <= num2) && (num1 <= num3) ? num1 : (num2 <= num3)? num2 : num3;


If num1 is less than or equal to num2 and num1 is less than or equal to num3, then smallest = num1, else if num2 is less than or equal to num3, then smallest = num2, else smallest = num3
 
Top Bottom