Basics of Java development ~ How to write a program (flow and conditional branching) ~

Basics of Java development ~ How to write a program (flow and conditional branching) ~

Previous article Basics of Java Development ~ How to Write Programs (Variables and Types) ~ is a continuation. Continue to use the program that adds 1 to 10.


package lessons;
public class L1 {
    public static void main(String[] args) {
        int startNum=1;
        int endNum=10;
        int resultNum=0;
        int count=0;
        while(startNum<=endNum) {
            resultNum=startNum+resultNum;
            startNum=++startNum;
            ++count;
        }
        startNum=startNum-count;

        System.out.println(startNum+"From"+endNum+"The result of adding up to"+resultNum);
    }
}

Algorithms and flowcharts

It's a bit off the beaten track, but I'll explain what the flowchart looks like. A program is something that flows from above like water. Like water, we will take various paths. Think ahead of time what path you should take before programming. The thought path is called Algrism </ b>. Flowcharts are used to share algorithms with others. Please refer to this for the flowchart.


Let's create a flowchart of the program that adds 1 to 10 this time. Untitled Diagram.png


When programming, just apply this to the rules of the language as it is !! In actual development, it may not be possible to draw a flow diagram very neatly, but without this idea, programming is not possible. Even if you can, it will be full of holes, so be especially aware of it at first.


Conditional expression

In the example used this time, the conditional expression of iterative processing called while is used. It is used in the following format.

while(<Conditional expression>){
Contents of processing
}

Process until the conditions are met. So, for example, if you perform the following processing, the processing will never end and the computer will stop.

int a = 1;
int b = 2;
while(a == b){
    System.out.println("It won't end");
}

Judgment of conditions is basically the same as mathematics. However, as you can see in the above example, in the case of equality, two are connected with ==. Returns true </ b> if the condition is correct, false1 </ b> if the condition is incorrect. There are some things that are different from mathematics in usage, so I will introduce them a little.

operator meaning
a == b Returns true if a and b are the same
a != b Returns true if a and b are different
a <> b Returns true if a and b are different

As you may have already noticed, the program is true </ font> </ b> or false. Only </ font> </ b> can be returned. So be sure to set the conditional branch to YES NO problem </ font> </ b>.


I will introduce if because it is a condition judgment formula that is often used.

//Conditional branch by if
if(<Conditional expression>){
   //Processing in the case of ture
}

Conditional branch by if else
if(<Conditional expression 1>){
   //<Conditional expression 1>Processing when is ture
   //If the conditions here are met, no further condition judgments will be made.
}else if(<Conditional expression 2>){
   //<Conditional expression 2>Processing when is ture
   //If the conditions here are met, no further condition judgments will be made.
}else{
   //Condition 1,If none of 2 applies
}

We will also introduce a case that handles multiple conditions. For example, if you are hungry and have money, let's make the program go to a convenience store. With the knowledge so far, in this case you can write like this.

if(I'm hungry){
   if(Have money){
Go to a convenience store
   }
}

This can be written in one line.

if(I'm hungry&&Have money){
Go to a convenience store
}

In a similar way, it's a modern idea, but if you're hungry or thirsty, go to a convenience store can be expressed as follows.

if(I'm hungry||Thirsty){
Go to a convenience store
}

This is summarized in the list below. This is part of the story of a logical formula, but it will be long, so please check it yourself.

1 2
a && b Returns true if a is true and b is true
a

Confirmation check

  • Let's write a program that adds multiples of 3 as many times as you like (you can use for instead of while. Rather, that is easier ...) (3+6+9+12+...+3n) The output result is "Z when a multiple of 3 is added Y times from X" (X, Y, Z are integers)

  • Think about what the next program is trying to do and fix what is wrong.

In writing

Recommended Posts