The order in which statements are executed is called a control structure, and there are mainly "** sequential ", " branch ", and " repeat **".
[Conditional expression] An expression showing a branch condition or a condition to continue repeating [Block] A set of statements to be executed by branching or repeating
python
・ Branch syntax
if(Conditional expression) {
block
} else {
block
}
-Repeat syntax
while(Conditional expression){
block
}
・ Three types of if syntax
python
▪︎if-else syntax(Uninflected word)
//Example
if(age >= 20) {
canDrink = true; //If the condition is true, the processing of this block works.
} else {
canDrink = false; //If the condition is false, the processing of this block works.
}
▪︎ if only syntax
//Example
if( age >= 20) {
canDrink = true;
}
▪︎if-else if-else syntax
//Example
if( height >= 170) {
size = 'L';
} else if(height >= 155){
size = 'M';
} else if(height >= 140){
size = 'S';
} else {
size = '?';
}
Conditions that can be rewritten as a switch statement (1) All conditional expressions can only be used to compare whether the left and right sides match, such as "variable == value" and "variable == variable" (<,>,! =, Etc. cannot be used) (2) The value to be compared is an integer (byte type, short type, int type), character string (String type) or character (char type), not a minority or boolean value.
python
//Example
switch(fortune){
case 1:
System.out.println("Daikichi");
break;
case 2:
System.out.println("Nakayoshi");
break;
case 3:
System.out.println("Kichi");
break;
default: //What to do if none of the cases apply(Can be omitted if not needed)
System.out.println("Bad");
}
-Since the switch statement only jumps to the applicable case, if break is not described, the program will be read in sequence after jumping to a specific case.
・ Two types of while statements
python
▪︎ while syntax(Uninflected word)
//Example
while(temp > 25) {
temp--;
System.out.println("I lowered the temperature by 1 degree");
}
▪︎do-while syntax
//Example
do{
temp--;
System.out.println("I lowered the temperature by 1 degree");
} while(temp > 25);
・ Repeat by for statement
python
for (Unter variable initialization process;Repeat condition;Increase / decrease processing of counter variables) {
Repeated processing
}
//Example
for( int i = 0; i < 10; i++) {
System.out.println(i);
}
//The variable i is initialized and 0 is assigned. Process in the block until i is greater than 10.
//I every time processing++(Add 1 to the variable i)The process is performed.
Such multiple structures such as "branch in branch" and "branch in repetition" are called "** nested " and " nested **".
python
▪︎ Branch processing in branch processing
//Example
if(height > 170){
if(eye > 1.0){
System.out.println("Pass");
}
}
▪︎ Branch processing in repetition
//Example
do{
if(i % # == 0){
System.out.println(i);
}
i++
} while(i < 100);
・ Break statement (interrupts the repetition itself)
python
for(int i = 1; i < 10; i++) {
if(i == 3){
break;
}
System.out.println(i);
}
・ Continue statement (interrupt only this sentence and go to the next lap)
python
for(int i = 1; i < 10; i++) {
if(i == 3){
continue;
}
System.out.println(i);
}
Recommended Posts