- True or False에 대한 내용 - 'bool'이라는 특별한 변수형을 이용 => true이면 1, false이면 0을 내보냄 - 0이 아닌 다른 값을 내보내면, 일반적으로 true로 해석
If statement
[Simple if statement]
single statement / multiple statement
// 예시 코드
#include <iostream>
using namespace std;
int main () {
int dividend, divisor;
cout << "Please enter two integers to divide. : "
cin >> dividend >> divisor;
if(divisor!=0)
cout << dividend << "/" << divisor << "=" << dividend/divisor << endl;
}
위의 코드 진행 순서
[Simple if-else statement]
if - else statement 기본 구조
// 예시 코드
#include <iostream>
using namespace std;
int main () {
int dividend, divisor;
cout << "Please enter two integers to divide. : "
cin >> dividend >> divisor;
if(divisor!=0)
cout << dividend << "/" << divisor << "=" << dividend/divisor << endl;
else
cout << "Division by zero is not allowed\n";
}
[조건문에 사용되는 비교연산자]
[Nested if-else statement]
- 여러개의 조건을 한번에 처리 하기 위함
Nested if-else statement 구조
While statement
[Simple while statement]
single statement / multiple statement
// 예시 코드
#include <iostream>
using namespace std;
int main () {
int count = 1;
while (count <=5){ // count가 5일 때 까지 반복
cout << count << endl;
}
}
[If + While statement]
// 예시 코드
#include <iostream>
using namespace std;
int main () {
int input = 0, sum = 0;
cout << "Enter numbers to sum, negative number ends list : ";
while (input >= 0) {
cin >> input
if (input >= 0)
sum += input;
}
cout << "Sum = " << sum << endl;
}
[Nested while statement]
Nested while statement 구조
참고사항)
- 아래의 3가지 표현법은 모두 동일
// 1) 추천 양식
if (x < 10)
y=x;
// 2)
if (x < 10) y=x;
// 3)
if (x < 10)
y=x;