【c++builder starter チュートリアルシリーズ】シーズン2 c++builderの部 第3回...
TRANSCRIPT
© 2016 Embarcadero Technologies, Inc.
All rights reserved. Proprietary and confidential.
第3回
‟条件とループ„
シーズン2:プログラミング言語をやさしく覚えよう
C++Builderの部
2© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
Delphi / C++Builder Starter チュートリアルシリーズ
シーズン2 :2017年1月23日~ 3月27日全 9 回
時間 :毎週月曜 17 時 00分~17時 50分
Delphi 17時00分~17時20分 / C++Builder 17時30分~17時50分
ねらい :プログラミング言語をやさしく覚えよう
シーズン2
第1回 2017年1月23日 シューティングゲームのプログラム
第2回 1月30日 変数と型
第3回 2月6日 条件とループ
第4回 2月13日 関数
第5回 2月20日 配列とレコード
第6回 2月27日 文字列とオブジェクト
第7回 3月6日 オブジェクト指向
第8回 3月13日 作ってみよう(仮)
第9回 3月27日 コミュニティと勉強会
セミナー情報 :下記のWebサイト
http://forms.embarcadero.com/starter-tutorial-webinar
3© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
第3回「条件とループ」
今日のねらい• 条件によって行う処理を変える方法を知る• 一定条件のもと繰り返し処理を行う方法を知る
実施内容• 一般的な文の基本• 条件文の使い方を学ぶ• ループ文の使い方を学ぶ• ループ文の制御を学ぶ
4© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
無料版 C++Builder 10.1 Berlin Starter Edition 入手方法
• シリアルキーを知らせるメール内にも再ダウンロードリンク有
エンバ
Web製品
C++
Builder
Starter
バナー登録 Get
無料で使える開発環境をダウンロード
EDN*に登録済の方はEDNアカウントでダウンロード可
登録完了後、自動でインストーラーのダウンロード開始
インストール時にシリアルキーを入力
登録時のメールアドレスにシリアルキーが配信される
5© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
void __fastcall TForm1::Button1Click(TObject *Sender){
int iA = 1;int iB = 0;
iB = iA + 2;ShowMessage( Format( "%d + 2 = %d", ARRAYOFCONST(( iA, iB )) ) );
}
一般的な文 (Statements)
左から右に読んで、上から下に進む
文はセミコロン ; により次の文へと分けられる
複数行をまとめる場合は {と }で囲む。
条件文やループ文により、分岐、繰り返し処理を行う
6© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
if 文 - 条件を満たす場合に追加の処理を行う
条件・判定により、実行する/しない処理を変えることができる
値が True/真の場合に限って、続く文を処理する
複数の処理を行う場合は { } で囲む
int count = 0;
if (Switch1->IsChecked == True) {ShowMessage( L"Switch1 が on です。" );
count = count + 1;}
if (Switch1->IsChecked == True) ShowMessage( L"Switch1 が on です。" ); if ( … )
if文が true / 真の時に実行される処理
true
false
7© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
if … else 文 – 条件を満たさない場合に別の処理を追加
if … else … とすれば、条件を満たす場合と満たさない場合にそ
れぞれ別の処を記述できる
if (Switch1->IsChecked == True) {// if文が true / 真の時に実行される処理ShowMessage( L”Switch1 が on です。” );
}else {
// if文が false / 偽の時に実行される処理ShowMessage( L"Switch1 が off です。" );
}
if ( … )
if文が true / 真の時に実行される処理
true false
if文が false / 偽の時に実行される処理
8© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
if 条件の比較、複合条件、複合文
条件の判定に演算子を使用 : == , != , < , <=, > , >= , !
複数の条件を合わせて判定する場合、論理演算子を使用: && (= AND) || ( = OR)
否定(反転)の場合も論理演算子: !
例 : if (! CheckBox1.isChecked) ... // Trueではなければ
条件判定後False/偽ののち、さらに別の条件判定を行うときは else ifがある
{
if ( RadioButton1.isChecked && CheckBox1.isChecked ) then // && で複数条件確認{ // { } で複数の文をまとめる
i = i + 1;
ShowMessage( L”チェックボックスON”);
}
else if ( i <= 10 ) // else ifでさらに条件判定ShowMessage(L”チェックボックスOFFでiが10以下”);
else
ShowMessage(L”チェックボックスOFFでiが10より大きい”);
}
演算子 意味
== 左辺と右辺が等しい
!= 等しくない
< 左辺値が小さい
> 左辺値が大きい
<= 左辺が右辺以下
>= 左辺が右辺以上
9© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
switch (値) { case 値1 : 処理1 ; case 値2: 処理2 ; }
単なる真偽ではなく、複数の値で処理を分岐さ
せたい場合の方法
値は整数型(または列挙型)。浮動小数点は使
えない。
値に対応するラベルを case 値1 : case 値2 : の
ように書く
ラベルは判定結果の飛び先の定義であり、ラベ
ル自体に特別な機能はない
break; を見つけたら、そこで処理を終えて
switch を抜ける
いずれの case ラベルにも該当しない場合には
default: ラベルに飛ぶ(default が無い場合は何
もしない)
switch
( … )
処理1case 値1
処理2case 値2
処理3case 値3
その他の値の処理default
break;
break;
break;
10© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
switch の記述例1:基本の書き方
switch (値) { case 値1 : 処理1 ; case 値2: 処理2 ; }
switch( a ) {case 1:処理1;
break;
case 2:処理2;
break;
case 3:処理3;
break;
default:デフォルトの処理;
}
switch
( … )
処理1case 値1
処理2case 値2
処理3case 値3
その他の値の処理default
break;
break;
break;
11© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
同じ処理を if 文で書くと…
switch (値) { case 値1 : 処理1 ; case 値2: 処理2 ; }
a == 1 ?if (a == 1) {処理1;
}else if ( a == 2 ) {処理2;
}else if ( a == 3 ) {処理3;
}else {デフォルトの処理;
}
a == 2 ?
a == 3 ?
処理1
処理2
処理3
デフォルトの処理
true
false
false
false
true
true
12© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
switch (値) { case 値1 : 処理1 ; case 値2: 処理2 ; }
switch の記述例2:break; を書かないと?
switch( a ) {case 1:処理1;
case 2:処理2;
break;
case 3:処理3;
break;
default:デフォルトの処理;
}
case 2: は飛び先のラベルであり、それ以上の機能はない。
よって、a = 1 の処理は case
1: から始まって case 2: のbreak; まで処理が行われる。
処理1case 値1
処理2case 値2
処理3case 値3
その他の値の処理default
break;
break;
switch
( … )
13© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
switch (値) { case 値1 : 処理1 ; case 値2: 処理2 ; }
switch
( … )
case 値1
case 値2
処理3case 値3
その他の値の処理default
break;
switch の記述例3:複数の case を連続して書くと?
switch( a ) {case 1:case 2://1と2の共通処理
break;
case 3://処理3
break;
default://デフォルトの処理
}
1と2の共通処理 break;
処理がないので先の行に進む。
14© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
ループ for (初期化文 ; 条件 ; 式 ) { 処理; … }
カウンタ(変数)の値が任意の初期値から一
定の値になるまでループ
カウンタの値の増減は式で設定する
for ( int counter = 0 ; counter < 10 ; counter ++ ) {//処理
ShowMessage(Format(“i = %d”, ARRAYOFCONST(( i )) )
);}
条件counter <
10
初期化文int counter =0
処理
式
false
true
15© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
ループ for ( 変数宣言 : 配列または列挙型 ) { 処理; … }
宣言された変数に対して配列や列挙型の全要
素を順番に代入してループする
データ全体を処理する場合や、不連続に変化
する値の集合へのループがシンプルに書ける
コンパイル設定でClangコンパイラを有効にし
た場合に利用可能
//配列型を宣言し、5つの初期値を設定する
int array[] = { 1, 3, 5 ,6, 9 };
for ( int i : array ) {//処理
ShowMessage(Format(“i = %d”, ARRAYOFCONST(( i )) )
);}
未処理の値がある?
配列または列挙型の値の集合を順次取り出す
処理
false
true
16© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
参考: Clangコンパイラを有効にする設定
Clangを使用する場合はプロジェクト毎に「従来のBorlandコンパイラを使用」を false にする。
17© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
ループ while ( 条件式 ) { 処理; … }
式が true の間はループを続ける
条件によってはwhile ループの中身を1度も実行せずに
終了する場合がある(開始時点で条件式が false の場
合)
int counter = 0;while ( counter < 10 ) {
/* このループは10回実行される */
counter++;}
int counter = 10;while ( counter < 10 ) {
/* ループが一度も実行されない例 *//* ループに入る時点で counter の値が 10 なので、*//* ループの継続条件である「10 未満」を満たさない */
counter++;}
条件式?
処理
false
true
18© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
ループ do { 処理; … } while ( 条件式 );
式が true の間はループを続ける
ループの中身は少なくとも1回は必ず実行される
int counter = 0;do {
/* このループは10回実行される */
counter++;}while ( counter < 10 );
int counter = 10;do {
/* ループが1回だけ実行される例 */
counter++;}
while ( counter < 10 )
条件式?
処理
false
true
19© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
continue, break による処理の分岐制御
for, while や switch を途中で抜けたり、
処理の先頭に戻るための制御を行う
• continue
for, while の条件式判断に戻る
• break
for, while, switch の処理を抜ける
int somefunction ( int number ) { while ( … ) {
switch ( ) {case xx:…continue;
case yy:for ( int i = 0 ; i < 10 ; i++ ) {…if ( 何かの条件 ) {
break;}…
}…break;
default:….
}
for () {….
}}
}
20© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
本日のセミナー内容は弊社ブログに掲載予定
[コミュニティ]↓
[日本人ブログ]
実施内容の再視聴
皆さんが見ているWebページの下に、順次アップロード
エンバカデロWebサイト : http://forms.embarcadero.com/starter-tutorial-webinar
[リソース] – [イベント]の「Delphi / C++Builder Starter チュートリアルシリーズ」ページ
実施内容サマリー
• Community embarcadero (コミュニティエンバカデロ)にWebリンク、サンプルコード情報等
http://community.embarcadero.com/
「エンバカデロ」で検索
21© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
第3回「条件とループ」まとめ
今日のねらい• 条件によって行う処理を変える方法を知る• 一定条件のもと繰り返し処理を行う方法を知る
実施内容• 一般的な文の基本• 条件文の使い方を学ぶ• ループ文の使い方を学ぶ• ループ文の制御を学ぶ
22© 2017 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
次回のC++パートは2月13日(月)17:30より
“関数„