1-if Statement
if and if-else
- Statements are exected conditionally
# if.cpp
#include <iostream>
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;
}
- When will “Where I’m?” be printed?
- How to make the code easier to understand
? : operator
- When can we use the ternary conditional operator?
#ternary.cpp
#include <iostream>
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;
}
2-Conditions
Condition
- What should be a condition
int num = 10;
if (num < 5)
cout << "The number is less than 5." << endl;
- The condition should be an expression which is convertible to bool
Its value can be bool, char, int, float
Relational Expressions
- The condition can be a relational expression
- The 6 relational/comparison operators
- Return 1 if the condition (such as a == b) is true
- Return 0 if the condition is false
Logical Expressions
-
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;
Non-boolean Expressions
- They will be converted to bool implicitly if it is feasible.
float count = 0.2f;
if (count). // not recommend to use a float-point number
cout << "There are some." << endl;
- Pointers are also frequently used as conditions
int * p = new int[1024];
if (!p) // if (p == NULL)
cout << "Memory allocation failed." << ends;
3- While loop
While loop
-Syntax:
While (expression)
{
}
- If the condition is true, the statement (loop body) will be executed
#while.cpp
#include <iostream>
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;
}
do-while loop
- The test takes place after each iteration in a do-while loop
- The test takes place before each iteration in a while loop
#include <iostream>
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;
}
break statement
- Terminate a loop
#include <iostream>
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;
}
continue statement
- Skip the remaining part of the loop body and continue the next iteration
#include <iostream>
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;
}
死循环!
The Condition, Be Careful
- Can you find any problem from the code?
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)
why?
- Expression 3+4 has a value
- Expression a+b has a value
- Expression (a==b) has value (true or false)
- a = b is an assignment, also an expression and has a value
- The follow code can be compiled successfully
int b = 0;
int m = (b = 8);
cout << "m=" << m << endl;
4- for loop
for loop
- Syntax:
for (init-clause; cons-expression; iteration-expression)
loop-statement
- Example
#for.cpp
#include <iostream>
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;
}
for loop VS while loop
Endless loop
- Sometimes we need it
5-goto-switch
goto statement
- Jump to a desired location
- An unrecommended statement
#goto.cpp
#include <iostream>
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;
}
switch statement
- Execute one of several statements, depending on the value of a condition
- break prevents to execute some following statements. Don’t forget break!
- More similar with goto, not if-else
#include <iostream>
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;
}
}