if : 단 하나의 조건을 검사한 후에 다음 실행문을 실행하는 조건문이다.
if~else : 단독으로 if / else만 사용한다면, true/false를 검사 후 참과 거짓에 따라서 조건문을 실행하고,
중첩시켜서 사용한다면 여러가지 조건들중 하나에 맞는것에 따라 실행문을 실행하고, 만약 충족되는 조건이 없다면 else문에서 실행된다.
switch : 위 if~else문의 중첩문과 비슷하게
다양한 조건을 만들어두고, 조건에 맞는 실행문을 실행하며 조건에 하나도 부합하지 않는다면 default문을 실행한다.
국어:80 수학:80 영어:60 점의 평균을 출력하고, 평균에 따른 수우미양가를 출력하시오.
int kor = 80;
int math = 80;
int eng = 60;
double avg = (kor + math + eng) / 3.0;
char grade = ' ';
if(avg >= 90)
grade = '수';
else if(avg >= 80)
grade = '우';
else if(avg >= 70)
grade = '미';
else if(avg >= 60)
grade = '양';
else
grade = '가';
이때 주의해야할 점, 괄호를 넣지 않고
kor + math + eng / 3.0으로 계산하게되면
단항연산자의 우선순위에 따라서 eng/3.0이 먼저 연산되므로 주의해야한다.
int num1 = -80;
int num2 = 33;
int num3 = 55;
int max = 0;
if (num1 > num2) {
if (num1 > num3)
max = num1;
else
max = num3;
} else {
if (num2 > num3)
max = num2;
else
max = num3;
}
System.out.println(max);
break문에 걸리면 break;까지만 실행되고 조건문을 빠져나온다는 뜻이다.
int num = -10;
if(num < 0)
num *= -num;
else
num *= +num;
while문은 조건을 먼저 검사하고 반복문이 실행되지만,
do~while문은 최초 1회는 무조건 실행 후 반복조건을 검사한 후 루프로 돌아간다는 차이점이 있다.
1. 변수의 선언 및 초기화
2. 변수의 조건 검사
3. for문 내 구문 실행
4. 변수의 증감 실행
1->2->3->4->(2->3->4)->(2->3->4).... 를 반복하다가
조건이 false값에 도달하게되면 for문에서 빠져나온다.
int i = 1;
while (i <= 9) {
for (int j = 1; j <= 9; j++) {
System.out.println(i + "*" + j + "=" + i * j);
if (j == 9)
break;
}
i++;
}
int i=0;
for(int j=1; j<=100; j++)
{
i += j;
}
System.out.println("100까지의 합은 "+i+"입니다");
실행결과
100까지의 합은 5050입니다.
int sum = 0;
for(int i=1; i<=100; i+=2)
{
sum += i;
}
출력 결과
2500
break는 반복문을 완전히 빠져나오는 것이고,
continue는 반복문의 조건부분으로 다시 돌아가라는 뜻이다.
1과 1000 사이의 숫자중 3의 배수 이자 5의 배수인 첫번째 수는?
for(int i=1; i<=1000; i++)
{
if(i%3 == 0 && i%5 == 0){
System.out.println(i);
break;
}
}
실행결과
15
int count=0;
for(int i=1; i<=1000; i++)
{
if(i % 2 == 0 && i % 3 == 0)
count++;
}
System.out.println(count);
실행결과
166
126500 의 금액을 한국화폐으로 바꾸었을 때 각각 몇 개의 화폐가 필요한지 계산해서 출력하라.
예) int a = 126500;
오만원 : 2장
만원: 2장
오천원짜리 : 1장
천원짜리 : 1장
오백원짜리 : 1개
백원짜리 : 0개
int balance = 126500;
int count_5man = 0;
int count_1man = 0;
int count_5sen = 0;
int count_1sen = 0;
int count_500 = 0;
int count_100 = 0;
if(balance >= 50000) {
count_5man = (int)balance/50000;
balance -= count_5man * 50000;
}
if(balance >= 10000) {
count_1man =(int)balance/10000;
balance -= count_1man * 10000;
}
if(balance >= 5000) {
count_5sen =(int)balance/5000;
balance -= count_5sen * 5000;
}
if(balance >= 1000) {
count_1sen = (int)balance/1000;
balance -= count_1sen * 1000;
}
if(balance >= 500) {
count_500 = (int)balance/500;
balance -= count_500 * 500;
}
if(balance >= 100) {
count_100 = (int)balance/100;
balance -= count_100 * 100;
}
System.out.println("5만원권의 갯수 : " + count_5man);
System.out.println("1만원권의 갯수 : " + count_1man);
System.out.println("5천원권의 갯수 : " + count_5sen);
System.out.println("1천원권의 갯수 : " + count_1sen);
System.out.println("5백원권의 갯수 : " + count_500);
System.out.println("1백원권의 갯수 : " + count_100);
int num1 = 90;
int num2 = 70;
int num3 = 100;
int max = 0;
max = ( num1 > num2 ? num1 : num2) > num3?
(num1>num2 ? num1:num2):num3 ;
//num1보다 num2가 큰지 검사하고
더 크면 num1, 작으면 num2를 대입 후 다시
반환된 값과 num3을 비교 후,
대입된 값 : num3으로 비교 대입시킨다.
System.out.println(max);
for문 : for ( ; ; )
while문 : while(true) { }
do~while문 : do{ ... }while(true)
for(int i=1; i<=9; i++){
for(int j=1; j<=9; j++)
System.out.println(i + "*" + j + "=" + i*j);
}
for(int i=2; i<=9; i+=2){
for(int j=1; j<=9; j++)
System.out.println(i + "*" + j + "=" + i*j);
}
for(int i=1; i<=9; i++){
if(i%3 != 0)
continue;
for(int j=1; j<=9; j++)
System.out.println(i + "*" + j + "=" + i*j);
}
출력
*****
*****
*****
*****
*****
코드
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
System.out.println("*");
출력
*
**
***
****
*****
코드
for(int i=0; i<5; i++)
for(int j=0; j<i; j++)
System.out.println("*");