KAIHATSUGIKEN GROUP
********************************************************************************
7 プログラム中での条件判断
********************************************************************************
if (条件が真であるとき)
結果が真であればif分の後に続く文を実行する
*********
#include <iostream.h>
void main(void)
{
int test_score = 95; //int型変数test_scoreを準備
if (test_score >= 90)// test_scoreが90以上であるとき下の単文を実行
cout << "Congratulations, you got an A!" << endl;
}
****************************************
#include <iostream.h>
void main(void)
{
int test_score = 95; //int型変数test_scoreを準備
if (test_score >= 90)
{ //実行される単文が二つ以上ある時は"{}"で囲む
cout << "Congratulations, you got an A!" << endl;
cout << "Your test score was " << test_score << endl;
}
}
********
Congratulations, you got an A!
Your test score was 95
********************************************************************************
else (条件が偽の場合)
条件が偽であるときに実行する文はelseで指定します。
********
#include <iostream.h>
void main(void)
{
int test_score = 95; //int型変数test_scoreを準備して
//95をtest_scoreに代入
if (test_score >= 90) //90以上であれば 下の単文を実行
cout << "Congratulations, you got an A!" << endl;
else //90以上でなければ 下の単文を実行
cout << "You need to work harder next time!" << endl;
}
************************************
#include <iostream.h>
void main(void)
{
int test_score = 65; //int型変数test_scoreを準備して
//65を代入
if (test_score >= 90) //90以上であれば下の文を実行
{ //単文が二つ以上ある場合は"{}"で囲む
cout << "Congratulations, you got an A!" << endl;
cout << " Your test score was " << test_score << endl;
}
else //90以上でなければ下の単文を実行
{
cout << "You should have worked harder!" << endl;
cout << "You missed " << 100 - test_score <<
" poinnts " << endl;
}
}
********************************************************************************
入力の値に対応するプログラム
********
#include <iostream.h>
void main(void)
{
int test_score; //int型変数test_scoreを準備
cout << "Type in the test score and press Enter; "; //画面に表示される
cin >> test_score; //入力文字列をtest_scoreに格納
if (test_score >= 90) //入力文字列が90以上であれば下の単文を実行
{
cout << "Congratulations, you got an A!" << endl;
cout << "Your test score was " << test_score << endl;
}
else //入力文字列が90以上でなければ下の単文を実行
{
cout <<"You should have worked harder!" << endl;
cout << "You missed " << 100 - test_score <<
"points " << endl;
}
}
********************************************************************************
&& 演算子 〜であってかつ〜である
**********
if ((user_owns_a_dog) && (dog == dalmatian))
犬を飼っていてかつ犬はダルメシアンである。
********************************************************************************
|| 演算子 〜もしくは〜である
**********
if ((user_owns_a_dog) || (user_owns_a_cat))
犬もしくは猫を飼っている
C++では 1は真を表し 0は偽を表す
**********
#include <iostream.h>
void main(void)
{
int user_owns_a_dog = 1; //int型変数user_owns_a_dogを準備して
//1を代入
int user_owns_a_cat = 0; //int型変数user_owns_a_catを準備して
//0を代入
if (user_owns_a_dog) //犬のオーナー
cout << "Dogs are great" << endl;
if (user_owns_a_cat) //猫のオーナー
cout << "Cats are great" << endl;
if ((user_owns_a_dog) && (user_owns_a_cat)) //犬と猫 両方のオーナー
cout << "Dogs and cats can get along" << endl;
if ((user_owns_a_dog) || (user_owns_a_cat))//犬か猫のどちらかのオーナー
cout << "Pets are great!" << endl;
}
********************************************************************************
! 演算子 〜でない
if (! user_owns_a_dog)
cout << "You shouls buy a dog" << endl;
犬を飼っていない
********************************************************************************
#include <iostram.h>
void main(void)
{
int user_owns_a_dog = 1; //犬のオーナーである
int user_owns_a_cat = 0; //猫のオーナーでない
if (! user_owns_a_dog) //犬のオーナーでない
cout << "Dogs are grat" << endl;
if (! user_owns_a_cat) //猫のオーナーでない
cout << "Cats are great" << endl;
}
********************************************************************************
switch文について
switch文は条件を記述して1つ以上のcaseごとに条件と合致するかどうかを判定する
default文はそれまでのcase分から漏れたものを受け止めるあらゆる条件に合致する
case文です。
break文はswitch文で条件に合致するcaseが見つかるとその後ろにあるcaseも全て合致した
ものと見なしてしまいます。breakはswitch文が終了したこととswitch分の次にある文に
処理を移すことを表します。
*********
#include <iostream.h>
void main(void)
{
char grade = 'B'; //選択するのは Bである
switch (grade) //条件の記述のあっているものが実行される
{
case 'A' : cout << "Congratulations on your A" << endl;
break;
case 'B' : cout << "Not bad, a B is ok" << endl;
break; //実行されるとswitch文からでる
case 'C' : cout << "C's are only average" << endl;l
break;
case 'D' : cout << "D's are terrible" << endl;
break;
default: cout << "No excuses! Study harder!" << endl;
break; //defaultはそれまでのcase分から漏れたものを
//受け止めるためのもの。あらゆる条件に合致する
}
}
********************************************************************************