Programming For Non-Techies - Control Structures

Programming For Non-Techies - Control Structures

Programming For Non-Techies is a weekly feature for non-techies to learn to program every Friday.  If it’s your first time here please review the first lesson to get caught up.  If you’re a non-techie and you want to learn to program, or maybe you want a job in tech and need a better understanding of programming, these posts will help you!

Control Structures

Code is organized into blocks.  A block is simply a logical grouping of code.  You will see that some languages identify a block by using punctuation like curly braces.  The way we decide which blocks of code are executed is through control structures.  The main control structures are If-Then, If-Then-Else, For Loops, While Loops and Do-While Loops.  Keep reading for brief descriptions of how each works.  Look at the example code at the end of the post to see these implemented in Java.

If-Then

This is as basic as you can get with control structures.  It will check if a condition is true then do something.

If-Then-Else

This expands on if-then and allows you to create multiple logic flows.  As before, it will check if a condition is true then do something.  If not, do something else.

When you look at the example code, you will see that there are two ‘if’ statements.  You can add as many as you need.  For now.  Overdoing this can become an issue as you progress.

For Loop

For loops allow you to iterate, or loop, through a range of values.

While Loop

The while loop continually executes a block of code while a given condition is true.  If the condition is false, the block is skipped.

Do While Loop

This is a lot like a while loop.  The big difference is that it checks the condition at the end of the block.  This results in executing the block at least once.

Switch Statement

This will look for a certain value and execute a block of code.  Notice that a default can be defined.  This is a catch-all that will execute if nothing else matches.

Let’s look at code

In all of these control structures there is some condition that is evaluated.  If that condition isn’t true then in most cases the entire block is skipped.  The exception is the do-while loop.  Since it evaluates the condition at the end, it will execute the code once before it checks.

Experiment with the example code in Eclipse.  Specifically, change the numbers that are passed in.  Look at line 13 which has example.forLoop(3);.  Change the 3 to a 5 or a 10.  Do this for all the examples and see what happens.  There’s a lot going on in the example program and we will build on it over the next few weeks.

If you haven’t set up Eclipse, please refer to last week’s post.

 

public class ExampleProgram {

	public static void main(String[] args) {
		ExampleProgram example = new ExampleProgram();

		System.out.println("ifThen");
		example.ifThen(1);

		System.out.println("\nifThenElse");
		example.ifThenElse(2);

		System.out.println("\nforLoop");
		example.forLoop(3);

		System.out.println("\nwhileLoop");
		example.whileLoop(4);

		System.out.println("\ndoWhileLoop");
		example.doWhileLoop(5);

		System.out.println("\nswitchStatement");
		example.switchStatement(6);
	}

	private void ifThen(int x) {
		if (x > 5) {
			System.out.println("greater than 5");
		}
	}

	private void ifThenElse(int x) {
		if (x > 5) {
			System.out.println("greater than 5");
		} else if (x > 10) {
			System.out.println("greater than 10");
		} else {
			System.out.println("5 or less");
		}
	}

	private void forLoop(int x) {
		for (int i = 0; i < x; i++) {
			System.out.println("forLoop: " + i);
		}
	}

	private void whileLoop(int x) {
		while (x < 10) {
			System.out.println("x = " + x);
			x++;
		}
	}

	private void doWhileLoop(int x) {
		do {
			System.out.println("x = " + x);
			x++;
		} while (x &amp;lt; 10);
	}

	private void switchStatement(int x) {
		switch (x) {
		case 1:
			System.out.println("one");
			break;
		case 2:
			System.out.println("two");
			break;
		case 3:
			System.out.println("three");
			break;
		case 4:
			System.out.println("four");
			break;
		case 5:
			System.out.println("five");
			break;
		case 6:
			System.out.println("six");
			break;
		case 7:
			System.out.println("seven");
			break;
		case 8:
			System.out.println("eight");
			break;
		case 9:
			System.out.println("nine");
			break;
		case 10:
			System.out.println("ten");
			break;
		default:
			System.out.println("error");
			break;
		}
	}
}

Leave a Comment