# if.cpp
#include
using namespace std;
int main()
{
int num = 10;
if (num < 5)
cout << "The number is less than 5. " << endl;
if (num == 5 )
{
cout << "The number is 5." << endl;
}
else
{
cout << "The number is not 5." << endl;
}
if (num < 5)
cout << "The number is less than 5." << endl;
else if (num > 10)
cout << "The number is greater than 10." << endl;
else
cout << "The number is in range [5, 10]." << endl;
if(num < 20)
if(num < 5)
cout << "The number is less than 5" << endl;
else
cout << "Where I'm?" << endl;
int * p = new int[1024];
if (p)
cout << "Memory has been allocated." << endl;
return 0;
}
#ternary.cpp
#include
using namespace std;
int main()
{
bool isPositive = true;
int factor = 0;
//some operations may change isPositive's value
if(isPositive)
factor = 1;
else
factor = -1;
//the if-else statement can be replaced by a ternary conditional operation
factor = isPositive ? 1 : -1;
//sometimes the following code can be more efficient.
factor = isPositive * 2 - 1;
return 0;
}
int num = 10;
if (num < 5)
cout << "The number is less than 5." << endl;
Its value can be bool, char, int, float
If an operands is not bool, it will be converted to bool implicitly
Precedence: ! > && > ||
What’s the value of the follow expression?
if (-2 && true)
cout << "The condition is true." << endl;
if (!-2)
cout << "(!-2) is true, really?" << endl;
float count = 0.2f;
if (count). // not recommend to use a float-point number
cout << "There are some." << endl;
int * p = new int[1024];
if (!p) // if (p == NULL)
cout << "Memory allocation failed." << ends;
-Syntax:
While (expression)
{
}
#while.cpp
#include
using namespace std;
int main()
{
int num = 10;
while(num > 0)
{
cout << "num = " << num << endl;
num--;
}
// num = 10;
// do
// {
// cout << "num = " << num << endl;
// num--;
// }while (num > 0);
// num = 10;
// while (num > 0)
// {
// if (num == 5)
// break;
// cout << "num = " << num << endl;
// num--;
// }
return 0;
}
#include
using namespace std;
int main()
{
// int num = 10;
// while(num > 0)
// {
// cout << "num = " << num << endl;
// num--;
// }
int num = 10;
do
{
cout << "num = " << num << endl;
num--;
}while (num > 0);
// num = 10;
// while (num > 0)
// {
// if (num == 5)
// break;
// cout << "num = " << num << endl;
// num--;
// }
return 0;
}
#include
using namespace std;
int main()
{
// int num = 10;
// while(num > 0)
// {
// cout << "num = " << num << endl;
// num--;
// }
// int num = 10;
// do
// {
// cout << "num = " << num << endl;
// num--;
// }while (num > 0);
int num = 10;
while (num > 0)
{
if (num == 5)
break;
cout << "num = " << num << endl;
num--;
}
return 0;
}
#include
using namespace std;
int main()
{
// int num = 10;
// while(num > 0)
// {
// cout << "num = " << num << endl;
// num--;
// }
// int num = 10;
// do
// {
// cout << "num = " << num << endl;
// num--;
// }while (num > 0);
// int num = 10;
// while (num > 0)
// {
// if (num == 5)
// break;
// cout << "num = " << num << endl;
// num--;
// }
int num = 10;
while (num > 0)
{
if (num == 5)
continue;
cout << "num = " << num << endl;
num--;
}
return 0;
}
死循环!
size_t num = 10;
while(num >= 0)
{
cout << "num = " << num << endl;
num--;
}
死循环!!
size_t. 无符号, 减到0再减一则变成最大的数,没有负数,则循环停不下来
bool flag = true;
int count = 0;
while (flag = true)
{
cout << "Count = " << count++ << endl;
// and do sth
if (count == 10) // meet a condition
flag = false; // set flag to false to break the loop
}
while (flag = true)
int b = 0;
int m = (b = 8);
cout << "m=" << m << endl;
for (init-clause; cons-expression; iteration-expression)
loop-statement
#for.cpp
#include
using namespace std;
int main()
{
int sum = 0;
for(int i = 0; i < 10; i++)
{
sum += i;
cout << "Line " << i << endl;
}
cout << "sum = " << sum << endl;
return 0;
}
#goto.cpp
#include
using namespace std;
float mysquare(float value)
{
float result = 0.0f;
if(value >= 1.0f || value <= 0)
{
cerr << "The input is out of range." << endl;
goto EXIT_ERROR;
}
result = value * value;
return result;
EXIT_ERROR:
//do sth such as closing files here
return 0.0f;
}
int main()
{
float value;
cout << "Input a floating-point number." << endl;
cin >> value;
float result = mysquare(value);
if (result > 0)
cout << "The square is " << result << "." << endl;
return 0;
}
#include
using namespace std;
int main()
{
unsigned char input_char = 0;
cout << "Please input a character to start." << endl;
cin >> input_char;
while (input_char != 'q')
{
switch (input_char)
{
case 'a':
case 'A':
cout << "Move left. Input 'q' to quit." << endl;
break;
case 'd':
case 'D':
cout << "Move right. Input 'q' to quit." << endl;
break;
default:
cout << "Undefined key. Input 'q' to quit." << endl;
break;
}
cin >> input_char;
}
}