Java의 순환 제어 문장과 조건 판단 문장의 사용을 전면적으로 파악하다
우리가 실행해야 할 코드 블록이 몇 번이고 순환이라고 불리는 경우가 있을 수 있다.
Java는 매우 유연한 삼순환 메커니즘을 가지고 있다.다음 세 가지 순환 중 하나를 사용할 수 있습니다.
while 순환
while 순환은 반복할 수 있는 특정 작업 횟수를 제어하는 구조입니다.
문법
while 순환의 문법은:
while(Boolean_expression)
{
  //Statements
}
여기서 while 순환의 관건은 순환이 영원히 실행되지 않을 수도 있다는 것이다.표현식이 테스트를 진행하면 결과는 가짜이고 순환체는 건너뛰며while 순환 후 첫 번째 문장이 실행됩니다.
예제
public class Test {
  public static void main(String args[]) {
   int x = 10;
   while( x < 20 ) {
     System.out.print("value of x : " + x );
     x++;
     System.out.print("
");
   }
  }
}
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
do ... while 순환은while 순환과 유사하며, 다른 것은 하나의...while 순환은 적어도 한 번은 실행할 것을 보증합니다.
문법
do...while 순환의 문법은:
do
{
  //Statements
} while (Boolean_expression);
만약 볼 표현식이 사실이라면, 흐르는 것을 제어하고, 순환 중인 문장을 다시 실행합니다.이 과정은 볼 표현식이 가짜가 될 때까지 반복적으로 진행된다.
예제
public class Test {
  public static void main(String args[]){
   int x = 10;
   do{
     System.out.print("value of x : " + x );
     x++;
     System.out.print("
");
   }while( x < 20 );
  }
}
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
for 순환은 순환 제어 구조로 실행해야 할 특정 횟수의 순환을 효과적으로 작성할 수 있다.
한 임무가 몇 번 반복될지 알 때 for순환이 좋다.
문법
for 순환의 구문은 다음과 같습니다.
for(initialization; Boolean_expression; update)
{
  //Statements
}
초기화 단계는 먼저 실행되고 한 번만 실행됩니다.이 절차는 모든 순환 제어 변수를 설명하고 초기화할 수 있습니다.성명 하나를 여기에 둘 필요는 없고, 단지 하나의 분호만 나타나면 된다.
다음으로, 볼 표현식 값을 구합니다.true라면 순환체를 실행합니다.false라면 순환체가 실행되지 않고 프로세스 제어가 for순환을 거친 다음 문장으로 넘어갑니다.
이후 순환체는 for순환이 실행될 때 제어 프로세스가 업데이트 문장으로 이동합니다.이 문장은 순환 제어 변수를 업데이트할 수 있습니다.이 문장은 부울 표현식 뒤에 분호가 나타나면 비어 있을 수 있다.
볼 표현식은 지금 다시 계산을 평가한다.true라면, 순환해서 실행하고, 이 과정을 반복합니다. (순환체, 그리고 업데이트된 절차, 그리고 볼 표현식)이후 볼 표현식은false이고 순환이 종료됩니다.
예제
public class Test {
  public static void main(String args[]) {
   for(int x = 10; x < 20; x = x+1) {
     System.out.print("value of x : " + x );
     System.out.print("
");
   }
  }
}
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
Java5까지 향상된 for 사이클에 대해 설명했습니다.이것은 주로 수조에 쓰인다.
문법
향상된 for 순환 구문은 다음과 같습니다.
for(declaration : expression)
{
  //Statements
}
표현: 이 계산 결과가 완성되려면 순환수 그룹이 필요합니다.표현식은 하나의 수조 변수나 하나의 수조를 되돌려주는 방법으로 호출될 수 있다.
예제
public class Test {
  public static void main(String args[]){
   int [] numbers = {10, 20, 30, 40, 50};
   for(int x : numbers ){
     System.out.print( x );
     System.out.print(",");
   }
   System.out.print("
");
   String [] names ={"James", "Larry", "Tom", "Lacy"};
   for( String name : names ) {
     System.out.print( name );
     System.out.print(",");
   }
  }
}
10,20,30,40,50,
James,Larry,Tom,Lacy,
키워드break는 전체 순환을 멈추는 데 사용됩니다.break 키워드는 모든 순환이나 스위치 문장에 사용해야 합니다.
키워드break는 가장 안쪽 순환의 실행을 멈추고 블록 뒤에 있는 다음 코드를 실행합니다.
문법
break 문법은 모든 순환의 단독 문장입니다.
break
public class Test {
  public static void main(String args[]) {
   int [] numbers = {10, 20, 30, 40, 50};
   for(int x : numbers ) {
     if( x == 30 ) {
     break;
     }
     System.out.print( x );
     System.out.print("
");
   }
  }
}
10
20
continue 키워드는 임의의 링 제어 구조에서 사용할 수 있습니다.그것은 순환을 즉시 순환의 다음 교체로 전환시켰다.
for 순환에서continue 키워드는 컨트롤 흐름이 업데이트 문장으로 바로 넘어갈 수 있습니다.
while 순환이나do/while 순환에서 제어 흐름은 즉시 볼 표현식으로 넘어갑니다.
문법
continue 구문은 모든 순환의 개별 구문입니다.
continue
  public static void main(String args[]) {
   int [] numbers = {10, 20, 30, 40, 50};
   for(int x : numbers ) {
     if( x == 30 ) {
     continue;
     }
     System.out.print( x );
     System.out.print("
");
   }
  }
}
10
20
40
50
Java에는 다음과 같은 두 가지 유형의 조건부 판단 문구가 있습니다.
.if 문장
if 문장은 하나의 볼 표현식 뒤에 하나 이상의 문장으로 구성된다.
문법
if 문장의 구문은 다음과 같습니다.
if(Boolean_expression)
{
  //Statements will execute if the Boolean expression is true
}
예제
public class Test {
  public static void main(String args[]){
   int x = 10;
   if( x < 20 ){
     System.out.print("This is if statement");
   }
  }
}
This is if statement
모든 if문장 뒤에 선택할 수 있는else문장이 있습니다. 볼 표현식이false이면 문장이 실행됩니다.
문법
if...else의 구문은 다음과 같습니다.
if(Boolean_expression){
  //Executes when the Boolean expression is true
}else{
  //Executes when the Boolean expression is false
}
public class Test {
  public static void main(String args[]){
   int x = 30;
   if( x < 20 ){
     System.out.print("This is if statement");
   }else{
     System.out.print("This is else statement");
   }
  }
}
This is else statement
if 뒤에 선택할 수 있는else if...else문장은 서로 다른 조건에서 단일한if문장과elseif문장을 테스트하는 데 매우 유용하다.
if,elseif,else문장을 사용할 때 몇 가지 명심해야 한다.
if...else의 구문은 다음과 같습니다.
if(Boolean_expression 1){
  //Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
  //Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
  //Executes when the Boolean expression 3 is true
}else {
  //Executes when the none of the above condition is true.
}
public class Test {
  public static void main(String args[]){
   int x = 30;
   if( x == 10 ){
     System.out.print("Value of X is 10");
   }else if( x == 20 ){
     System.out.print("Value of X is 20");
   }else if( x == 30 ){
     System.out.print("Value of X is 30");
   }else{
     System.out.print("This is else statement");
   }
  }
}
Value of X is 30
이것은 항상 합법적인 if-else 문장입니다. 이것은 다른 if 또는else if 문장에서 if 또는else if 문장을 사용할 수 있음을 의미합니다.
문법
중첩 if...else 구문은 다음과 같습니다.
if(Boolean_expression 1){
  //Executes when the Boolean expression 1 is true
  if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
  }
}
예제
public class Test {
  public static void main(String args[]){
   int x = 30;
   int y = 10;
   if( x == 30 ){
     if( y == 10 ){
       System.out.print("X = 30 and Y = 10");
     }
    }
  }
}
X = 30 and Y = 10
switch 문장은 일련의 상등성을 테스트할 수 있는 변수를 허용합니다.모든 값은 케이스라고 불리며, 시작하는 변수는 케이스마다 검사됩니다.
문법
향상된 for 순환 구문은 다음과 같습니다.
switch(expression){
  case value :
    //Statements
    break; //optional
  case value :
    //Statements
    break; //optional
  //You can have any number of case statements.
  default : //Optional
    //Statements
}
public class Test {
  public static void main(String args[]){
   //char grade = args[0].charAt(0);
   char grade = 'C';
   switch(grade)
   {
     case 'A' :
      System.out.println("Excellent!"); 
      break;
     case 'B' :
     case 'C' :
      System.out.println("Well done");
      break;
     case 'D' :
      System.out.println("You passed");
     case 'F' :
      System.out.println("Better try again");
      break;
     default :
      System.out.println("Invalid grade");
   }
   System.out.println("Your grade is " + grade);
  }
}
$ java Test
Well done
Your grade is a C
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.