금요일, 3월 29
Shadow

#010 제어문

5장 제어문

5.1 선택문
if,switch
if(조건절)문장;
if-else
int a,b;
boolean flag;
flag=false;
if(a<b)flag=true;
System.out.println(“Flag is “+flag);
——————–
boolean dataAvailable;

if(dataAvailable)
ProcessData();
else
waitForMoreData();
———————-
int byteAvailable;
if(byteAvailable>0){
ProcessData();
bytesAvailable-=n;
}else
{waitFormoreData();
bytesAvailable=n;}
———————
if(i==10){
if(j<20)a=b;
if(k>100)c=d;
else a=c;
}
else a=d;
———————-
if-else if
if(조건절)
문장;
else if(조건절)
문장;
else
문장;

———————–
switch문
switch(수식){
case 1 :
처리문장;
break;
default
처리문장;
———————–
IFelse.java
class Ifelse{
public static void main(String args[]){

int month =integer.parseInt(args[0]);
//실행 명령에서 사용자로 부터 데이터를 입력받아 정수로 변환한 다음 month에 저장
String MtoS;
if(month==12 ||month==1 ||month==2)
MtoS=”겨울”;
else if(month==3 ||month==4 ||month==5)
MtoS=”봄”;
else if(month==6 ||month==7 ||month==8)
MtoS=”여름”;
else if(month==9 ||month==10 ||month==11)
MtoS=”가을”;
else
MtoS=”해당되는 계절이 없습니다.”;

System.out.println(month + “월은 ” + MtoS + ” 입니다.”);
}
}

Switch.java
class Switch{
public static void main(String args[]){
for(int i=1;i<5;i++)
switch(i){
case 1:
System.out.println(i +”가위”);
break;
case 2:
System.out.println(i +”바위”);
break;
case 3:
System.out.println(i +”보”);
break;
default:
system.out.print(i +”는 1에서 3사이의 값이 아닙니다.);}
}
}

SwitchNobreak.java
class SwitchNoBreak {
public static void main(String args[]){

int month = Integer.parseInt(args[0]);
String MtoS;
switch(month){
case 12:
case 1:
case 2:
MtoS=”겨울입니다.”;
break;
case 3:
case 4:
case 5:
MtoS=”봄입니다.”;
break;
case 6:
case 7:
case 8:
MtoS=”여름입니다.”;
break;
case 9:
case 10:
case 11:
MtoS=”가을입니다.”;
break;
default:
Mtos=”12개월 이내의 달이 아닙니다.”;
}
System.out.println(month + “월은 ” +MtoS);
}
5.2 반복문
조건절로 지정된 조건이 참일동안만
while(){}
do
{
}while();
DoWhile.java
class DoWhile{
public static void main(String args[]){
int N=1;
System.out.println(” *    구구단     *”);
do{
System.out.println(”   “+3 + “*” + N + “=”+(3*N));
N++;
}while(N<10);
}
}
do{
System.out.println(”   “+3 + “*” + N + “=”+(3*N));
}while(++N < 10);

class DoUse Menu{
public static void main(String args[]) throws java.io.IOException {

char choice;
do{
System.out.println(“객체지향 용어 설명”_);
System.out.println(“1.객체”);
System.out.println(“2.클래스”);
System.out.println(“3.메시지”);
System.out.println(“4.상속”);
choice=(char) System.in.read();
}While(choice<‘1′ || choice>’4’);

System.out.println(“n”);

switch(choice){
case ‘1’:
System.out.println(“1.객체”);
System.out.println(“객체는 정보를 관리하기 위한 논리적인 단위이다”);
break;
case ‘2’:
System.out.println(“2.클래스”);
System.out.println(“클래스는 객체를 생성하는 형판(template)이다.”);
break;
case ‘3’:
System.out.println(“3.메시지”);
System.out.println(“메시지는 객체에게 일을 시키는 행위이다.”);
break;
case ‘4’:
System.out.println(“4.상속”);
System.out.println(“”);
break;

반복문for

for문은 주어진 초기값을 시작으로 저건을 만족하는 동안 블록을 수행
초기값설정을 수행하고 조건을 판단하여 참이면 블록을 수행 증감부분 수행
for(초기값;조건;증감){
반복문장들}
class For{
public static void main(String args[]){
int n;
System.out.print(“1에서 10까지의 정수:”);
for(n=1;n<11;n++)
System.out.print(n+” “);
}
}

class DoubleC{
public static void main(String args[]){
for(a=1,b=10;a<b; a++,b–){
System.out.print(“a=”+a);
System.out.print(“b=”+b);
}
}
}

boolean flag=false
for(int i=1;!flag; i++)
if finished_routine())flag=true;
}

class Forflag{
public static void main(String args[]){
int i;
boolean flag=false;
int =1;
for(;!flag;){
System.out.println(“i의 값은 “+i+”입니다.”);
if(i==10)flag=true;
i++}}}
for(;;)
{
}

While.java
class While{
void main static void main(String args[]){

}
}
5.3 제어의 이동
프로그램 제어를 이동시키기 위해 break,countrue,return

break
1.switch문에서 switch문을 벗어나는데 사용
2.반복문에서 반복루프를 벗어나는데 사용
3.기존프로그램 goto문의 개선된 형태로서사용

break label
class BreakTest{
public static void main(String args[]){
boolen t=true;
first:{
second:{
third:{
System.out.println(“third블록 ‘break’문장전”);
if(t) break second;
//second블럭 밖으로 제어가 이동;
System.out.println(“third블록 break문장후”);
}
System.out.println(“second블록문장”);
}
System.out.println(“first블록문장”);
}
}
}

class BreakErr{
public satic void main(String args[]){
one:for(int i=0;i<3;i++){
System.out.print(“pass”+i+”:”);
for(int j=0;j<100;j++){
if(j==10) break one;
//내포된 상태가 아니므로 에러 발생
System.out.print(j+” “);
}
}
}
continue label
내포된 for문에서 제어를 label로 지정된 for 문의 처음으로 이동시킨다.
class ContinueLabelTest{
public static void main(String args[]){
outer: for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(j>i){
System.out.println();
continue outer;
//outer로 지정된 for 블록 처음(i로)으로 제어이동
}
System.out.print(“”+(i*j));
}
}
}}

class Return{
public static void main(String args[]){
boolean t =true;
System.out.println(“알아”);
if(t) return;
System.out.println(“자바”);
}
}—>JVM으로 제어권이동

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.