条件分岐
#if
score = int(input(“Score? “))
比較演算子
if score > 80:
print(“OK!”)
print(“Good Job”)
else:
print(“NG!”)
print(“Nice try!”)
print(“End of program …”)
score = int(input(“Score? “))
if score > 80:
print(“OK!”)
print(“Good Job”)
else:
print(“NG!”)
print(“Nice try!”)
print(“End of program …”)
// for.cpp : このファイルには ‘main’ 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
using namespace std;
int main()
{
for (int i = 0; i < 5; ++i) cout << “Hello, World\n”;
}
using namespace std;
int main() {
int n = 5;
switch (n % 3) {
case 0:
cout << “3の倍数です。\n”;
break;
case 1:
cout << “3で割った余りは1です\n”;
break;
case 3:
cout << “何かがおかしいです。\n”;
break;
}
}
using namespace std;
int main(){
int n = 5;
if(n % 2 == 1) cout << “nは奇数です。\n”;
}
using namespace std;
int main(){
using cplx = complex;
shared_ptr<cplx> p = make_shared<cplx>();
cout << *p << endl;
shared_ptr<cplx> q = make_shared<cplx>(3., 4.);
cout << *q << endl;
shared_ptr<cplx> r = make_shared<cplx>(*q);
cout << *r << endl;
}
score = int(input(“Score? “))
if score > 80:
print(“OK!”)
print(“Good Job”)
print(“End of program …”)
class Pokemon {
public:
// コンストラクタ
Pokemon(const std::string& nickname) {
this->nickname_ = nickname;
// nickname_ = nickname; と省略できる
}
// デストラクタ
virtual ~Pokemon() {
}
virtual void attack() = 0;
protected:
std::string nickname_;
};
class Pikachu : public Pokemon {
public:
Pikachu(const std::string& nickname) : Pokemon(nickname) {
}
virtual ~Pikachu() {
}
void attack() override {
std::cout << nickname_ << “の十万ボルト!” << std::endl;
}
};
class Zenigame : public Pokemon {
public:
Zenigame(const std::string& nickname) : Pokemon(nickname) {
}
virtual ~Zenigame() {
}
void attack() override {
std::cout << nickname_ << “のハイドロポンプ!” << std::endl;
}
};
int main() {
int i;
Pokemon* monsters[2];
monsters[0] = new Pikachu(“サトシのピカチュウ”);
monsters[1] = new Zenigame(“サトシのゼニガメ”);
std::cin >> i;
if (i >= 0 && i < 2) { monsters[i]->attack();
}
delete monsters[0];
delete monsters[1];
}
initial_balance = int(input(“Initial Balance? “))
RATE = 1.1
print(f”Year 0: {initial_balance:,.2f}”)
print(f”Year 1: {initial_balance * RATE:,.2f}”)
print(f”Year 2: {initial_balance * RATE * RATE:,.2f}”)
class TodoList:
def init(self):
self.tasks = []
def add_task(self, task):
self.tasks.append(task)
def remove_task(self, task):
if task in self.tasks:
self.tasks.remove(task)
else:
print("タスクが見つかりません。")
def list_tasks(self):
if not self.tasks:
print("ToDoリストは空です。")
else:
print("ToDoリスト:")
for idx, task in enumerate(self.tasks, start=1):
print(f"{idx}. {task}")
def main():
todo_list = TodoList()
while True:
print(“\n操作を選択してください:”)
print(“1. タスクを追加”)
print(“2. タスクを削除”)
print(“3. タスク一覧を表示”)
print(“4. 終了”)
choice = input(“選択: “)
if choice == '1':
task = input("タスクを入力してください: ")
todo_list.add_task(task)
print("タスクが追加されました。")
elif choice == '2':
task_idx = int(input("削除するタスクの番号を入力してください: "))
if 1 <= task_idx <= len(todo_list.tasks):
task_to_remove = todo_list.tasks[task_idx - 1]
todo_list.remove_task(task_to_remove)
print("タスクが削除されました。")
else:
print("無効な番号です。")
elif choice == '3':
todo_list.list_tasks()
elif choice == '4':
print("プログラムを終了します。")
break
else:
print("無効な選択です。")
if name == “main“:
main()
int main()
{
std::cout << “Hello, World!\n”;
}