본문 바로가기

Programming/자바

생활코딩 자바 - 반복문

while
  • package org.opentutorials.javatutorials.loo;

    public class WhileDemo {

    public static void main(String[] args) {
    while (true) {
    System.out.println("hello");
    }

    }}

 

  • package org.opentutorials.javatutorials.loo;

    public class WhileDemo2 {

    public static void main(String[] args) {
    int i = 0;
    while (i<10) {
    System.out.println("hello" + i);
    i++;
    }

    }}

=>

hello0
hello1
hello2
hello3
hello4
hello5
hello6
hello7
hello8
hello9

 

 

for

for (초기화; 종료조건; 반복실행) {

반복적으로 실행될 구문

}

 

  •  

for (int i = 0; i<10; i++) {

System.out.println("hello" + i);

}

 

 

 

int max = 10; 가변적

int i = 0;
while (i<10) {
System.out.println("hello" + i);
i++; 고정적

 

 

break, continue
  • break(if문 탈출)

for (int i =0; i<10; i++) {
if (i == 5)
break;
System.out.println("coding"+i);
}

 

=> 0, 1, 2, 3, 4

 

 

  • continue(if문 지속)

for (int i = 0; i < 10; i++) {
if (i==5)
continue;
System.out.println("coding" + i);
}

 

=> 0,1,2,3,4,6,7,8,9

 

 

반복문의 중첩

 

  •  

for (int i=0; i<10; i++) {
  for (int j=0;j<10;j++) {
    System.out.println(i + "" + j);

}}

=> 00, 01, 02,03, ... 99

 

 

 

 

 

 

 

'Programming > 자바' 카테고리의 다른 글

생활코딩 자바 - 메소드  (0) 2022.03.19
생활코딩 자바 - 배열  (0) 2022.03.19
생활코딩 자바 - 논리연산자  (0) 2022.03.13
생활코딩 자바 - 조건문  (0) 2022.03.13
생활코딩 자바 - 비교와 Boolean  (0) 2022.03.13