Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ if-else演算子 | 制御文
/
C基礎

bookif-else演算子

メニューを表示するにはスワイプしてください

if-else文は、プログラミングにおける基本的な構文です。特定の条件に基づいてプログラムの流れを制御することができます。if-else構造は非常にシンプルです。

main.c

main.c

copy
123456
if (condition) { // Execute this block of code if condition is `true` } else { // Execute this block of code if condition is `false` }

例えば、temperature変数がセンサーから値を取得し、温度が設定した上限を超えた場合に警告を出すプログラムを作成したいとします。

main.c

main.c

copy
1234567891011121314
#include <stdio.h> int main() { int temperature = 200; // In celsius if (temperature > 80) { printf("Temperature is so high: %d degrees Celsius\n", temperature); } else { printf("Temperature is normal: %d degrees Celsius\n", temperature); } return 0; }

プログラム内で複数if文を組み込むことができ、特にさまざまな条件を評価する必要がある場合に有効です。if-else文は、else-ifを使ってさらに拡張できます。

main.c

main.c

copy
123456789101112131415161718192021
#include <stdio.h> int main() { int tempereture = 50; // In celsius if (tempereture > 50 && tempereture < 100) { printf("Temperature is high: %d degrees Celsius\n", tempereture); // First instruction } else if (tempereture > 100 && tempereture < 150) { printf("Temperature is so high: %d degrees Celsius\n", tempereture);// Second instruction } else if (tempereture > 150) { printf("Temperature is critically high: %d degrees Celsius\n", tempereture);// Third instruction } else { printf("Temperature is normal: %d degrees Celsius\n", tempereture); // Fourth instruction } return 0; }

1. if-else文の目的は何ですか?

2. C言語におけるif-else文の基本構造と、コードブロックの構文を説明してください。

question mark

if-else文の目的は何ですか?

正しい答えを選んでください

question mark

C言語におけるif-else文の基本構造と、コードブロックの構文を説明してください。

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  1

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 4.  1
some-alt