MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Statements that make comparisons

conditional judgment
All the programs we have created so far have been one-way programs.
In other words, it executes statements in the order they are written, from beginning to end, and
It was the simplest mechanism, ending when there were no more statements.

This would work for a really simple program.
If you try to make the process any more complicated, you will quickly get stuck.

The solution to this problem is to change the content of the process depending on the conditions.

Lunch decisions
If you have money to spare, then the Yakiniku Set Meal
If you can't afford it, then plain udon

The decision will need to be based on conditions such as

In the computer world, a condition is nothing more than a numerical value.
More specifically, a comparison of two numbers can be a condition.
Statements that make conditional judgments
The C language provides the if statement as a statement that makes a decision based on a condition.
The if statement compares the values of two numbers and divides the process based on the result.
The usage of the if statement is as follows

How to use if statements
. if (Conditional) statement ;

The comparison of numbers with an if statement is very clear and simple.
That is, it only determines whether a given number is 0 or not.
In C, when making a decision based on a condition, 0 is called a false value and non-zero is called a true value.

Keywords.
True value]

A conditional judgment call, meaning a non-zero number (even a negative number).
Sometimes denoted as true.



Keywords.
False value

A conditional judgment call, meaning 0.
Sometimes denoted as false.


In an if statement, the horizontal statement is executed only if the specified number is true (the number is non-zero).
Otherwise, the statement is skipped and the statement after the if statement is executed.
The following program displays a number only when the number is true (the number is non-zero).

source code
 #include <stdio.h>

int main(void)
{
    int suuti = 10;
    if (suuti)
        printf("%d\n", suuti);
    return 0;
}

The result of executing this program will be as follows

Execution Result
10

In this program, int suuti=10; but
This is the way to write the assignment at the same time as the declaration.
This writing method is sometimes called initialization because the value of the variable can be determined first.

Keywords.
Initialization

Declaring a variable and assigning a numerical value at the same time.


In this program, if the value of suuti is set to 0, nothing is displayed, but if the value of
When set to any other value, the value is displayed.
Operators for comparison
From reading the previous section, the if statement may appear to be a rather useless statement.
In any case, it can only determine whether the value is 0 or not.

However, more sophisticated comparisons are possible by combining ordinary calculations with if statements.
For example, if you subtract numbers that have the same value from each other, the answer will naturally be 0.
This property makes it possible to determine the value by subtraction.
The following program uses this property to determine if the input number is 10.

Source Code
 #include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);
    if (suuti - 10)
        printf("Input value is not 10. \n");
    return 0;
}

If you let this program run and enter 10, the result will be as follows

Execution Result
10 Input data

If you run this program and enter anything other than 10, the results will be as follows

Execution Result
135 Data entered
Input value is not 10.

More complex applications of this method allow for very sophisticated comparisons.

However, it is tedious to say that one must subtract to find out if a variable has a value of 10 or not.

Therefore, the C language provides dedicated comparison operators.
To check whether two numbers are equal, use the == operator.
This operator performs the calculation that the result is true when two values are equal.

== and ==
Even professionals often inadvertently mistake = and ==.
= is an assignment to the left variable, while == is a comparison of the left and right numbers to see if they are equal.
If it doesn't work as expected, suspect this first.

The following program is an example of using the == operator to check whether the input value is 10 or not.

source code
 #include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);
    if (suuti == 10)
        printf("Input value is 10. \n");
    return 0;
}

If you let this program run and enter 10, the result will be as follows

Execution Result
10 Input data
The input value is 10.

If you run this program and enter anything other than 10, the results will be as follows

Execution Result
135 Input data

The display/hide correspondence is the opposite of the previous one, but the decision is made properly.


About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box.