Categories
discuss

Please explain the usage of Labeled Statements

  • Is breaking and continuing the only uses of labeled statements in Java?
  • When have you used Labeled Statements in your programs?

Sorry the code snippet has been deleted. I am splitting the question

Answer

JLS 14.7 Labeled statements

(edited for clarity)

Statements may have label prefixes (Identifier : Statement). The Identifier is declared to be the label of the immediately contained Statement.

Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break (§14.15) or continue (§14.16) statements appearing anywhere within the labeled statement.

So the JLS is clear that labels are used with break or continue, and no other grammatical element of the Java programming language uses it.

Strictly speaking, break and continue, labeled or not, are NEVER necessary. They can always be written out of the code. Used idiomatically, however, they can lead to more readable code.


Here’s an illustrative example: given an int[], we want to :

  • print "One (1)" on 1
  • print "Two (2)" on 2
  • print "Zero " on 0
  • immediately stop processing on any other number

    int[] arr = { 1, 2, 0, 1, -1, 0, 2 };
    loop:
    for (int num : arr) {
        switch (num) {
        case 1:
            System.out.print("One ");
            break;
        case 2:
            System.out.print("Two ");
            break;
        case 0:
            System.out.print("Zero ");
            continue loop;
        default:
            break loop;
        }
        System.out.print("(" + num + ") ");
    }
    // prints "One (1) Two (2) Zero One (1) "
    

Here we see that:

  • The different numbers are processed in a switch
  • Unlabeled break in the switch is used to avoid “fall-through” between cases
  • Labeled continue loop; is used to skip post-processing on case 0: (the label is not necessary here)
  • Labeled break loop; is used to terminate the loop on default: (the label is necessary here; otherwise it’s a switch break)

So labeled break/continue can also be used outside of nested loops; it can be used when a switch is nested inside a loop. More generally, it’s used when there are potentially multiple break/continue target, and you want to choose one that is not immediately enclosing the break/continue statement.


Here’s another example:

    morningRoutine: {
        phase1: eatBreakfast();
        if (grumpy) break morningRoutine;
        phase2: kissWife();
        phase3: hugChildren();
    }
    http://stackoverflow.com is the best website ever!

Here’s another case of a labeled break being used not within an iterative statement, but rather within a simple block statement. One may argue that the labels lead to better readability; this point is subjective.

And no, the last line DOES NOT give compile time error. It’s actually inspired by Java Puzzlers Puzzle 22: Dupe of URL. Unfortunately, the puzzle does not go into “proper” use of labeled statements in more depth.

Categories
discuss

Call javascript from MVC controller action

Can I call javascript function from MVC controller action (not from view page) and get return value? How?


I need to make request to server from code (.cs) using javascript like here (but this is aspx page)

function getInitData() { 
var code; code = 'return {' ;
code += 'me: API.getProfiles({uids: API.getVariable({key: 1280}), fields: "photo"})[0]'; 
code += '};'
VK.Api.call('execute', { 'code': code }, onGetInitData); 
} 

Answer

You can call a controller action from a JavaScript function but not vice-versa. How would the server know which client to target? The server simply responds to requests.

An example of calling a controller action from JavaScript (using the jQuery JavaScript library) in the response sent to the client.

$.ajax({
           type: "POST",
           url: "/Controller/Action", // the URL of the controller action method
           data: null, // optional data
           success: function(result) {
                // do something with result
           },                
           error : function(req, status, error) {
                // do something with error   
           }
       });
Categories
discuss

What is crtbegin.o and crtbegin_dynamic.o?

When debugging a link error (undefined reference to _dso_handle) using the Android x86 toolchain, I noticed it’s statically linking crtbegin_dynamic.o. What is the purpose of this file? There is another similar crtbegin.o in the toolchain install directory that contains the missing symbol (_dso_handle). What is the difference between crtbegin.o and crtbegin_dynamic.o?

Answer

I think they relate to dynamic libraries (shared objects) and executables, but not quite sure. Hope this provides you a lead.

Categories
discuss

Is there a way to determine if a

Pages: