SlideShare a Scribd company logo
1 of 12
Download to read offline
반복문 ( while )
FaceBook Group "코딩의 아침"
Self Study & Sharing
Ø 반복문의 이해
물론 음악이 끝나고 버튼을 다시 누르고~ 누르고~
해도 되겠지만~ ! 굉장히 귀찮은 일!
같은 작업을 여러 번 해야 하는데. . .
반복 재생 버튼이 있다면 이런 수고로움을 덜 수 있겠죠~!
같은 곡을 반복해서 듣고 싶다면
어떻게 하시나요?
Ø 반복문의 이해
cout << "FaceBook Group 코딩의 아침" <<endl;
만약,
출력을 5번 해야 한다고 생각해 봅시다.
5번 정도야 그냥 치고 말지~
(Ctrl + C, Ctrl + V) 힘을 빌려도 되요~
하지만 100번, 1000번, 10000번 . . . 그 이상 반복해야한다면?
cpp파일이 저희 그룹 이름으로 도배가 되겠네요 ^^
cout << "FaceBook Group 코딩의 아침" <<endl;
cout << "FaceBook Group 코딩의 아침" <<endl;
cout << "FaceBook Group 코딩의 아침" <<endl;
cout << "FaceBook Group 코딩의 아침" <<endl;
... ... ... ... ... 노답...
cout << "FaceBook Group 코딩의 아침" <<endl;
Ø 반복문의 이해
반복문을 활용하면
코드 몇 줄로 원하는 반복 작업을 실행 시킬 수 있습니다!
while(조건)
{
조건을 만족하면 반복
}
하지만, 특정 코드가 무한 반복 된다면,
다음 실행을 원하고자 하는 코드는 무용지물이 되겠죠...
각, 반복문을 실행하는 조건과 반복문을 탈출하는 조건에 대해
자세히 알아보도록 합시다.
Ø 반복문 - while 사용 방법
while ( true )
{
실행이 되겠지만
조건이 true 이므로
무한 반복 됨
}
while ( false )
{
실행이 당연히~ 안됌!
조건이 false 잖아요~!
}
while ( 조건 )
{
실행 할 내용
}
( )안의 조건이 true 인지 false인지에 따라 { } 몸체 부분실행 유무를 판단합니다.
while(1) 이라면? 0이 아닌 수는 true! 실행 조건이 된다는 사실!
; 몸체 실행 후, 조건을 다시 확인
Ø 반복문 - while 기본 예시
void main(void)
{
int iRoutine = 5;
while (iRoutine)
{
cout << "FaceBook Group 코딩의아침" <<endl;
--iRoutine;
}
}
void main(void)
{
cout << "FaceBook Group 코딩의아침" <<endl;
cout << "FaceBook Group 코딩의아침" <<endl;
cout << "FaceBook Group 코딩의아침" <<endl;
cout << "FaceBook Group 코딩의아침" <<endl;
cout << "FaceBook Group 코딩의아침" <<endl;
}
같은 결과지만,
반복문 사용 유무의 차이
눈에 보이시나요??? ^^
void main(void)
{
int iRoutine = 5;
while (iRoutine)
{
cout << "FaceBook Group 코딩의아침" <<endl;
--iRoutine;
}
}
Ø 반복문 - while 탈출하기
탈출이란?
반복문의 실행을 멈추는 것을 의미 합니다.
그렇다면,
원하는 결과를 얻은 후
실행을 멈추게 하는 부분은
어디 일까요?
5로 초기화된 iRoutine이 0이 되면 조건이 false가 되므로 반복문을 탈출하게 됩니다.
Ø 반복문 : while 이용하기
그렇다면 while( ) 을
여러 번 출력하기 위해서 사용한다는 건 너무 ~ 뻔한 예시니까...
다른 반복 상황을 설정 해 볼까요?
어떤 상황이 완료될 때 까지 유지 되야하는 경우는 어떨까요?
던전 마지막 방! 보스가 있는 방에 서 있는 거죠.
항상 이런 메세지가 나오지 않나요?
지금 들어가면, 보스몬스터를 없애거나 당신이 죽을 때까지 나갈 수 없습니다.
입장하시겠습니까? 1.입장 2.도망
Ø while을 이용한 코드작성
입장 하셨네요? ^^
보스몹을 죽이거나! 내가 죽거나! 한 명이 살아 남을 때 까지 싸워 봅시다.
싸
우
자
!
HP : 100HP : 80
VS
Ø while을 이용한 코드작성
while(true)
{
cout << "지금 들어가면, 보스몬스터를 없애거나 당신이 죽을 때까지 나갈 수 없습니다." <<endl;
cout << "입장하시겠습니까? 1.입장 2.도망" <<endl;
cin >> iInput;
if( 2 == iInput)
break; //탈출조건
}
; 이미 던전에 들어왔습니다.! 반복되는 상황
; 도망가기를 선택하면 반복문을 탈출 합니다.
break; 키워드를 사용해서 반복문을 중단 할 수 있습니다.
Ø while을 이용한 코드작성
1. 공격!!
if( 1 == iInput)
{
int iInput = 0;
while(true)
{
system("cls");
cout << "보스HP : " << iBossHp <<endl;
cout << "내HP : " << iPlayerHp <<endl;
cout << " ========================== " <<endl;
cout << "1.공격 2.도망" << endl;
cin >> iInput;
if( 1 == iInput)
{
iBossHp -= iPlayerAtt;
iPlayerHp -= iBossAtt;
}
if( 2 == iInput || iBossHp <= 0 || iPlayerHp <= 0 )
break;
}
}
; 보스존 입장
; 한명이 살아 남을 때까지 아래의 메세지가 반복 출력 됩니다.
; 도망가거나, 둘 중 하나가 죽는 경우
반복문을 탈출 합니다.
[C언어] 반복문 (While)

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

[C언어] 반복문 (While)

  • 1. 반복문 ( while ) FaceBook Group "코딩의 아침" Self Study & Sharing
  • 2. Ø 반복문의 이해 물론 음악이 끝나고 버튼을 다시 누르고~ 누르고~ 해도 되겠지만~ ! 굉장히 귀찮은 일! 같은 작업을 여러 번 해야 하는데. . . 반복 재생 버튼이 있다면 이런 수고로움을 덜 수 있겠죠~! 같은 곡을 반복해서 듣고 싶다면 어떻게 하시나요?
  • 3. Ø 반복문의 이해 cout << "FaceBook Group 코딩의 아침" <<endl; 만약, 출력을 5번 해야 한다고 생각해 봅시다. 5번 정도야 그냥 치고 말지~ (Ctrl + C, Ctrl + V) 힘을 빌려도 되요~ 하지만 100번, 1000번, 10000번 . . . 그 이상 반복해야한다면? cpp파일이 저희 그룹 이름으로 도배가 되겠네요 ^^ cout << "FaceBook Group 코딩의 아침" <<endl; cout << "FaceBook Group 코딩의 아침" <<endl; cout << "FaceBook Group 코딩의 아침" <<endl; cout << "FaceBook Group 코딩의 아침" <<endl; ... ... ... ... ... 노답... cout << "FaceBook Group 코딩의 아침" <<endl;
  • 4. Ø 반복문의 이해 반복문을 활용하면 코드 몇 줄로 원하는 반복 작업을 실행 시킬 수 있습니다! while(조건) { 조건을 만족하면 반복 } 하지만, 특정 코드가 무한 반복 된다면, 다음 실행을 원하고자 하는 코드는 무용지물이 되겠죠... 각, 반복문을 실행하는 조건과 반복문을 탈출하는 조건에 대해 자세히 알아보도록 합시다.
  • 5. Ø 반복문 - while 사용 방법 while ( true ) { 실행이 되겠지만 조건이 true 이므로 무한 반복 됨 } while ( false ) { 실행이 당연히~ 안됌! 조건이 false 잖아요~! } while ( 조건 ) { 실행 할 내용 } ( )안의 조건이 true 인지 false인지에 따라 { } 몸체 부분실행 유무를 판단합니다. while(1) 이라면? 0이 아닌 수는 true! 실행 조건이 된다는 사실! ; 몸체 실행 후, 조건을 다시 확인
  • 6. Ø 반복문 - while 기본 예시 void main(void) { int iRoutine = 5; while (iRoutine) { cout << "FaceBook Group 코딩의아침" <<endl; --iRoutine; } } void main(void) { cout << "FaceBook Group 코딩의아침" <<endl; cout << "FaceBook Group 코딩의아침" <<endl; cout << "FaceBook Group 코딩의아침" <<endl; cout << "FaceBook Group 코딩의아침" <<endl; cout << "FaceBook Group 코딩의아침" <<endl; } 같은 결과지만, 반복문 사용 유무의 차이 눈에 보이시나요??? ^^
  • 7. void main(void) { int iRoutine = 5; while (iRoutine) { cout << "FaceBook Group 코딩의아침" <<endl; --iRoutine; } } Ø 반복문 - while 탈출하기 탈출이란? 반복문의 실행을 멈추는 것을 의미 합니다. 그렇다면, 원하는 결과를 얻은 후 실행을 멈추게 하는 부분은 어디 일까요? 5로 초기화된 iRoutine이 0이 되면 조건이 false가 되므로 반복문을 탈출하게 됩니다.
  • 8. Ø 반복문 : while 이용하기 그렇다면 while( ) 을 여러 번 출력하기 위해서 사용한다는 건 너무 ~ 뻔한 예시니까... 다른 반복 상황을 설정 해 볼까요? 어떤 상황이 완료될 때 까지 유지 되야하는 경우는 어떨까요? 던전 마지막 방! 보스가 있는 방에 서 있는 거죠. 항상 이런 메세지가 나오지 않나요? 지금 들어가면, 보스몬스터를 없애거나 당신이 죽을 때까지 나갈 수 없습니다. 입장하시겠습니까? 1.입장 2.도망
  • 9. Ø while을 이용한 코드작성 입장 하셨네요? ^^ 보스몹을 죽이거나! 내가 죽거나! 한 명이 살아 남을 때 까지 싸워 봅시다. 싸 우 자 ! HP : 100HP : 80 VS
  • 10. Ø while을 이용한 코드작성 while(true) { cout << "지금 들어가면, 보스몬스터를 없애거나 당신이 죽을 때까지 나갈 수 없습니다." <<endl; cout << "입장하시겠습니까? 1.입장 2.도망" <<endl; cin >> iInput; if( 2 == iInput) break; //탈출조건 } ; 이미 던전에 들어왔습니다.! 반복되는 상황 ; 도망가기를 선택하면 반복문을 탈출 합니다. break; 키워드를 사용해서 반복문을 중단 할 수 있습니다.
  • 11. Ø while을 이용한 코드작성 1. 공격!! if( 1 == iInput) { int iInput = 0; while(true) { system("cls"); cout << "보스HP : " << iBossHp <<endl; cout << "내HP : " << iPlayerHp <<endl; cout << " ========================== " <<endl; cout << "1.공격 2.도망" << endl; cin >> iInput; if( 1 == iInput) { iBossHp -= iPlayerAtt; iPlayerHp -= iBossAtt; } if( 2 == iInput || iBossHp <= 0 || iPlayerHp <= 0 ) break; } } ; 보스존 입장 ; 한명이 살아 남을 때까지 아래의 메세지가 반복 출력 됩니다. ; 도망가거나, 둘 중 하나가 죽는 경우 반복문을 탈출 합니다.