The perfect online platform for artists and freelancers

Whether it be traveling, making music, or writing, everyone has a passion for something that they love. So why not share that passion with the rest of the world? Freelance Frontier is unlike any other content sharing site out there. Most websites allow you to share your content for their own profit. At Freelance Frontier we believe that the users are the ones doing the work and therefore deserve a share of the profit. Getting started is easy and free. We hope to see your content soon!

Build a Portfolio

Build your online portfolio. Share your posts, resume, education and more to stand out. Integrate external media such as Facebook and LinkedIn to showcase even more about yourself.

Robust Services

Users can enjoy a wide variety of free services such as audience geolocation, published work reports, in-app messaging and more. Stay in the loop with other freelancers like yourself.

Fast & Simple

Getting started is easy and free. Instantly begin uploading your work and track how it performs against a variety of metrics. Easy to use tools allow you to quickly extract the information you want.

Notifications

Receive push notifications on your dashboard to get alerted to things that matter. Stay on top of important events such as messages from potential clients, employers, post performance and a whole lot more.

If We Win, Our Users Win


Earn a percentage of the organization's revenue share based on metrics such as your post like count, comment count and more. We will keep all users notified of when the organization will be able to make payments. One-third of the organization's revenue will always be allocated for our freelancers. We will always be investing in ways to grow the organization and provide more free services to our great community.


Stripe Payments through the Freelance Frontier
Freelance Frontier Mobile Mockup

Mobile Friendly

Publish content or analyze your audience on the go with our responsive UI. Connect with like minded entrepreneurs from across the globe. Keep a lookout for the mobile app. For more info, get in touch with us today.

Freelance Frontier iPad Mockup

Using Program Control Structures in Java

by Matt Daronco | 2016-02-08

Views Today: 0
Views All Time: 1472
Likes: 19
Comments: 0
Using Program Control Structures in Java

        Control structures are a key and characteristic component to any programming language. Both compiled and interpreted languages contain embedded control structure syntax in order to give a much wider spectrum of functionality. We will run through several built in control structures in the Java programming language, however you should take note note that the syntax is applicable across several other languages as well, including C and C++.

  • If-Else Statements

            If-else statements allow you to configure your program to execute code blocks only if a certain condition is met. For example, you could print different text to the console depending upon the age of a particular application user:

    
    class ControlDemo {
        public static void main(String[] args) {
            String userName = "John Doe";
            int userAge = 20;
            // only do this if the user is young enough
            if (userAge < 20) {
                System.out.println("Hello, " + userName);
            } else {
                System.out.println("Hello, Mr. " + userName);
            }
        }
    }
                            

    Output of the code snippet below:

    Hello, Mr. John Doe

            It is not necessary to specify an else clause in your control structure, if you don't need to defer to some other action. Of course, you can nest if-else statements within each other. For example, we can nest if statements here if we want to also check for gender:

    
    class ControlDemo {
        public static void main(String[] args) {
            String userName = "John Doe";
            boolean isMale = true;
            int userAge = 20;
    
            if (userAge < 20) {
                System.out.println("Hello, " + userName);
            } else {
                if (isMale) {
                    System.out.println("Hello, Mr." + userName);
                } else {
                    System.out.println("Hello, Ms." + userName);
                }
            }
        }
    }
                            

            Here is a list of some conditional operators that you can use within if-else blocks:

    1.   <

              The less-than operator. This operator tests to see whether the left side value is less than the right side value.

    2.   >

              The greater-than operator. This operator tests to see whether the right hand side is greater in value than the left hand side value.

    3.   ==

              The equal to operator. This operator tests to see whether two values are equal or not.

    4.   !=

              The not equal to operator. This operator tests to see whether the left value does not equal the right side value.

    5.   &&

              The AND operator. This operator results in TRUE if both conditions are met. Example:

      
      int valueOne = 20;
      int valueTwo = 25;
      
      if (valueOne > 18 && valueTwo > 18) {
          System.out.println("Both ages are over 18."); // this line will execute in this case
      }
                                      

              In the code snippet above, the computer hardware transmits digital electrical signals to an AND gate in order to perform the conditional operation. See a chip diagram containing 2-input AND gates below:

      Image Source

      AND Gate Diagram

              In the image above, consider input wires 12 and 13. These two input wires feed into an AND gate, and their resulting current is outputted to wire 11. See a truth table below for all possible combinations of I/O for each individual AND gate:

      Wire 12 (input) Wire 13 (input) Wire 11 (output)
      0 0 0
      0 1 0
      1 0 0
      1 1 1

              As the truth table displays above, we can see that in only one case (the fourth row) will the output of the AND gate possess current (a binary value of '1').

    6.   ||

              The OR operator. This operator tests to see if only one of the two conditions passes. Example:

      
      int valOne = 20;
      int valTwo = 10;
      
      if (valOne > 18 || valTwo > 18) {
          System.out.println("One of the ages is over 18."); // this line will execute in this case
      }
                                      

              Within the system hardware, OR evaluations are performed by sending digital electrical signals to an OR gate. For example, consider the circuit below:

      Image Source

      Or Gate Circuit

              In the image above, consider the wires labeled 12 and 13. Here is a truth table for all possible input cases to the corresponding OR gate (note that in digital electronics a value of '1' is roughly equivalent to ~5V of current, whereas a binary value of '0' is roughly ~0V of current):

      Wire 12 (Input) Wire 13 (Input) Wire 11 (Output)
      0 0 0
      0 1 1
      1 0 1
      1 1 1

              Thus, the output for a two-input OR gate will be ON (also known as '1' in positive logic) in three out of the four possible combinations.

    7.   !

              The NOT operator. This operator logically flips the value of any expression or simplified value. It is often referred to as a bit-wise operator, since it only takes one binary digit to store a Boolean value ('0' or '1' => FALSE or TRUE).

              The expression !1, or equivalently !TRUE, will result in 0, or FALSE. Within the computer hardware, a NOT gate (also referred to as an inverter) is used to reverse the current in a given section of a circuit. See the inverter diagram below:

      Image Source

      Inverter Circuit Diagam

              The truth table for the inverter is very simple, since it just outputs the reverse state of the input (!A is read as "A-not"):

      A (input) !A (output)
      0 1
      1 0

              It is interesting to note that an AND gate is actually an NAND gate with an extra inverter (since its cheaper to manufacture).

  • Else-If Statements

            This control structure is similar to the if-else control statement, yet provide conditional operations for multiple different cases. See an example below, where the program input is read in from a command line client:

    
    public class ControlExample {
        // this program will take the lottery player's code from command line as input from the user
        public static void main(String[] args) {
            if (args.length > 0) { // check to see if the user even entered a draw
                int lotteryWinningCode = 122524;
                int lotteryPlayerDraw = Integer.parseInt(args[0]); // the program takes only one command line argument, which goes to the
                // first index of the 'args' String array
    
                if (lotteryPlayerDraw < lotteryWinningCode) {
                    System.out.println("Sorry, you undershot the winning numbers!  Please try again.");
                } else if (lotteryPlayerDraw > lotteryWinningCode) {
                    System.out.println("Sorry, you over-shot the winning numbers!  Please try again.");
                } else if (lotteryPlayerDraw == 0) {
                    System.out.println("That was a lousy guess...  :|");
                } else { // you can add as many 'else if' calls as you want
                     System.out.println("Congratulations, you have won the lottery!");
                }
            } else {
                System.out.println("You must enter a number in order to play the lottery!");
            }
        }
    }
                            

            In the program above, let's assume the user has entered the number 120005. In this case, the output of the program would result in:

    Sorry, you undershot the winning numbers!  Please try again.
                        

            The program will exit the else-if control structure once it evaluates the first condition, since the user's draw was less than the winning number.

  • The Switch Statement

            The switch statement is usually used when the program has multiple execution paths possible, given a certain variable which is "switched" (compared to many values). Think of a switch statement as a fork road, metaphorically. See an example Java switch statement below:

    
    public class SwitchDemo {
        public static void main(String[] args) {
            int employeeType = 3;
            switch (employeeType) { // each condition is evaluated within a 'case' block
                 case 1:
                    System.out.println("Employee type is manager.");
                    break;
                 case 2:
                    System.out.println("Employee type is engineer.");
                    break;
                 case 3:
                    System.out.println("Employee type is tenant.");
                    break;
                 case 4:
                    System.out.println("Employee type is designer.");
                    break;
                 default:
                    System.out.println("Error: no employee type associated with that code.");
                    break;
            }
        }
    }
                            

            In the program above, the vlaue of the integer employeeType is compared against the value specified after the case keyword. The break keyword tells Java to "jump-out" of the switch statement and continue program execution immediately after the switch statement closing curly brace. The program above outputs the following to the console:

    Employee type is tenant.

            If none of the cases resolve to true, then the optional default execution block is triggered, which is set using the default keyword.

  • The For Loop

            This control structure is found in numerous programming languages. It allows the programmer to configure an iterative process within a program. The loop incrementation is performed within the for loop parentheses. For example, if we wanted to run a loop 10 times, printing the square of the incremented integer inc during each loop, we could write this:

    
    public class ForLoopDemo {
        public static void main(String[] args) {
            int square;
    
            for (int i = 0; i < 10; i++) {
                square = i*i;
                System.out.println("Current value of inc^2: " + Integer.toString(square));
            } // the program will repeat the code within the for loop braces 10 times.  During each iteration, the
              // variable inc is incremented by 1.  So on the third loop, for example, inc = 2 (0-based incrementing)
        }
    }
                            

    Output of the for loop program above:

    Current value of inc^2: 0
    Current value of inc^2: 1
    Current value of inc^2: 4
    Current value of inc^2: 9
    Current value of inc^2: 16
    Current value of inc^2: 25
    Current value of inc^2: 36
    Current value of inc^2: 49
    Current value of inc^2: 64
    Current value of inc^2: 81
                        
  • The While Loop

            The while loop is another iterative control structure. It continues to run while the loop condition is held. The loop incrementation is defined within the curly braces enclosing the while block. See an example below, where the while loop will execute 5 times:

    
    public class WhileLoopDemo {
        public static void main(String[] args) {
            int inc = 0;
    
            while (inc < 5) {
                System.out.println("The current value of inc: " + Integer.toString(inc));
                inc++; // shorthand for (inc = inc + 1).  Note how we increment the counter variable inside of the curly braces
            }
        }
    }
                            

            The output of the program above would look like this:

    The current value of inc: 0
    The current value of inc: 1
    The current value of inc: 2
    The current value of inc: 3
    The current value of inc: 4
                        
  • The Do-While Loop

            The do-while loop is another iterative structure. It's essentially the while loop with a twist: no matter what, Java will "do" the first iteration fo the loop, then check to see if the loop condition still holds true at the bottom of the control structure. See an example below, where we iterate 5 times:

    
    public class DoWhileLoopDemo {
        public static void main(String[] args) {
            int inc = 0;
    
            do {
               System.out.println("The current value of inc: " + Integer.toString(inc));
               inc++;
            } while (inc < 5);
        }
    }
                            

            The output of the program above will be identical to the previous example for the while control structure.

Click to Take Quiz

Test Yourself

        There are several more applications and topics that could be covered concerning program control structures. For additional help, consultation or more, don't hesitate to contact us.

No recent articles to show.
1.  
Using Command Line Package Managers

Matt Daronco - 2016-01-21

  Programming

2.  
You'll Never Be Without - EP

Nathan Turner - 2016-01-20

  Music & Video

3.  
Implementing a Hash Table in C++

Matt Daronco - 2016-01-16

  Programming

4.  
5.  
9 Things You Should Know About Go

Matt Daronco - 2016-01-25

  Programming

6.  
What is Data Binding?

Matt Daronco - 2016-01-30

  Programming

7.  
Implementing Binary Trees in C

Matt Daronco - 2016-01-22

  Programming

10.  
Using Arrays in Java

Matt Daronco - 2016-02-09

  Programming

11.  
12.  
Class Inheritance in Java

Matt Daronco - 2016-02-22

  Programming

1.  
Implementing a Hash Table in C++

Matt Daronco - 2016-01-16

Views today: 3

  Programming

2.  
Creating a Search Engine with PHP and jQuery

Matt Daronco - 2016-01-15

Views today: 2

  Programming

3.  
You'll Never Be Without - EP

Nathan Turner - 2016-01-20

Views today: 2

  Music & Video

4.  
Leveraging Browser Caching

Matt Daronco - 2016-01-22

Views today: 2

  Programming

5.  
Short on Boring, Big on Laughs

Ryan Emerson - 2016-01-22

Views today: 2

  Writing & Miscellaneous

6.  
Implementing Binary Trees in C

Matt Daronco - 2016-01-22

Views today: 2

  Programming

7.  
My Journey Abroad in Rotorua, New Zealand: Gallery

Kendra Kubo - 2016-04-03

Views today: 1

  Photography

8.  
Understanding the Client-Server Model of Computing

Matt Daronco - 2016-01-16

Views today: 1

  Programming

9.  
Interviewing an Artist

Matt Daronco - 2015-12-04

Views today: 1

  Writing & Miscellaneous

10.  
5 SEO Tips for your Website

Matt Daronco - 2016-01-15

Views today: 1

  Programming

11.  
Sustainable Development

Terry Roberts - 2016-01-12

Views today: 1

  Government & Politics

12.  
Using Command Line Package Managers

Matt Daronco - 2016-01-21

Views today: 1

  Programming

Go Pro Shots

(swipe up on mobile to close after enlarging)