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.
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.
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
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
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.
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).
The result of executing this program will be as follows
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.
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.
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.
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.
If you let this program run and enter 10, the result will be as follows
If you run this program and enter anything other than 10, the results will be as follows
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.
The following program is an example of using the == operator to check whether the input value is 10 or not.
If you let this program run and enter 10, the result will be as follows
If you run this program and enter anything other than 10, the results will be as follows
The display/hide correspondence is the opposite of the previous one, but the decision is made properly.
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.
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.
= 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.
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.