영넌 개발로그
[C++ 기초] 반복문 for loop / 논리 연산자 logical_operator 본문
Range based for loop
동적 배열에 대해서는 동작하지 않는다. (방법이 따로 있음)
- for ( int k : 배열이름) {}
배열 내 데이터 타입 말고 double과 같은 형식으로 써도 저절로 캐스팅 연산 됨
int main()
{
int a[5] = { 1,2,3,4,5 };
for (int k : a)
{
cout << k << endl;
}
return 0;
}
2. for (auto k: 배열이름) {}
내가 배열 내 데이터 타입을 잘 모를 때 사용하기 좋음
int main()
{
int a[5] = { 1,2,3,4,5 };
for (auto k: a)
{
cout << k << endl;
}
return 0;
}
Logical Operator
- &&: AND
- || : OR
- ! : NOT
-
and, or, not 지원 (소문자)
int main()
{
int a(10), b(20); //c++표현법 int a=10,b=20
if ((a < b) and (b & 2 == 0)) {
cout << "1" << endl;
}
else if ((a < b) or (b % 2 == 0)) {
cout << "2" << endl;
}
else if (not(a < b)) {
cout << "3" << endl;
}
return 0;
}
'코딩 > C++' 카테고리의 다른 글
[C++ 기초] text file 읽고 쓰기, fstream 헤더 (0) | 2020.10.23 |
---|---|
[C++ 기초] cctype 헤더파일 (0) | 2020.10.23 |
[C++ 기초] 함수 호출 inline function (0) | 2020.10.23 |
[C++ 기초] 함수 오버로딩 function overloading (0) | 2020.10.23 |
Comments