아두이노 프로그래밍 기초(3) – 조건문, 반복문

이번 시간에는 조건문, 반복문에 대해서 알아보고자 한다.

조건문

참 거짓 또는 특정 조건에 맞는 경우에 지정된 동작을 수행하는 문장이다. 구현된 모든 기능을 사용하지 않고, 원하는 기능만 사용할 수 있게 도와준다. 또한 특정 조건에 맞는 상황에만 동작 될 수 있도록 해준다.

0은 False를 나타내고, 이외의 모든 숫자들은 True를 나타낸다. 일반적으로 관계 연산자와 논리 연산자를 이용하여 조건을 만들어 준다. 특정 상황에서는 반드시 수행하도록 만들기 위해서 상수를 넣어주는 경우가 있다.

if문

조건 문장이 참인 경우에 실행한다.


조건문에는 switch문도 존재하지만 아두이노에서 흔하게 사용되지는 않기 때문에 생략하기로 한다.

반복문

반복문은 특정 조건을 만족하는 동안에 반복 수행을 하기 위한 문장이다. 반복의 조건을 무너뜨리기 위한 연산이 필요한데 if문을 사용하여 반복문에서 벗어나기도 한다. 반복문의 종류는 조건을 확인하는 위치에 따라 두 가지 종류로 나뉘어진다.

while문

특정 조건을 주고, 해당 조건을 만족하는 동안 특정 영역을 반복하는 구조이다. while문의 소괄호 안에 조건을 명시하고 조건이 만족되는 동안 중괄호 안의 내용을 반복 수행한다. 반복의 대상이 한 문장이면 중괄호가 생략 가능하다.

While문의 순서도는 다음과 같다.

아두이노에서는 반복문으로 보통 while문을 많이 쓰기 때문에 여기까지만 알아도 무방하지만, C/C++에 대해서 조금 더 알고자 한다면 다음에 나오는 for문에 대해서도 알아두면 좋다.

for문

반복을 수행하기 위한 세 가지 필수 요소들을 묶어 놓은 반복문이다. 초기문은 반복을 위한 변수의 선언 및 초기화, 조건문은 반복의 조건을 검사, 증감문은 반복의 조건을 무너뜨리기 위한 연산이다.

While문과 for문을 비교하면 다음과 같다.

8 Replies to “아두이노 프로그래밍 기초(3) – 조건문, 반복문”

  1. I was recommended this web site by my cousin. I am not sure
    whether this post is written by him as nobody else know such detailed about my trouble.
    You’re wonderful! Thanks!

  2. Heya i’m for the primary time here. I came across this board and I
    to find It really helpful & it helped me out much.
    I am hoping to offer one thing again and help others like you helped me.

  3. This is really interesting, You are a very skilled blogger.
    I have joined your rss feed and look forward to seeking more of your fantastic post.
    Also, I’ve shared your site in my social networks!

  4. Does your website have a contact page? I’m having trouble
    locating it but, I’d like to shoot you an e-mail. I’ve got some suggestions for your
    blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.

  5. I am curious to find out what blog platform you’re utilizing?

    I’m having some minor security issues with my latest website and I’d like to find something more
    safeguarded. Do you have any suggestions?

Leave a Reply

Your email address will not be published. Required fields are marked *