Comparison Operators (See Basic C programming 2):
a > b
: Whether a
is greater than b
.a >= b
: Whether a
is greater than or equal to b
.a < b
: Whether a
is less than b
.a <= b
: Whether a
is less than or equal to b
.a == b
: Whether a
and b
are equal.a != b
: Whether a
and b
are not equal.Combination of multiple condition:
&&
condition 2: Whether both of the two conditions are satisfied.||
condition 2: Whether one of the two conditions is satisfied.#include <stdio.h>
int main(){
int a1, a2;
// Input two value
printf("Input two values a1 and a2\n");
scanf("%d", &a1);
scanf("%d", &a2);
// If statement
if(a1 > 3 && a2 > 3){
printf("a1 and a2 are greater than 3.");
}else{
printf("not satisfied");
}
return 0;
}
a1 > 3 && a2 > 3
is condition consisting of a1 > 3
and a2 > 3
.If
statement is processed.&&
operator, we can arrange multiple conditions, e.g., a1 > 3 && a2 > 3 && a3 > 3 &&...
.>=
, <
, <=
, ==
, !=
.#include <stdio.h>
int main(){
int a1, a2;
// Input two value
printf("Input two values a1 and a2\n");
scanf("%d", &a1);
scanf("%d", &a2);
// If statement
if(a1 > 3 || a2 > 3){
printf("a1 or a2 is greater than 3.");
}else{
printf("not satisfied");
}
return 0;
}
a1 > 3 || a2 > 3
is the condition consisting of a1 > 3
and a2 > 3
.If
statement is processed.||
operator, e.g., a1 > 3 || a2 > 3 || a3 > 3 ||...
.>=
, <
, <=
, ==
, !=
.If
statement in If
statement.#include <stdio.h>
int main(){
int a1, a2;
// Input two value
printf("Input two values a1 and a2\n");
scanf("%d", &a1);
scanf("%d", &a2);
// first If statement
if(a1 > 3){
printf("a1 is greater than 3.\n");
// second If statement
if(a2 > 3){
printf("a1 and a2 are greater than 3.");
}
}else{
printf("not satisfied");
}
return 0;
}
If
statement, the first condition a1 > 3
is checked. When the first condition is satisfied, the second condition a2 > 3
is checked by the second If
statement.If
statement in If
statement are called Nest.>=
, <
, <=
, ==
, !=
.#include <stdio.h>
#include <stdlib.h> // for "rand" function
#include <time.h> // for "time" function
int main() {
srand(time(0)); // base random seed (only write one time)
int r;
int max_rand = 5;
int min_rand = 3;
// 1st random
r = rand() % (max_rand - min_rand + 1) + min_rand; // generate random number
printf("r = %d\n", r);
// 2nd random
r = rand() % (max_rand - min_rand + 1) + min_rand; // generate random number
printf("r = %d\n", r);
return 0;
}
#include <stdlib.h>
, #include <time.h>
, and srand(time(0));
only one time.rand() % (max_rand - min_rand + 1) + min_rand
, random number from min_rand
to max_rand
can be generated.min_rand
and max_rand
, run the code, and check results.If
statement, we can make simple game.#include <stdio.h>
#include <stdlib.h> // for "rand" function
#include <time.h> // for "time" function
int main() {
// random settings
srand(time(0));
int max_rand = 6;
int min_rand = 1;
// scores
int your_score;
int npc_score;
// getting scores
your_score = rand() % (max_rand - min_rand + 1) + min_rand;
npc_score = rand() % (max_rand - min_rand + 1) + min_rand;
// result
printf("You: %d \n", your_score);
printf("NPC: %d \n", npc_score);
if(your_score > npc_score){
printf("You win!");
}else if(npc_score > your_score){
printf("NPC win!");
}else if(npc_score == your_score){
printf("Same score");
}
return 0;
}
rand()
function gives random values to integer variables your_score
and npc_score
.If
statement.printf
function.a
、b
、c
. After that, input specific values from keyboard into these variables and when all values are same, show same values.a
、b
、c
. After that, input specific values from keyboard into these variables and when all values are not same, show Different values.printf
function.scanf
function, and show winners name.