In this class, I learned the conditional statement 'Switch' case.

When setting conditions for an specific action, 'Switch' case complete the condition statement by directly specifying each condition.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    switch(value) {
    	case value1:
        	action1;
        break;
        
        case value2:
        	action2;
        break;
        
        case value3:
        	action3;
        break;
    
    return 0;
}

 

I learned the conditional statement including 'if' and 'else'.

When setting conditions for an action, we can define the action by dividing it into 'if', 'elseif', and 'else' syntax.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    if (Condition 1) {
    	Action 1;
    }
    else if (Condition 2) {
    	Action 2;
    }
    else (Condition 3) {
    	Action 3;
    }
    
    return 0;
}

The action is executed after checking the conditions sequentially from the top. In other words, if the first condition of the if statement is true, the 'Action 1' code will be performed. On the other hand, if the if statement is false, the condition of the next elseif statement is verified, and 'Action 2' code is performed when true.

 

To help you understand the if statement, we have written an example code that grades according to your test scores.

Score Grade
90 ~ 100 A
80 ~ 90 B
Less than 80 C
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
    int Score;
    char Grade;
    
    if (Score > 90 ) {
    	Grade = 'A';
        printf('Your grade is %C', Grade);
    }
    else if (Score > 80 && Score <= 90) {
    	Grade = 'B';
        printf('Your grade is %C', Grade);
    }
    else (Score <= 80) {
    	Grade = 'C';
        printf('Your grade is %C', Grade);
    }
    
    return 0;
}

 

 

If the score is 95, the first condition is verified as 'TRUE'. Therefore, the grade is assigned 'A'.
And if the score is 75, two conditons are verified as 'FALSE' and last condition is verified as 'TRUE'.
So, the grade is assigned 'B'.

In this class, I learn the bitwise shift operator including '<<' and '>>'.


1. Right-shift operator (>>)
    - The right-shift operator (<<
) moves the bits of an integer or enumeration type expression to the right.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int A = 0b1000;
    
    A = A >> 2;
    
    printf('%x', A);    
    
    return 0;
}

The results showed 'A' as 0b0010 that which resulted in change of 2^(-2).



2. Left-shift operator (<<) 
    - The left-shift operator (<<) moves the bits to the left.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int A = 0b0001;
    
    A = A << 2;
    
    printf('%x', A);    
    
    return 0;
}

The results showed 'A' as 0b0100 that which changed by the square of 2.

In this class, I learned about the logical operators (e.g. &&, ||, !) and bit operators (&, |, ~, ^).

1. Logical operators
    - AND (&&): If A and B are true, it is denoted as 'A && B'
    - OR (||): If A or B are true, it is denoted as 'A || B'
    - NOT (!): Non-A values output as a result, it is denoted as '!A'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int A = 1, B = 0, C = 1;
    int Result1, Result2, Result3, Result4, Result5, Result6
    
    Result1 = A && B;
    Result2 = A && C;
    Result3 = A || B;
    Result4 = B || C;
    Result5 = !A;
    Result6 = !B;
    
    print('%d \n', Result1);
    print('%d \n', Result2);
    print('%d \n', Result3);
    print('%d \n', Result4);
    print('%d \n', Result5);
    print('%d', Result6);
    
    return 0;
}


2. Bit operators
    - AND (&): Calculate each bit for AND
    - OR (|): Calculate each bit for OR
    - NOT (~): Calculate each bit for NOT

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int A = 0b1100, B = 0b1010;
    int Result1, Result2, Result3;
    
    Result1 = A & B;
    Result2 = A | B;
    Result3 = ~A;
    
    printf('%x \n', Result1);
    printf('%x \n', Result2);
    printf('%x', Result3);
        
    return 0;
}

 

In this class, I learned about the operators that enable calculate or compare the variables.
Here's four types of operators that easily classified to explan their functions.

1. Assignment operator
    - 'A = B' means that value 'B' should be assigned to variable 'A'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    a = 123;
    printf('%d', a);
    
    return 0;
}

 

 

2. Relational operator
    - Comparing the relationship between left and the right and results showed whether the operator is true or false.
    - Examples: 'A > B', 'A >= B', 'A! = B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    int b;
    
    a = 123;
    b = 456;
    
    printf('%d \n', a > b);
    printf('%d \n', a >= b);
    printf('%d \n', a == b);
    printf('%d \n', a != b);
    
    return 0;
}

 

3. Arithmetic operator
    - Examples: 'A + B', 'A - B', 'A * B', 'A / B', 'A % B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 123 + 1;
    printf('%d \n', a);
    
    a = 123 - 2;
    printf('%d \n', a);
    
    a = 123 * 3;
    printf('%d \n', a);
    
    a = 123 / 10;
    printf('%d \n', a);
    
    a = 123 % 7;
    printf('%d \n', a);
    
    return 0;
}

 

4. Incremental operator
    - Examples: 'A ++' means 'A = A + 1 and 'A --' means 'A = A - 1'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 1;
    printf('%d \n', a);
    
    a = a + 1;
    printf('%d \n', a);
    
    a++;
    pritnf('%d \n', a);
    
    a--;
    printf('%d \n', a);
    
    return 0;
}

 

5. Compound substitution operator
    - Examples: 'A += B' means 'A = A + B' and 'A -= B' means 'A = A - B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 1;
    printf('%d \n', a);
    
    a = a + 2;
    printf('%d \n', a);
    
    a += 2;
    printf('%d \n', a);
    
    return 0;
}

+ Recent posts