Control Structures – How to Program with Java Podcast Episode 3

by Trevor Page on September 26, 2012

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.




Top 7 Tools Java Professionals Use Daily
The best part is, 85% of these tools are FREE
Download this 1-page PDF that will show you exactly how to get your hands on all these tools today.

{ 8 comments… read them below or add one }

Christine Butcher September 28, 2012 at 3:55 pm

what an awesome intro!

Reply

Felti November 1, 2012 at 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……

Reply

Trevor Page November 1, 2012 at 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 :)

Reply

Durim January 31, 2013 at 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 :)

Reply

Durim February 28, 2013 at 10:02 am

Hi Trevor!

Is there any question here:$

Thanks!

Reply

Trevor Page February 28, 2013 at 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)

Reply

Durim March 1, 2013 at 3:10 am

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

Reply

William October 9, 2013 at 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?

Reply

Leave a Comment

Powered by sweet Captcha

Previous post:

Next post: