'While'에 해당되는 글 3건

  1. 2021.01.27 반복문
  2. 2020.12.14 반복문 for, while, do while
  3. 2020.05.25 while문

반복문

Swift/문법 2021. 1. 27. 14:09

for in

for item int items {

code

}

 

for integers in integers {

print(integer)

}  

 

* Dictionary의 item은 key와 value로 구성된 튜플 타입

for (name, age) in people {   // name에는 key값이, age에는 value값이 들어옴

print("\(name): \(age)")

}

 

 

while

- 조건문처럼 조건 부분에 ()는 선택사항

- 조건 부분에는 다른 값이 아니라 꼭 Bool 값이 들어가야 함

 

while integers.count > 1 {

integers.removeLast()

}

 

 

repeat while

- do while과 비슷함

 

repeat {

integers.removeLast()   // 먼저 실행이 된 후에

} while integers.count > 0   // while에서 조건을 체크한 이후에 계속 반복을 실행

'Swift > 문법' 카테고리의 다른 글

옵셔널 추출(Optional Unwrapping)  (0) 2021.01.27
옵셔널(Optional)  (0) 2021.01.27
조건문  (0) 2021.01.27
함수 고급  (0) 2021.01.27
함수 기본  (0) 2021.01.27
Posted by khon98
,

For(선언; 조건; 증감)

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

I 1; 1부터 10까지; i 1더한다

 

int main(void){

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

        printf("Hello World %d\n", i);

    }

}

 

 

 

While

// while (조건)

int main(void){

    int i = 1; // 선언

    while (i <= 10) { // 조건

        printf("Hello World %d\n", i++); // 증감

    }

}

 

 

 

Do while

// do { } while (조건);

int main(void){

    int i = 1; // 선언

    do {

        printf("Hello Wordl %d\n", i++); // 조건

    } while (i <= 10); // 증감

}

 

'C언어' 카테고리의 다른 글

조건과 분기  (0) 2020.12.16
이중 반복문  (0) 2020.12.14
++  (0) 2020.12.14
Printf와 Scanf  (0) 2020.12.14
변수와 상수  (0) 2020.12.14
Posted by khon98
,

while문

자바 2020. 5. 25. 21:57

while

코드의 일부분을 반복할 경우 사용한다

반복 조건이 결정 되어 있을 때 사용한다

반복 횟수를 결정하지 못하고 반복할 조건이 있을 경우 사용하는 것이 while문이다

조건식을 먼저 검사하고 수행 여부를 결정하기 때문에 조건식이 처음부터 거짓이라면 코드는 단 한 번도 수행되지 않는다

 

while(조건식){

코드

}

 

---------------------------------------------------------------------------

//1부터 숫자를 하나씩 증가시켜서 2, 3, 5로 나누어 떨어지지 않는 수를 출력하시오.

 

int cnt = 0;

int number = 1;

 

while(cnt < 100){

     if(!(number % 2 == 0 || number % 3 == 0 || number % 5 == 0)){

        System.out.printf("%d : %d\n" , cnt + 1, number);

        cnt++;

     }

     number++;

}

---------------------------------------------------------------------------

 

cnt라는 변수는 숫자 하나를 찾을 때마다 증가시킴

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

class  (0) 2020.05.27
do while문  (0) 2020.05.25
for문  (0) 2020.05.23
Switch문  (0) 2020.05.22
IF문  (0) 2020.05.20
Posted by khon98
,