본문 바로가기

빅데이터과정/JAVA

#52_140827_JAVA_LOOP

728x90

# LOOP



 

while loop 

 

1.     loop 문의 3가지 조건

변수 선언

반복 조건

반복 조건을 무너뜨릴 조건

 

 


 

 



 

 

For …. loop 

 


 

 

구구단

public static void main(String[] args)

{

         for(int num1=1; num1<10;num1++)

         {

                  for(int num2=1; num2<10;num2++)

                  {

                           System.out.println(num1+" * "+num2+" ="+num1*num2);    

                  }

         }

}

결과값

1 * 1 =1

1 * 2 =2

1 * 3 =3

1 * 4 =4

1 * 5 =5

1 * 6 =6

………………………..

 

 

 





 

중첩 loop 

 

 

구구단

public static void main(String[] args)

{

         for(int num1=1; num1<10;num1++)

         {

                  for(int num2=2; num2<10;num2++)

                  {

                           System.out.print(num2+" * "+num1+" ="+num1*num2+"    ");  

                  }

         System.out.println("");

         }

}

결과값

2 * 1 =2    3 * 1 =3    4 * 1 =4    5 * 1 =5    6 * 1 =6    7 * 1 =7    8 * 1 =8    9 * 1 =9   

2 * 2 =4    3 * 2 =6    4 * 2 =8    5 * 2 =10    6 * 2 =12    7 * 2 =14    8 * 2 =16    9 * 2 =18   

2 * 3 =6    3 * 3 =9    4 * 3 =12    5 * 3 =15    6 * 3 =18    7 * 3 =21    8 * 3 =24    9 * 3 =27   

2 * 4 =8    3 * 4 =12    4 * 4 =16    5 * 4 =20    6 * 4 =24    7 * 4 =28    8 * 4 =32    9 * 4 =36   

2 * 5 =10    3 * 5 =15    4 * 5 =20    5 * 5 =25    6 * 5 =30    7 * 5 =35    8 * 5 =40    9 * 5 =45

………………………..

 

 

 

 

 

Break문과 continue

 


 

 

Break

public static void main(String[] args)

{

      int num=1;

      boolean search=false;

     

      while(num<100)

      {

            if(num%5==0 && num%7==0)

            {

                  search=true;

                  break;

            }

            num++;

      }

     

      if(search)

            System.out.println("찾는 정수 : " + num);

      else

            System.out.println("5 배수이자 7 배수를 찾지 못했습니다.");

}

결과값

찾는 정수 : 35

 

 

 

Continue

public static void main(String[] args)

{

      int num=0;

      int count=0;

     

      while((num++)<100)   // 0 부터 시작해서 100 보다 작을때까지 loop  돌리는데

      {

            if(num%5!=0 || num%7!=0)  // 5 배수가 아니거나 7 배수가 아니라면

                  continue;        //  35 거나 100  아니라면 지속해라

           

            count++;

 

            System.out.println(num);

      }

 

System.out.println("count: " + count);

}

결과값

35

70

count: 2

 

 

 

숫자를 입력받고 입력한 숫자까지 출력(while break 활용)

public static void main(String[] args)

{

    System.out.print("숫자를 입력하시오 ? ");

    Scanner sc=new Scanner(System.in); 

    float input_num1=sc.nextFloat();

    int num=1;

   

      while(true)

      {

            System.out.println(num);

            if(num==input_num1)

            {

                  break;

            }

            num++;

      }

 

}

결과값

숫자를 입력하시오 ? 10

1

2

3

4

5

6

7

8

9

10

 

 

 






'빅데이터과정 > JAVA' 카테고리의 다른 글

#52_140827_JAVA_상속  (0) 2014.08.27
#52_140827_JAVA_메소드  (0) 2014.08.27
#52_140827_JAVA_조건문  (0) 2014.08.27
#52_140827_JAVA_연산자  (0) 2014.08.27
#51_140826_JAVA_기본  (0) 2014.08.26