MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Comparison operators

Equality operator
In the previous section, we introduced the == operator, and operators of this type are called "equality operators."
There are two types of equality operators available.

symbol 真になる 偽になる
== 2つの値が等しい 2つの値が等しくない
!= 2つの値が等しくない 2つの値が等しい

These operators are used to compare whether values are equal or not.
The following program checks if the input is 10.

Source code
#include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);
    if (suuti == 10)
        printf("入力値は 10 is.\n");
    if (suuti != 10)
        printf("入力値は 10 ではありません。\n");
    return 0;
}

"If you run this program and enter 10 as input, the result will be as follows:"

Execution results
10 入力したデータ
入力値は 10 is.

If you run this program and enter a number other than 10, the result will be as follows:

Execution results
135 入力したデータ
入力値は 10 ではありません。


As it is an operator.
These comparison operators, as well as the relational and logical operators that will be explained next, are also operators.
You can simply place it in an equation and use it for calculations.

次の数式は、variable val1 と val2 が
等しいHourには answer に(多くのCompilerでは)1 、
The value 0 is assigned when they are not equal.
answer = val1 == val2;

上級者にはこの様なやり方を好む人もいますが、
While it's easy to understand, I wouldn't recommend it.

Relational operators
The equality operator was an operator used to determine if two values were equal.
Relational operators are used to compare the relative size of two values.

symbol 真になる 偽になる
< 左の値が右より小さい 左の値が右より小さくない
> 左の値が右より大きい 左の値が右より大きくない
<= 左の値が右以下 左の値が右以下ではない
>= 左の値が右以上 左の値が右以上ではない

It may seem obvious, but the difference with being small is...
It's a difference of whether or not it includes equalities."Less than or equal to and greater than or equal to also return true when they are equal."

These operators are used to compare the relative size of two values.
The following program checks if the input number is greater than 10.

Source code
#include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);

    if (suuti == 10)
        printf("入力値は 10 is.\n");

    if (suuti > 10)
        printf("入力値は 10 より大きいis.\n");

    if (suuti < 10)
        printf("入力値は 10 より小さいis.\n");

    return 0;
}

"If you run this program and enter 10 as input, the result will be as follows:"

Execution results
10 入力したデータ
入力値は 10 is.

"The result when running this program with an input greater than 10 is as follows:"

Execution results
135 入力したデータ
入力値は 10 より大きいis.

If you run this program with an input less than 10, the result will be as follows:

Execution results
5 入力したデータ
入力値は 10 より小さいis.

Logical operators
Logical operators are operators with a slightly different nature than the operators we've seen so far.
It can be used to combine multiple conditions or to invert a judgment.

symbol 意味 真になる 偽になる
&& かつ(AND) 右と左の条件が両方真 右と左の条件のどちらか片方でも偽
|| または(OR) 右と左の条件のどちらか片方でも真 右と左の条件が両方偽
! 否(NOT) 条件が偽 条件が真

In if statements, you can only evaluate one condition at a time.
By using these operators, you can make judgments based on multiple conditions.
The following program checks if an input number is between 8 and 12.

Source code
#include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);

    if (suuti >= 8 && suuti <= 12)
        printf("8~12の間is.\n");

    if (!(suuti >= 8 && suuti <= 12))
        printf("8~12の間ではありません。\n");

    return 0;
}


"If you run this program and enter a number between 8 and 12, the result will be as follows:"

Execution results
9 入力したデータ
8~12の間is.

If you run this program and enter a number outside the range of 8-12, the result will be as follows:

Execution results
3 入力したデータ
8~12の間ではありません。

The key to this program lies in the condition part of the if statement.
Since an if statement can only evaluate one condition, it's not possible to determine whether a value is 8 or greater and 12 or less using a typical approach.
However, the && operator allows for evaluation based on two conditions.

"The second if statement's condition is simply the first condition with a negation operator added."
The ! operator is an operator that inverts the result of a judgment.
"It will produce the exact opposite result from the first if statement."


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19