Home> 2010-a > a-05 条件と分岐

このエントリーをはてなブックマークに追加

a-05 条件と分岐

  • 2010年5月17日 14:40
  • 2010-a

if〜else 条件分岐命令

if (条件式) {条件式がtrueの場合の実行内容}

07-1

int x = 1;
if (x > 0) background(255, 0, 0); //xが0よりも大きいならば、背景色を赤にする

条件式

== 左辺が右辺に等しい

「変数iが10に等しければtrue」

i == 10

※「i=10」は「iに10を代入」である。混同しやすいので注意。

!= 左辺が右辺に等しくない

「変数iが10に等しくなければtrue」

i! = 10

< 左辺が右辺より小さい

「変数iが10より小さければtrue」

i < 10

> 左辺が右辺より大きい

「変数iが10より大きければtrue」

i > 10

<= 左辺が右辺以下

「変数iが10以下ならばtrue」

i <= 10

>= 左辺が右辺以上

「変数iが10以上ならばtrue」

i >= 10

※<=, >=を逆に書く(=<, =>)とエラーになるので注意。

&& かつ

「変数iが0より大きく、かつ、変数iが10より小さければtrue」

i > 0 && i < 10

|| または

「変数iが0より小さい、または、変数iが10より大きいならばtrue」

i < 0 || i > 10

if (条件式) {条件式がtrueの場合の実行内容} else {条件式がfalseの場合の実行内容}

「x座標が3の倍数のときには赤い線を、3の倍数でないときにはオレンジの線を描く」

07-5

size(200, 200);
colorMode(HSB, 100);
background(99);

for(int i = 0; i < width; i++){
  //条件に従って色の設定をする
  if(i % 3 == 0){
    stroke(0, 99, 99); //赤
  }else{
    stroke(10, 99, 99); //オレンジ
  }
  //線を描く
  line(i, 0, i, height);
}

if (条件式A) {条件式Aがtrueの場合の実行内容} else if (条件式B) {条件式Bがtrueの場合の実行内容} else {条件式A,Bがfalseの場合の実行内容}

「x座標が3の倍数のときには赤い線を、3で割って1余る数のときにはオレンジの線を、それら以外の場合は黒い線を描く」

size(200, 200);
colorMode(HSB, 100);
background(99);

for(int i = 0; i < width; i++){
  //条件に従って色の設定をする
  if(i % 3 == 0){
    stroke(0, 99, 99); //赤
  }else (i % 3 == 1){
    stroke(10, 99, 99); //オレンジ
  }else{
    stroke(0); //黒
  }
  //線を描く
  line(i, 0, i, height);
}

switch 〜 case 〜 default パターン振り分け

07-3

size(200, 200);
colorMode(HSB, 100);
background(99);

for(int i = 0; i < width; i++){
  //条件に従って色の設定をする
  switch(i % 5){
    case 0:
      stroke(0, 99, 99); //赤
      break;
    case 1:
      stroke(10, 99, 99); //オレンジ
      break;
    case 2:
      stroke(20, 99, 99); //黄
      break;
    default: //3,4のとき
      stroke(30, 99, 99); //緑
      break;
  }
  //線を描く
  line(i, 0, i, height);
}

07-4

size(200, 200);
colorMode(HSB, 100);
background(99);
noStroke();

for(int i = 0; i< width / 5; i++){
  //条件に従って色の設定をする
  switch(i % 5){
    case 0:
      fill(0, 99, 99); //赤
      break;
    case 1:
      fill(10, 99, 99); //オレンジ
      break;
    case 2:
      fill(20, 99, 99); //黄
      break;
    default: //3,4のとき
      fill(30, 99, 99); //緑
      break;
  }
  //矩形を描く
  rect(i*5, 0, 5, height);
}

演習

「繰り返し、赤い線を描く(ウィンドウの幅いっぱいに赤い線を敷き詰める)」

size(200, 200);
colorMode(HSB, 100);
stroke(0, 99, 99);

for(int i = 0; i < width; i++){
  line(i, 0, i, height);
}

※このプログラムに条件分岐を加えていく → 「特定の条件を満たす場合にのみ、線を描く」

  • x座標が3のときだけ、赤い線を描く
  • x座標が3でないときだけ、赤い線を描く
  • x座標が3または33のときだけ、赤い線を描く
  • x座標が3の倍数のときだけ、赤い線を描く
  • x座標が3の倍数かつ7の倍数のときだけ、赤い線を描く

「x座標が3または33のときだけ、赤い線を描く」

07-6

size(200, 200);
colorMode(HSB, 100);
background(99);
stroke(0, 99, 99);

for(int i = 0; i < width; i++){
  if(i == 3 || i == 33){//iが3,33のときだけ以下を実行
    line(i, 0, i, height);
  }
}

「x座標が3の倍数のときだけ、赤い線を描く」

07-2

size(200, 200);
colorMode(HSB, 100);
background(99);
stroke(0, 99, 99);

for(int i = 0; i < width; i++){
  if(i % 3 == 0){//iが3の倍数のときだけ以下を実行
    line(i, 0, i, height);
  }
}

※ i%3は「iを3で割ったときの余り」を計算する。たとえば「5%3」は「2」であり、「9%3」は「0」となる。

まとめ

制御

条件

Index of all entries

Home> 2010-a > a-05 条件と分岐

カテゴリ
アーカイブ
購読
Recommends
Processing: A Programming Handbook for Visual Designers and Artists Learning Processing: A Beginner's Guide to Programming Images, Animation, and Interaction (Morgan Kaufmann Series in Computer Graphics and Geometric Modeling) FORM+CODE -デザイン/アート/建築における、かたちとコード Form+Code in Design, Art, and Architecture (Design Briefs) Generative Gestaltung Generative Art Built with Processing[Ver. 1.x対応版] -デザイン/アートのためのプログラミング入門 Getting Started With Processing Arduinoをはじめよう ビジュアライジング・データ ―Processingによる情報視覚化手法 +GAINER―PHYSICAL COMPUTING WITH GAINER
Powerd By

Return to page top