Comparison Operators:
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.By using if
statements, we can implement processing that depends on the state of variables.
#include <stdio.h>
int main(){
int a = 5;
printf("a is %d \n", a);
if(a > 3){ // If statement
printf("a is greater than 3.");
}
return 0;
}
printf
function when a
is greater than 3.int a = 2
and check for whether printf
function is processed or not.>=
, <
, <=
, ==
, !=
.To add a process for when the condition is not satisfied, use else
statement.
#include <stdio.h>
int main(){
int a = 1;
printf("a is %d \n", a);
if(a > 4){ // If statement
printf("a is greater than 4.");
}else{ // else statement
printf("a is not greater than 4.");
}
return 0;
}
a>4
. Therefore, only the inside of the else
statement is run.int a = 5
and check result.>=
, <
, <=
, ==
, !=
.if
statement is not satisfied, we can give another condition by using elseif
statement.#include <stdio.h>
int main(){
int a = 1;
printf("a is %d \n", a);
if(a > 4){ // If statement
printf("a is greater than 4.");
}else if(a > 2){ // else if statement
printf("a is greater than 2.");
}else if(a > 0){ // else if statement
printf("a is greater than 0.");
}
return 0;
}
else if
statements can be used.a
and check results.>=
, <
, <=
, ==
, !=
.else
statement can be added to an If-elseif
statement.#include <stdio.h>
int main(){
int a = -10;
printf("a is %d \n", a);
if(a > 4){ // If statement
printf("a is greater than 4.");
}else if(a > 2){ // else if statement
printf("a is greater than 2.");
}else if(a > 0){ // else if statement
printf("a is greater than 0.");
}else{ // else statement
printf("a is NOT greater than 0");
}
return 0;
}
else
statement is added at the end of all conditions.else
statement is the process for that all conditions are not satisfied.a
and check results.>=
, <
, <=
, ==
, !=
.The following is code for input value from keyboard and substituting it to a
, after that, checking whther a
is greater than 4 or not.
#include <stdio.h>
int main(){
// input from key board
int a;
printf("Input: ");
scanf("%d", &a);
printf("a is %d \n", a);
// If-else statement
if(a > 4){
printf("a is greater than 4.");
}else{
printf("a is NOT greater than 4");
}
return 0;
}
/
of them. If calculation result is greater than 5, display greater than 5
. If it is not satisfied that condition, display not satisfied
.#include <stdio.h>
int main(){
double a1, a2, result;
int ope;
// Select operator
printf("Select operator [1: +, 2: -, 3: *, 4: /]\n");
scanf("%d", &ope);
// Input two value
printf("Input two values a1 and a2\n");
scanf("%lf", &a1);
scanf("%lf", &a2);
// Results
if(ope == 1){
result = a1 + a2;
printf("a1 + b2 = %lf", result);
}else if(ope == 2){
result = a1 - a2;
printf("a1 - b2 = %lf", result);
}else if(ope == 3){
result = a1 * a2;
printf("a1 * b2 = %lf", result);
}else if(ope == 4){
result = a1 / a2;
printf("a1 / b2 = %lf", result);
}else{
printf("Warning: Select only from 1 to 4! \a");
}
return 0;
}
(3) Enter the width and height by using scanf
, after that solving the area of a triangle or rectangle. The user can select a triangle or rectangle from keyboard like assignment (1).
(4) Create a slightly complex and practical code using if
, if-else
, if-elseif
or if-elseif-else
statements and various comparison operator.
Submission: Please submit following materials.