Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Menu
- 에러페이지
- 어노테이션
- 전화걸기
- struts2
- HTML
- layout
- Spring
- 메소드
- Graphic
- 안드로이드
- AWT
- JavaScript
- paint
- Java
- Android
- 국제화
- 기본
- OGNL
- 예외처리
- 오버로딩
- 메서드
- JSP
- 생성자
- mybatis
- 이클립스
- 클래스
- oracle
- Eclips
- 배열
Archives
- Today
- Total
note
While 문 본문
기본
public class While01 {//while 문
public static void main(String[] args){
int i;
for(i=1; i<=5; i++)
System.out.print(" "+i);
System.out.println("\n-----------------");
i=1; //초기식
while(i<=5){ //조건식
System.out.print(" " +i); //반복 처리할 문장
i++; //증감식
}
}
}
for문과 다르게 조건식만 while문은 들어가 있다
for문과 while은 항상 무한루프를 염두해둬야한다
1 2 3 4 5
-----------------
1 2 3 4 5
public class While02 {//while문2
public static void main(String[] args){
int i=1; //초기식
while(i++<=4) //증감식, 조건식
System.out.print(i + ", ");//후행 처리일경우 5까지출력
System.out.println("\n----------- >>");
i=1;
while(++i<=4)
System.out.print( i + ", ");
System.out.println("\n----------- >>");;
i=0;
while(i++<=4)
System.out.print( i + ", ");
System.out.println("\n----------- >>");;
}
}
2, 3, 4, 5,
----------- >>
2, 3, 4,
----------- >>
1, 2, 3, 4, 5,
----------- >>
while문으로 1부터 10사이의 짝수의 합구하기
public class While04 {//while문으로 1부터 10사이의 짝수의 합구하기
public static void main(String[] args){
int n; //제어변수 선언
int tot=0; //합을 누적할 변수 선언
n=0; //제어변수 n을 0으로 초기화
while(n<=8){ //제어변수 n이 8보다 작거나 같을때까지 반복수행
n+=2; //제어변수 n을 2씩증가한후 (증감식)
tot += n; //제어변수 n의 합을 누적할 변수에 더한다
}
System.out.println("tot = "+tot); //반복문에서 벗어나면 합을 출력한다
}
}
tot = 30