single
チャレンジ:ネストされたif文
メニューを表示するにはスワイプしてください
ネストされた if 文は、単に別の if 文の内部にある if 文です。この構造により、複数の条件を順番に評価し、特定のコードブロックを実行することが可能になります。
nested_if.h
1234567891011121314if (condition1) { // Code block 1 if (condition2) { // Code block 2 if (condition3) { // Code block 3 } } }
外側の if 文はゲートキーパーの役割を果たし、その評価に基づいて内部の別の if 文への扉を開く場合があります。たとえば、従業員のパフォーマンスに基づいて給与を決定するシナリオを考えてみましょう。
main.cpp
12345678910111213141516171819202122232425#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 17; int hours_worked = 37; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // And if the number of hours worked is more than 40 if (hours_worked > 40) { // add an additional 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } } std::cout << current_salary << std::endl; }
このコードは、完了したタスク数と作業時間に基づいて従業員の新しい給与を計算します。タスクが15を超える場合は20%増加し、さらに作業時間が40時間を超える場合は追加で20%増加します。ご覧の通り、現在の計算結果は1200です。これはネストされたif文を使用することでのみ実現できます。ここでは、ネストされたif文を使わずに同じロジックを得ようとした例をいくつか紹介します。
main.cpp
12345678910111213141516171819202122232425#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 9; int hours_worked = 41; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } // If the number of hours worked is more than 40 if (hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } std::cout << current_salary << std::endl; }
最初は同じように動作するように見えるかもしれませんが、この場合、作業者は15件以上のタスクを完了したかどうかに関係なく、**追加で20%**の昇給を受け取ります。コードを実行して出力を確認すると、作業者が今回は15件以上のタスクを完了していなくても、1200という値が表示されます。
main.cpp
1234567891011121314151617181920212223#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 19; int hours_worked = 39; // If the number of completed tasks is greater than 15 // AND the number of of hours worked is more than 40 if (completed_tasks > 15 && hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; std::cout << current_salary << std::endl; } std::cout << current_salary << std::endl; }
この場合、意図した通りに動作するように思えるかもしれませんが、残念ながらこれも正しくありません。出力は1000です。これは、作業者が15件以上のタスクを完了しても、40時間以上働かなければ何も受け取れないためです。したがって、正しい実装を得るためには入れ子のif文を使用する必要があります。
ネストされた if 文は、意思決定プロセスや複雑なシナリオの処理に非常に役立つ構造。コード構造を慎重に設計し、可読性を維持することで、ネストされた if 文の強力な機能を活用し、効率的で保守性の高いプログラムを作成可能。
ネストされた if 文は、場合によっては必要だが、常に必要というわけではない。第3セクションでは、それらをいつ、なぜ、どのように回避するかについて解説。
スワイプしてコーディングを開始
あなたは映画チケットシステムを作成しています。チケット料金は、顧客の年齢と週末か平日かによって異なります。
- ネストされたif文を使用してチケット料金を決定してください:
- 顧客が18歳未満の場合:
- 週末は
12.0 - 平日は
8.0
- 週末は
- 顧客が18歳以上60歳以下の場合:
- 週末は
20.0 - 平日は
15.0
- 週末は
- 顧客が60歳超の場合:
- 週末は
16.0 - 平日は
10.0
- 週末は
- 顧客が18歳未満の場合:
- 関数からチケット料金を返却してください。
例
calculateTicketPrice(16, true) → 12.0
calculateTicketPrice(30, false) → 15.0
calculateTicketPrice(70, true) → 16.0
解答
フィードバックありがとうございます!
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください