All the programs I've created so far have been one-way programs.
In other words, the statements are executed in order, from beginning to end.
It was the simplest mechanism: that when the sentence ended, it would be over.
"For truly simple programs, this might just work."
Whenever I try to make it do even a slightly complex task, it quickly gets stuck.
To solve this problem, we will vary the processing based on the conditions.
The Lunch Decision
おGoldに余裕がある ならば 焼肉定食 おGoldに余裕がない ならば 素うどん
It will require judgments based on conditions or nuances.
In the world of computers, conditions are nothing more than numeric values.
To be more specific, the comparison of two numbers can be the condition itself.
Conditional sentences
C language provides the if statement as a statement for making decisions based on conditions.
An if statement compares the values of two numbers and then performs different actions based on the result.
The usage of if statements is as follows:
How to Use If Statements
if (condition) statement;
Numerical comparisons using if statements are very clear and straightforward.
In essence, it simply determines whether the specified number is zero or not.
In C, when performing conditional judgments, we refer to 0 as a false value and anything other than 0 as a true value.
In an if statement, the following statement is executed only if the specified number is true (non-zero).
Otherwise, the line is skipped, and the statement after the if statement is executed.
The following program displays a number only when the number is true (i.e., not zero).
Source code
#include <stdio.h>
int main(void)
{
int suuti = 10;
if (suuti)
printf("%d\n", suuti);
return 0;
}
The results of this program's execution are as follows:
Execution results
10
In this program, we have int suuti=10;
This is how to declare and assign a value at the same time.
This way of writing is sometimes called initialization because it allows you to determine the value of a variable initially.
キーワード
【Initialization】
Declaring a variable and assigning a numerical value to it simultaneously.
"In this program, if the value of suuti is set to 0, nothing is displayed."
"Other values will display that value."
Comparison operators
You might think, after reading the previous section, that if statements are rather useless.
Since all it can do is determine whether it's zero or not.
However, combining standard calculations with if statements allows for more advanced comparisons.
For example, when you subtract a number from itself, the answer is naturally 0.
By utilizing this property, value determination can be achieved through subtraction.
The following program exploits 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("入力値は 10 ではありません。\n");
return 0;
}
"If you run this program and enter 10 as input, the result will be as follows:"
Execution results
10 入力したデータ
If you run this program and enter a number other than 10, the result will be as follows:
Execution results
135 入力したデータ 入力値は 10 ではありません。
If this method is applied in a more complex way, it would allow for highly sophisticated comparisons.
"However, it's inconvenient to have to subtract just to check if a variable's value is 10."
Therefore, C has operators dedicated solely for comparison.
When checking if two numbers are equal, use the == operator.
This operator performs a calculation that results in true when two values are equal.
The following program is an example of checking if an input value is 10 using the == operator.
Source code
#include <stdio.h>
int main(void)
{
int suuti;
scanf("%d", &suuti);
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.
"If you run this program and enter a number other than 10, the result will be as follows:"
Execution results
135 入力したデータ
"Although the show/hide functionality is reversed from before, the assessment is still being performed correctly."
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.