C++/๋ฐ˜๋ณต๋ฌธ (Loop)

do-while Loop์˜ ๊ธฐ๋ณธ์ ์ธ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. // do-while Loop do { statements; } while (expression); do-while๋ฌธ์€ while๋ฌธ์—์„œ ์ฒ˜๋Ÿผ conditional expression์ด ์ถฉ์กฑ๋˜์—ˆ์„ ๋•Œ, do ๋‚ด๋ถ€์˜ statements๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€, ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•œ ์ดํ›„์— ์กฐ๊ฑด๋ฌธ์„ ํ™•์ธํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜์ž๋ฉด, ์œ„์˜ ์˜ˆ์‹œ์—์„œ statements๋ถ€๋ถ„์„ ์‹คํ–‰ํ•œ ๋’ค, expression ๋ถ€๋ถ„์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— do-while๋ฌธ์˜ statements๋“ค์€ ์ตœ์†Œ ํ•œ๋ฒˆ์˜ ์‹คํ–‰์ด ๋ณด์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ ์ฝ”๋“œ Example 1 // Example 1 #include using namespace std; int main(){..
Syntax // syntax while (expression) statement; // or while (expression) { statement(s); } ์ค‘์š”ํ•œ ์ ์€ infinite loop์ด ๋˜์ง€ ์•Š๋„๋ก ์‹ ๊ฒฝ์“ฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Example Codes // Example 1 #include using namespace std; int main(){ int i {1}; while (i
ํ˜•์‹ // FORMAT for (var_type var_name: sequence){ statements;// can use var_name } ์˜ˆ์‹œ ์ฝ”๋“œ // Example Code #include using namespace std; int main(){ vector my_vec {10, 50, 22, 501, 1930}; for (int num:my_vec) { cout
Conditional Operator๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. variable = (cond_expr) ? expr1 : expr2 ๋‹ค์Œ์€ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. #include using namespace std; int main(){ int a{10}, b{20}; int score {92}; int result {}; // a > b ์ด๋ฉด result = a, b > a ์ด๋ฉด result = b result = (a > b) ? a : b; // score > 90 ์ด๋ฉด Excellent! ์ถœ๋ ฅ // score b) ? a : b; ๋Š” ๋‹ค์Œ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค if (a > b) { result = a; }else{ result = b; } ์—ฌ๋Ÿฌ ์ค„์˜ if-else๋ฌธ์„ ํ•œ์ค„๋กœ ์น˜ํ™˜ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—,..
Chan Lee
'C++/๋ฐ˜๋ณต๋ฌธ (Loop)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก