≡ Menu

Control Structures – How to Program with Java Podcast Episode 3

Control Structures


Here’s a breakdown of the Episode

Wiki: A control structure is a block of programming that analyzes variables and chooses a direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control “flows”). Hence it is the basic decision-making process in computing; flow controldetermines how a computer will respond when given certain conditions and parameters.

-Let’s describe that wiki statement in more human terms
– code read LINE BY LINE from top to bottom, and for the most part left to right) – CODE FLOW
– choose your own adventure… only you can have loops
– computer has a decision to make

– What are the different kinds of control structures
– IF statements
– Loops
– For
– While
– Do While

Your Homework

Please read this Java Tutorial on Control Structures to learn a bit about the actual syntax of the Java code for control structures.

If you need help, don’t hesitate to contact me at tpage@ecosim.ca.




{ 8 comments… add one }

  • Christine Butcher September 28, 2012, 3:55 pm

    what an awesome intro!

  • Felti November 1, 2012, 7:18 am

    Are for and while the only types of loops (Not Considering the IF Statements)? Is the Do While a type of loop aswell (Not Considering Other types that may be out there)? OR……are the 2 or 3 above not types of loops at all?? What types of loops are out there? Sorry for the unending questions by the way……

    • Trevor Page November 1, 2012, 3:52 pm

      for and while loops are both types of loops, and they are the most commonly used. Another type of loop is the do...while loop, but that is less often used.

      if statements are not loops, they are considered control structures.

      Hope that makes things nice and clear :)

  • Durim January 31, 2013, 6:46 pm

    Hi Trevor!

    I have a question for you :$
    Can you tell me please, why this code can’t be compiled ?! It says “Unreachable code” !

    public static void main(String[] args) {
           while ( false ) {
                     System.out.println("false");
         }
    }  
    

    I’m asking you, why we can’t write just the word “false” instead of a condition that produces a false result too ?!

    For example:

    public static void main(String[] args) {
            int a = 1;
            int b = 0;
            while ( a < b ) {
                      System.out.println("1 is less than 0");
            }
    }
    

    In this example, 1 is never less than 0, so it's false, right ?!
    So, why we can write it in this way and not like the former example (while (false) { ...) ?!

    Thank you soo much :)

    • Durim February 28, 2013, 10:02 am

      Hi Trevor!

      Is there any question here:$

      Thanks!

      • Trevor Page February 28, 2013, 3:42 pm

        Hey Durim,

        Sorry I missed your question! Must have been a busy day :S

        That’s actually a great question. The only answer I can give is that Java will know without any doubt that there’s no way that first “IF” statement will evaluate to true.

        Whereas the second “IF” statement has the possibility of being true (via more advanced methods like reflection or Aspects)

        • Durim March 1, 2013, 3:10 am

          Ahaa right, right:)
          Thank you so much, I get it now!
          See you in the future comments!

  • William October 9, 2013, 7:50 am

    Hi Trevor,

    I was just wondering about loops. Loved that podcast.
    So when would you use a do/while loop? Does a while loop not do the same thing?

    Is there any example of a problem that can only be solved with a do/while loop?

Leave a Comment

Powered by sweet Captcha

Next post:

Previous post: