{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "다음의 ```break``` 의 사용을 읽어보고 결과를 예측 해보자" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int i, j;\n", " for(i = 1; i <= 20; i++)\n", " {\n", " for(j = i; j <= 20; j++)\n", " {\n", " if(j%5 == 0)\n", " {\n", " printf(\"%3d\\n\", j);\n", " break;\n", " }\n", " }\n", " }\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "다음의 ```continue```의 사용을 보고 결과를 예측해보자. `continue` 실행시 실행 흐름은 몇 번 째 줄 끝으로 이동하는가? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int i = 0, sum = 0;\n", " while(i < 6)\n", " {\n", " i++;\n", " if(i % 3 == 0)\n", " {\n", " continue;\n", " sum += 1;\n", " }\n", " }\n", " \n", " printf(\"%d\\n\", sum);\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제 \n", "다음 코드를 실행하기 전에 결과를 예상해보시오." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " char ch[] = \"hello\"; // array\n", " int i, a, b, top = 0;\n", " \n", " for(i = 0; i <= 4; i++)\n", " {\n", " a = 0;\n", " for(b = i; b <= 4; b++)\n", " {\n", " if(ch[b] == ch[i])\n", " a = a + 1;\n", " }\n", " if(a > top)\n", " top = a;\n", " }\n", " printf(\"%d\", top)\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "\n", "다음은 100명의 성적의 평균을 구하는 프로그램의 일부이다. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for (cnt = 0; cnt < n; ++cnt) {\n", " scanf(\"%d\", &i);\n", " if (i >= 0){\n", " j += i;\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "성적을 모두 입력한 후 -1을 입력하면 그 명 수만큼의 평균을 구하고 싶다. 예: 30, 50, 70, -1 입력시 3명의 평균을 구한다.\n", "* break 로 이 기능을 구현하시오\n", "* goto 로 이 기능을 구현하시오\n", "* continue 로 이 기능이 구현 가능한지 생각해보고, 구현해보자" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " // your code here\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "다음의 코드를 `continue` 없이 똑같이 동작하도록 변경해보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " n = 0;\n", " sum = 0;\n", " while (n < 10) {\n", " scanf(\"%d\", &i);\n", " if (i == 0)\n", " continue;\n", " sum += i;\n", " n++;\n", " }\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "\n", "C 언어에 `break` 문이 없었다면 `goto`문으로 루프를 탈출 할 수 있다. \n", "\n", "다음의 코드를 수정하여 `break` 대신 `goto`를 써서 같은 동작을 하도록 변경하여 보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int n = 11, d = 0;\n", " for (d = 2; d < n; d++)\n", " if (n % d == 0)\n", " break;\n", "\n", " if (d < n)\n", " printf(\"%d is divisible by %d\\n\", n, d);\n", " else\n", " printf(\"%d is prime\\n\", n);\n", "\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "\n", "많은 경우 `goto`문을 쓰는 것은 코드를 위 아래로 여러번 이동하면서 봐야하기 때문에 이해하기가 쉽지 않다. 그래서 꼭 필요한 경우가 아니라면 쓰지 않는다. \n", "\n", "어떤 경우가 꼭 필요한 경우인지 생각을 정리해보자. \n", "\n", "다음의 코드를 수정하여 `goto`문이 안 쓰도록 바꿔보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " for (d = 2; d < n; d++)\n", " if (n % d == 0)\n", " goto prime;\n", " else\n", " goto general;\n", "\n", " printf(\"Does this line print out?\\n\");\n", " printf(\"if it does not print, then why not?\\n\");\n", "\n", " general:\n", " if (d < n)\n", " printf(\"%d is divisible by %d\\n\", n, d);\n", "\n", " prime:\n", " if (d > n)\n", " printf(\"%d is prime\\n\", n);\n", "\n", " return 0;\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "C", "language": "c", "name": "c" }, "language_info": { "file_extension": ".c", "mimetype": "text/plain", "name": "c" } }, "nbformat": 4, "nbformat_minor": 2 }