Tutorial 2: Basics: int, bool, condition, loop, etc.


Overview on this Tutorial:

1. Output Text to Console (Basics1.java)

2. Integer and Integer Operation (Basics2.java)

3. Comparison Operators, Boolean Type and Condition Statement (Basics3.java)

4. Loop (Basic4.java)


Output Text to Console (Basics1.java):

In this and next tutorial, we firstly concentrate on the code inside the function "startApp()", that is:

public void startApp()
{
    // Consider the codes between two curvy bracket only.
}

We will talk about the detail of function in Java later. In this program, you can notice that:

1. Text below "//" at a line is command.

2. Text between "/*" and "*/" are command.

3. Every function has a pair of curvy bracket ("{" and "}"), and the codes of a function lies between the pair of curvy bracket.

4. The codes in "startApp( )" will execute first (similar to "main" function/Program in C/C++/Pascal).

5. There is one line of code inside "startApp( )":

System.out.println("Hello World!");

6. "System.out.println( )" is a function to output text to console. The function takes one parameter, which is a String of text. The details of String will be discuss later.

7. You can also use "System.out.print( )" which does not include "end of line" after outputting the text.

Practice: You can try to add more "System.out.println(...)" and "System.out.print(...)" in the program.

 


Integer and Integer Operation (Basics2.java):

We consider the codes in "startApp()". Notice that:

1. We can declare an integer variable using "int".

int x;
int y, z;

2. We can assign a value to an variable using the operator "=".

x=1;

3. We can declare variable and assign value in the same line.

int a=7;

4. You might notice that every line of code in Java is ended by ";".

5. Here is the table showing the arithmetic operators for "int":

Operators Example Meaning
+ y + z y plus z
- z - y z minus y
* b * c b times c
/ a / y Quotient when a divide y
% a % y Reminder when a divide y
++

x++ or ++x

Increment x by 1 (x = x+1)
-- x-- or --x Decrement x by 1 (x = x-1)
+ +1 Positive one (Same as 1)
- -2 Negative two

Note that all the operations return "int".

Practice: You can try to use the "++" and "--" operation in the program.


Comparison Operators, Boolean Type and Condition Statement (Basics3.java):

1. Here is the table showing the comparison operators:

Operators Example Return "true" when:
> x > y x is greater than y
>= x >= y x is greater than or equal to y
< x < y x is less than y
<= x <= y x is less than or equal to y
== x == y x is equal to y
!=

x != y

x is not equal to y

2. In order to execute some codes based on a condition. You can use "if" or "if-else" statement.

if (<A boolean variable or an expression>) {
    /* Codes here will execute if the condition is true. */
}

if (<A boolean variable or an expression>) {
    /* Codes here will execute if the condition is true. */
}
else {
    /* Codes here will execute if the condition is false. */
}

Practice: You can try to use all the comparison operations in the program using "if" or "if-else" statements.

4. Apart from "int", another data type in Java is "boolean". You can declare a boolean variable and assign value to it.

boolean t = true;
boolean f = false;

Note that the only possible values for boolean are "true" and "false".

3. Here is the table showing the logical operations for boolean:

Operators Example Meaning
&& x && y x and y
|| x || y x or y
! !x not x

Practice: Assume num1, num2 & num3 are three integer variables, you can try write a few line of codes to print out the largest value among the three integers.


Loop (Basic4.java):

There are times that you want to repeatedly run some lines of code until some condition is satisfied. In such cases you can use a loop to do that. There are 3 main kinds of loops:

  1. while loop
    This is a loop that starts from checking the condition. If the condition is true, the code block in the loop will be executed once. Then the condition is checked again to decide whether the code block will be executed again. The loop terminates when the condition is false.

    Format:
    // While loop
    while (<A boolean variable or an expression>) {
        /* Codes here will execute whenever the condition is true. */
    }
    

    Example:
    // While loop
    int i = 2;
    while (i < 100) {
       System.out.print(i + " ");
       i = i * 2;
    }
    System.out.println();
    

  2. do-while loop
    The do-while loop is almost the same as a while loop, with the only excpetion that it executes the content of the loop once before the check.

    Format:
    // Do-while loop
    do {
       /* Codes here will execute whenever the condition is true. */
    } while (<A boolean variable or an expression>);
    

    Example:
    // Do-while loop
    i = 1;
    do {
       i = i * 2;
       System.out.print(i + " ");
       } while (i < 100);
    System.out.println();
    

    Note: the codes in do-while loop will execute as least once which is different from while statement.

    Practice: Trace the output before executing the program.

  3. for loop
    The for loop allows you to initialize and update some counters togther with the looping process.

    Format:
    for(<Initial statment>; <Condition>; <Increment statement>) {
       /* Codes here will execute whenever the Condition is true. */
    }
    

    Example:
    for (i=0; i<10; i++) {
       System.out.print(i + " ");
    }
    System.out.println();
    

    Which is same as the following code using "while" loop:

    i=0;
    while (i<10) {
       System.out.print(i + " ");
       i++;
    }
    System.out.println();
    

Practice: Assume there is an integer called "length", output an right angle triangle using "*" with the length equals to variable "length". Here is the sample output when "length" is 5.

*
**
***
****
*****