---
# System prepended metadata

title: Logic

---

# Logic

## Logic operation

* `==`
`==` operation will check if two arguments is the same , return `1` if it is true  , return `0` if it is false

Example : 
```clike
int a = 1 ;
int b = 2 ;
int c = 1 ;
char d = 'a' ;
char e = 'a' ;
int cond0 = (a == b) ;
int cond1 = (a == c) ;
int cond2 = (d == e) ;
```
```clike
cond0 : 0
cond1 : 1
cond2 : 1
```
* `>`
    `>` operation check if the left one is larger than right one , similarly , return `1` if it is true and return `0` if it isn't .
    
Example
```clike
int a = 1 ;
int b = 2 ;
int c = 1 ;
int cond = (a > b) ;
int cond1 = (b > a);
```

```clike
cond0 : 0
cond1 : 1
```

* `<`
    `<` operation check if the left one is smaller than right one , similarly , return `1` if it is true and return `0` if it isn't .

Example
```clike
int a = 1 ;
int b = 2 ;
int c = 1 ;
int cond = (a < b) ;
int cond1 = (b < a);
```

```clike
cond0 : 1
cond1 : 0
```
* `>=` , `<=`
    `>=` and `<=` is similar as former two operator , but it includes the `==` situation

Example
```clike
int a = 1 ;
int b = 2 ;
int c = 1 ;
int cond0 = (a <= b) ;
int cond1 = (a <= c) ;
int cond2 = (b >= a) ;
```

```clike
cond0 : 1
cond1 : 1
cond2 : 1
```

* `!`



### String 
For a char arrary , there exist a library `string.h` to compare or do the operation between two string .

* **strcmp**
    compare two string ,and if they are the same , return `0` .

Example str.c : 
```clike=
#include <stdio.h>
#include <string.h>

int main(){   

    char * test = "hello" ;
    char * test2 = "hello" ;
    int cond = strcmp(test , test2) ;
    printf("%d\n" , cond) ;
    
	return 0 ;
}
```
## If 
`if` is a logic gate to determine whether to do the specific operation .

### Format
Single `if` 
```clike
if( condition ){
    operation ;
}

```

Mutiple related `if`

```clike
if( condition ){
    operation ;
}
else if ( condition2 ){
    operation2 ;
}
else if( condition3 ){
    operation3 ;
}




```

`else` gate is all the other situation , except which inside `if` and `else if` 
```clike
if( condition ){
    operation ;
}
else if ( condition2 ){
    operation2 ;
}
else if( condition3 ){
    operation3 ;
}
else{
    exception operation // 例外處理
}
```
A cleaner way to write `if` when the operation has only one line .

```clike
if (cond)
    operation ;
else if (cond)
    operation ;
```
### Condition
The `condition` in **Format** can replace by the logic  , variables or combination of logic . The pogram will run the operation in the block if the statements equals to `1` .

* **Logic Example** 

```clike
if(a > b){
    printf("a is bigger than b") ;
}
```

* **Variables Example**
```clike
int a = 1 , b = 2 ;
int cond = (b < a) ;
if(cond){
    printf("b is smaller than a") ;
}
```

* **Combinations**
Before showing the example , there are two way to combine .

1 . `&&` and : 
if there are a and b two statements , a and b are both true .`a && b` will return `1`  , otherwise `a && b` will return `0` .
2 . `||`  or : 
if there are a and b two statements , either a and b is true . `a || b` will return `1` , otherwise `a || b` will return `0` .

Example : 
Using `if` gate to check if `c` is the biggest ,and `a` is the smallest
```clike
int a = 1 ;
int b = 2 ;
int c = 3 ;
if(c > a && c > b)
    printf("c is the biggest")
if(a > c || a >　b)
    printf("a is not the smallest")

```
### Important Notification

If the `cond` part we put an operation in  , then it will return `1` if is success  , and any not-zero variable will be see as true aka `1` .

Example : 
Using `if` gate to check if `a` is the smallest non-zero variable .
```clike
int a = 1 ;
int b = 2 ;
int c = 3 ;
if(a < c && a < b && a)
    printf("a is the smallest and isn't zero")
```


Example 2 
```clike
int a = 5 ;
if(a++ && a)
    printf("Sucess") ;
```


## Application of If

### Password system

If you want your program to have privacy , you can add a gate to check if the user has the authority to use the program .

```clike=
#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{	
	char * mypassword = "hahahaowo" ;
	char password[100] ;
	scanf("%s" , password) ;
	if(!strcmp(password , mypassword))
		printf("Login sucess\n");
	else
		printf("You don't have the authority\n") ;
	
	return 0;
}
```

New function 

`strcmp` :　 compare two string , if is the same  return `0` .

### Print out specific sentence

The program below can print out specific sentence by choosing the number .
```clike=
#include <stdio.h>

int main()
{
	int a ;
	printf("Select the sentence (1 ~ 3):　");
	scanf("%d" , &a) ;
	if( a == 1)
		printf("Hi it is 05:21 p.m now .\n") ;
	else if (a == 2)
		printf("How are you today ?\n") ;
	else if (a == 3)
		printf("I am busy now ! \n") ;
	else
		printf("Wrong number\n") ;
	return 0;
}
```

## Switch
`switch` is another way to implement the operation due to mutiple situation .

### Format

Swtich can identify which situation that variable satisifed .
```clike
switch(variable){
    
    case  (situation) :
        operation ;
        break ;
    case  (situation2) :
        operation2 ;
        break ;
    case  (situation3) :
        operation3 ;
        break ;
    default :
        exceptional operation
        break ;
}
```
We can also put in a value operation , which the program will calculate out and detect the result as `variable` we introduced above .

```clike
switch(value operation){
    
    case  (situation) :
        operation ;
        break ;
    case  (situation2) :
        operation2 ;
        break ;
    case  (situation3) :
        operation3 ;
        break ;
    default :
        exceptional operation
        break ;
}
```
### Example
```clike
int a = 5 ;
switch(a){
    case 5 :
        printf("a is 5\n") ;
        break ;
    default :
        printf("a isn't 5\n") ;
        break ;
}
```

```clike
int a = 5 ;
switch(a % 2){
    case 0 :
        printf("a is even number\n") ;
        break ;
    case 1 :
        printf("a is odd number\n") ;
        break ;
    default :
        break ;
}
```

### Application of Switch
We can use switch to achieve the same goal as we shown at application 2 .

```clike=
int main()
{
	int a ;
	printf("Enter your option (1 ~ 3)\n");
	scanf("%d" , &a) ;

	switch(a){
		case 1 :
			printf("It is 07:45 pm now \n") ;
			break ;
		case 2 :
			printf("Today is thursday !\n") ;
			break ;
		case 3 :
			printf("kkkkkkk \n");
			break ;
		default :
			printf("wrong number\n");
	}

	return 0;
}
```