X Tutup
Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 1.14 KB

File metadata and controls

34 lines (28 loc) · 1.14 KB

Changing the Control Flow

In many of these examples so far, we see a very simple control flow. The program starts at the first line, and just goes line by line until runs out of statements.

int q = 0;
int j = 5;
q = j * 4 - 20;
System.out.println(q); // -> 0

When programs start to get more sophisticated, the control flow can be changed to perform different tasks. There are various conditional statements, loop statements, and functions that can cause the control flow to move around within the code. Here we see using both a loop and a conditional IF statement to change the flow of control.

int q = 0;
int j = 6;
while (j > 0) {
    q = j * 4 - 20;
    System.out.println(q);
    j--;
    if (q > 0) {
        System.out.println("q is still positive");
    }
}

This ability to manipulate the control flow of a program is very important when you start developing logic for your apps and programs. Logic in programs depends heavily on being able to manipulate the control flows through the code. Let’s take a look at how each kind of statement allows a programmer to change the flow of control in programs.

X Tutup