[완료] 라벨 컨티뉴 코드 분석

gumagula의 이미지

public class LabelContinue {
    public static void main(String[] args) {
        int count = 0;
        outer: for (int i = 0; i<10; i++){
            inner: for (int j = 0; j<10; j++){
                if (j==2) continue inner;
                if (j==5) continue outer;
                ++count;
            }
            if (i==8) continue;
            ++count;
        }
        System.out.println("count = " +count);
    }
}

위와같은 코드의 인쇄되는 count값을 유추를 하라고 합니다. (컴파일러 없이 손으로...)

단순한 for문이나 continue문은 어떻게 될 지 대충 짐작이 되는데 (다중문 말고 단일 사용 같은 경우)

저러한 경우는 어떠한 원리로 동작하는지 도저히 감이 안잡혀서 이렇게 글을 써봅니다.

컴파일러로 돌리면 카운트 값은 40이 뜨긴 하는데...

도움 부탁드리겠습니다...

raymundo의 이미지

눈으로 확인하고 싶으면 곳곳에 print문을 넣어서 현재 i,j,count 값을 출력해서 보면 되겠죠. 특히 ++count 직전에 넣은 프린트문은 count증가가 언제 일어나는지 확인하는데 도움이 될 거고요.

종이와 펜으로만 푼다면,

i = 0일때
      j = 0 : 증가
      j = 1 : 증가
      j = 2 : continue 하니까 안쪽 루프의 아랫부분 스킵
      j = 3 : 증가
      j = 4 : 증가
      j = 5 : continue outer; 하니까, 바깥쪽 루프까지 나머지 다 스킵하고 다음 반복
 
i = 1일때
      위와 동
 
i = 2일때
...
i = 9일때

j가 5가 되면 바로 바깥 루프의 다음 반복으로 넘어가니까

if (i==8)...
++count;

이 부분은 실행되지 않는다는 걸 알 수 있고,

안쪽 루프가 딱히 i값에 따라 다르게 동작하는 부분이 없으니, i=1부터는 더 해볼 필요도 없이 계속 똑같은 과정을 거치는 걸 알 수 있고,

그럼 count 증가는 안쪽루프에서 4번, 이걸 바깥쪽 루프가 10번 도니까 40번.

뭐 이런 식으로 세어 나가면 되지 싶습니다.

좋은 하루 되세요!

gumagula의 이미지

읽어보니 딱하고 이해가 바로 오네요 ㅠㅠ 감사합니다

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.