Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ チャレンジ:ネストされたif文 | 条件文の導入
C++の条件文
セクション 1.  5
single

single

bookチャレンジ:ネストされたif文

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

ネストされた if 文は、単に別の if 文の内部にある if 文です。この構造により、複数の条件を順番に評価し、特定のコードブロックを実行することが可能になります。

nested_if.h

nested_if.h

copy
1234567891011121314
if (condition1) { // Code block 1 if (condition2) { // Code block 2 if (condition3) { // Code block 3 } } }

外側の if 文はゲートキーパーの役割を果たし、その評価に基づいて内部の別の if 文への扉を開く場合があります。たとえば、従業員のパフォーマンスに基づいて給与を決定するシナリオを考えてみましょう。

main.cpp

main.cpp

copy
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

main.cpp

copy
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

main.cpp

copy
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 文の強力な機能を活用し、効率的で保守性の高いプログラムを作成可能。

Note
注意

ネストされた if 文は、場合によっては必要だが、常に必要というわけではない。第3セクションでは、それらをいつ、なぜ、どのように回避するかについて解説。

タスク

スワイプしてコーディングを開始

あなたは映画チケットシステムを作成しています。チケット料金は、顧客の年齢週末か平日かによって異なります。

  1. ネストされたif文を使用してチケット料金を決定してください:
    • 顧客が18歳未満の場合:
      • 週末は12.0
      • 平日は8.0
    • 顧客が18歳以上60歳以下の場合:
      • 週末は20.0
      • 平日は15.0
    • 顧客が60歳超の場合:
      • 週末は16.0
      • 平日は10.0
  2. 関数からチケット料金を返却してください。

calculateTicketPrice(16, true)12.0
calculateTicketPrice(30, false)15.0
calculateTicketPrice(70, true)16.0

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

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

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

セクション 1.  5
single

single

AIに質問する

expand

AIに質問する

ChatGPT

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

some-alt