# #C Programming (I) - 4
## Conditional Control
English: Lei Kuoliang
nicolaslouis@mail.fcu.edu.tw
Chinese(TW): Wang MingHong
---
### This week's course catalog
0. Review
1. Operator
* Relational operator
* Logical operator
2. Judgement sentence if
3. Standing flag
4. Conditional operator (ternary operator)
5. Judgment sentence switch
---
## Last week's course review
----
### Operator (operator)
* Arithmetic operator
* Assign operator (assignment operator)
* Incremental decrement operator
* bit operator
-----
* Relational operator (this week's course)
* Logical operator (this week's course)
----
* Arithmetic operator
* plus +, minus -, multiply \*, divide /, remainder %, parentheses ()
* Assign operator
* =, +=, -=, \*=, /=, %=
* = is not a mathematical equivalent, but the meaning of assignment and assignment.
* Incremental decrement operator
* a++, a\-\- (when placed at the back), will run first and then +1
* ++a, \-\-a (when placed in front), will run +1 first
----
* bit operator (one)
a, b individual bits are compared separately:
* Bit AND:
`0b11111111 & 0b01010101 = 0b01010101`
* Bit OR:
`0b00000000 | 0b01010101 = 0b01010101`
* Bit XOR:
`0b00001111 ^ 0b00110011 = 0b00111100`
----
* Bit operator (2)
* 1 complement: ~a
Each bit 1 of a becomes 0, and 0 becomes 1
* shift:a >> n, a << n
Each bit of a is shifted to the right by n bits, and each bit of a is shifted to the left by n bits.
---
## Review of the second question of homework last week
---
## Operator
* Relational operators (greater than, less than, equal to...)
* Logical operators (logical AND, logical OR...)
---
### Bool (Bulin value)
* true true
* false false
* The basic variable type of C language does not have bool
1 when the statement is true (true)
0 when the statement is false (false)
True if the value is not 0 (true)
Judged as false when the value is 0 (false)
---
### Relational operator
| Name | Operator | Grammar |
| -------- | -------- | -------- |
| Greater than | `>` | `a > b` |
| Greater than or equal to | `>=` | `a >= b` |
| Less than | `<` | `a < b` |
| Less than or equal to | `<=` | `a <= b` |
| equals | `==` | `a == b` |
| Not equal | `!=` | `a != b` |
* `==`(equal) Don't confuse with `=` (assignment)!
----
example:
```c=
#include <stdio.h>
#include <stdlib.h>
int main()
{
char upper_a = 'A'; //65
char lower_a = 'a'; //97
int compare = upper_a < lower_a;
printf("%d\n", compare);
return 0;
}
```
The output is "1"
On behalf of upper_a < lower_a is true (true)
---
### Logical operator
| Name | Operator | Grammar |
| -------- | -------- | -------- |
| logical AND | `&&` | `a && b` |
| Logic OR | `||` | `a || b` |
| Logic NOT | |!` | `!a` |
----
|||||
|-| -------- | -------- | -------- |
**Logical AND**| **`&&`** | **0** | **Non 0** |
| | **0** | 0 | 0 |
| | **Not 0** | 0 | 1 |
|||
**Logical OR**| **`||`** | **0** | **Non 0** |
|| **0** | 0 | 1 |
|| **Non 0** | 1 | 1 |
|||
**Logic NOT** |**`!`** | **0** | **Non 0** |
| | | 1 | 0 |
|||
----
Logical operators have no XOR
----
example:
```c=
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%d\n", (3 < 5) && (10 < 6));
printf("%d\n", (3 < 5) || (10 < 6));
printf("%d\n", !(3 < 5));
return 0;
}
```
Result
```
0
1
0
```
---
## Conditional sentence if
----
The sentence pattern in Chinese is: If...just...
Convert to C language:
```c
if(Conditional sentence){
Narrative sentence 1;
Narrative sentence 2;
...
}
```
If there is only one narrative, you can omit the braces:
```c
if(Conditional sentence)
Narrative sentence 1;
```
----
If the value in parentheses ++ is not 0++ (true), then the statement in if will be run.
If the value ++ in parentheses is 0++ (false), it will not run.
----
Example:
```c=
#include <stdio.h>
#include <stdlib.h>
int main()
{
if((3 < 5) && (10 < 6))
printf("(3 < 5) && (10 < 6) is true\n");/*0 in parentheses,
So it won't run this line*/
if((3 < 5) || (10 < 6)){
printf("(3 < 5) || (10 < 6) != 0\n");
printf("(3 < 5) || (10 < 6) is true\n");
}
return 0;
}
```
Result
```
(3 < 5) || (10 < 6) != 0
(3 < 5) || (10 < 6) is true
```
---
### Exercise 1
```
%
```
* Let the user enter 1 integer
* If it is a positive even number, print "positive even number"
If it is a negative even number, print "negative even number"
If it is 0, print "zero"
Otherwise, no text will be printed.
```
6
positive even number
-4
negative even number
0
zero
```
---
## Conditional clause if...else
And if...else if...else
----
### if...else
If...else...
```c
if(conditional sentence){
Narrative sentence 1;
Narrative sentence 2;
...
}
else{
Narrative sentence 1;
Narrative sentence 2;
...
}
```
If there is only one narrative, you can also omit the braces
----
### if...else if...else
If...else if...else...
```c
if(conditional sentence){
Narrative sentence 1;
Narrative sentence 2;
...
}
else if(conditional){
Narrative sentence 1;
Narrative sentence 2;
...
}
else{
Narrative sentence 1;
Narrative sentence 2;
...
}
```
----
Of course, you can also have an unlimited number of if
If you don't need the last else, you can not write
```c
if(day == 1)
printf("MON");
else if(day == 2)
printf("TUE");
else if(day == 3)
printf("WED");
else if(day == 4)
printf("THU");
else if(day == 5)
printf("FRI");
else if(day == 6)
printf("SAT");
else if(day == 7)
printf("SUN");
```
---
## Flag
Flag is a very important skill in writing programs.
The flag here does not mean that something is wrong and triggers a bad event.
Means setting up a status tag to make programming easier--++-
----
### Variable naming
* Avoid using flag directly as variable name
* If it is a logical variable (whether, true or false), you can add is in the variable name, such as: is_failed, is_input_correct
* If it is a variable used for counting, use count, such as: count_female, count_word
----
Example:

<!--
```c=
#include <stdio.h>
#include <stdlib.h>
Int main()
{
Int value;
Int count_positive = 0; //one flag for counting
//Enter an integer. If the number entered is negative, the counter ++ is repeated 3 times.
Scanf("%d", &value);
If(value < 0)
Count_positive++;
Scanf("%d", &value);
If(value < 0)
Count_positive++;
Scanf("%d", &value);
If(value < 0)
Count_positive++;
/ / judge and output the result
If(count_positive){ //The counter is not 0
Printf("You have been lineartestpilot ");
If(count_positive == 1) //execute when the counter is just 1
Printf("a positive number.\n");
Else / / when the counter is not 1
Printf("%d positive numbers.\n", count_positive);
}
Else //execute if the counter is 0
Printf("All the numbers you have beenRRC are positive.\n");
Return 0;
}
```
-->
---
### Conditional operator ? :
Or "ternary operator"
The only operator that can put 3 parameters
----
The effect is a bit like if...else
```c
Conditional formula ? Set return value : Failure return value
```
E.g:
```c
if(a > b)
max = a;
else
max = b;
```
Equivalent to
```c
max = (a > b) ? a : b;
```
<!--
Give another example (guarantee that the variable apple must be greater than 0):
```c
If(apple == 1)
Printf("I have an apple.\n");
Else
Printf("I have many apples.\n");
```
Can be changed to
```c
Printf("I have %s apple%s.\n", //%s is the format specified word of the character array
(apple == 1) ? "an" : "many", / / 1st %s, set up to fill in an, otherwise fill in many
(apple == 1) ? "" : "s"); //The 2nd %s, set up without filling, otherwise fill s
```
-->
---
### Exercise 2
The user enters an integer from 1 to 52, ** uses if to judge **, and outputs the corresponding punk name.
The colors are spade, club, heart, diamond
For example, enter 11 and output spade-J

----
Example 1
```
11
spade-J
Process returnd 0 (0x0)
Press any key to continue.
```
Example 2
```
40
diamond-A
Process returnd 0 (0x0)
Press any key to continue.
```
---
## switch case default
----
The function of switch case default is a bit like if...else if...else
The difference is that switch can only be used to determine the value of a variable.
Not as good as if you can customize the judgment.
----
The format of the switch, and the equivalent if

----
Use default and not use default:

<!--
```c
switch(day){
case 1:
printf("MON");
break;
case 2:
printf("TUE");
break;
case 3:
printf("WED");
break;
case 4:
printf("THU");
break;
case 5:
printf("FRI");
break;
default:
printf("WEEKEND");
}
```
-->
----
No break situation:

---
### Exercise 3
The user enters an integer from 1 to 52, ** uses the switch to judge **, and outputs the suit color corresponding to the puck.
The colors are spade, club, heart, diamond
For example, if you enter 11, you will output spade (just output the color)

----
Example 1
```
11
spade
Process returnd 0 (0x0)
Press any key to continue.
```
Example 2
```
40
diamond
Process returnd 0 (0x0)
Press any key to continue.
```
---
## Homework
----
Starting this week, the code for returning homework should be explained.
----
### Work requirements:
1. Continue the previous week's assignment 1 to determine if you want to add one more character. To add a role, let the user fill in the new role data, you must ++ check whether the data meets the data limit specification ++, if you enter f, w, g, a need to be converted to uppercase.
2. Select first attack, rear attack, and not select duplicate characters.
3. Run the process, output content and blank lines in the format of the sample
4. If there is an input error in the program, the program is terminated after the cause of the error is printed.
(Use **return 0;** in main to end the program)
----
* Data limit:
Attributes: F = fire, W = water, G = soil, A = wind. You cannot input English letters other than F, W, G, A, f, w, g, and a.
Blood volume 40~50 integer
Attack an integer from 15 to 20
Defense 5~10 integer
The sum of the three is between 65 and 70.
(The data of role 1 to role 3 must also comply with this limit)
----

----

---
## Supplement - switch judgment range
Supplement - switch judgment range switch can not only determine the corresponding value,
In fact, you can also judge the range of values.
----
Use "`...`" (note `...` and add a blank key to the left and right sides)
```c
switch (score){
case 90 ... 100 :
printf("A\n");
break;
case 80 ... 89 :
printf("B\n");
break;
case 70 ... 79 :
printf("C\n");
break;
default :
printf("F\n");
}
```
---
## Supplement - ctype.h
Library for determining character types
To use, please add at the top of the code
#include <ctype.h>
----
For example, some functions that determine the character type (return 1 or 0)
| Function | Function |
| -------- | ---------- |
Isalnum | Whether it is a letter or number |
| isalpha | whether it is a letter |
| islower | Whether it is lowercase |
| isupper | Whether it is a capital letter |
| isdigit | Whether it is a number |
Isxdigit | Whether it is a 16-digit number |
Ispunct | Whether it is punctuation (note that there are no spaces, line breaks) |
#include <ctype.h>
----
Convert English case function (passback character)
| Function | Function |
| -------- | ---------- |
Tolower|convert to lowercase
Toupper| convert to uppercase
----
Exercise 4 code using ctype.h
```c=
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char word;
while(scanf("%c%*c", &word) && word != '0'){//Enter '0' to leave the loop
if (islower(word)) //If lowercase
printf("upper is %c\n", toupper(word));
else if (isupper(word)) //If capitalized
printf("lower is %c\n", tolower(word));
}
return 0;
}
```
---
###### tags: `108 Ai-Mod-Eng-LKL`
{"metaMigratedAt":"2023-06-15T01:37:01.696Z","metaMigratedFrom":"YAML","title":"W4- Condition Control","breaks":true,"slideOptions":"{\"transition\":\"slide\"}","contributors":"[{\"id\":\"befaa4d9-75b6-4c05-baa7-7949e0ffa1e2\",\"add\":17118,\"del\":4536}]"}