è«çåŒ
ããã°ã©ã ãå®è¡ããå®éã®äœæ¥ã¯åŒã«ãããŸãã æå³ãå¯äœçšãåŒãèµ·ããããã°ã©ã ã®éšåã¯ãã¹ãŠåŒãšåŒã°ããŸãã å®æ°ã®å€ïŒ42ãªã©ïŒãæååå€ïŒãhelloããªã©ïŒãåŒã§ããããããããã¯å¯Ÿå¿ããå®æ°42ããã³ãhelloããäœæãããããåºç¯ãªæŠå¿µã§å®çŸ©ãããŸãã
éèŠïŒå€ã®äœæãšå€æ°ã®å®£èšãæ··åããªãã§ãã ããã å€ãå€æ°ã«ãã€ã³ãããå¿ èŠã¯ãããŸããã
ãšãã㧠ã writelnãªã©ã®é¢æ°åŒã³åºãã¯ãå¯äœçšãããããåŒã§ãã writelnã®å Žåããã®å¹æã¯ãæåãå ¥åããããšã«ããæšæºåºåã¹ããªãŒã ã«éããããŸãã åã«æžããããã°ã©ã ã®å¥ã®äŸã¯ãå·ŠåŽã®å€æ°ãå€æŽããå²ãåœãŠæäœã§ãã
å€ã®äœæã«ãããåŒã¯ä»ã®åŒã«åå ã§ããŸãã ããã«ãããããåçŽãªåŒããããè€éãªåŒãäœæã§ããŸãã ããšãã°ãçŸåšã®æ°æž©ã®å€ãè¿ãcurrentTemperatureãšããé¢æ°ããããšããŸãããã®é¢æ°ãçæãããã®å€ã¯ã writelnåŒã§çŽæ¥äœ¿çšã§ããŸãã
writeln("It's ", currentTemperature()," degrees at the moment.");
ãã®è¡ã«ã¯æ¬¡ãå«ãŸããŸãã
- ãããã
- currentTemperatureïŒïŒ
- ãçŸåšã®åŠäœã
- writelnïŒïŒã¯ ãåã®3ã€ã䜿çšããåŒã§ãã
ãã®ç« ã§ã¯ãæ¡ä»¶æã§äœ¿çšãããç¹å®ã®ã¿ã€ãã®åŒã«ã€ããŠèª¬æããŸãã
å ã«é²ãåã«ãä»£å ¥æŒç®åãããäžåºŠç¹°ãè¿ããŠãå·Šå³ã«ããããã2ã€ã®åŒã匷調ããããšæããŸãã ä»£å ¥æŒç®åïŒ=ïŒã¯ãæå®ãããå³åŽã®åŒã®å€ãå·ŠåŽã®åŒïŒå€æ°ãªã©ïŒã«å²ãåœãŠãŸãã
temperature = 23 // temperature's value becomes 23
è«çåŒ
è«çåŒã¯ãããŒã«ä»£æ°ã§äœ¿çšãããåŒã§ãã è«çåŒã¯ããçããã¯ãã®å Žåããã¡ã€ã«ãä¿åããŸãããªã©ã®æ±ºå®ãè¡ããšãã«ã³ã³ãã¥ãŒã¿ãŒããã°ã©ã ãè¡ãããšã§ãã
è«çåŒã«ã¯ã falseãæå³ããfalseãštrueãæå³ããtrueã®2ã€ã®å€ã®ãããããæå®ã§ããŸãã
次ã®äŸã§ã¯writelnåŒã䜿çšããŸãã è¡ã®æåŸã«trueãå°å·ãããå Žåããã®è¡ã«å°å·ãããŠãããã®ãtrueã§ããããšãæå³ããŸãã åæ§ã«ã falseã¯ãå°å·ããããã®ãfalseã§ããããšãæå³ããŸãã ããšãã°ããã®åºåã次ã®ãããªå ŽåïŒ
ã³ãŒããŒããããŸãïŒtrueããã¯ãã³ãŒããŒããããŸãããšããæå³ã§ãã åæ§ã«ïŒ
ã³ãŒããŒããããŸãïŒfalseãã³ãŒããŒããªãããšããæå³ã§ãã å®éã«ã¯ãå·ŠåŽã®ããããã¯ã³ãŒããŒãããããšãæå³ããªãããšã«æ³šæããŠãã ããã ã... is ...ïŒfalseããšããæ§æã䜿çšããŠããããã¯æ£ãããªãããŸãã¯ãããã¯ééã£ãŠããããšããæå³ã§ãã è«çåŒã¯ãè¿°èªãã«ãŒããé¢æ°ã®åŒæ°ãªã©ã§åºã䜿çšãããŠããŸããããããã©ã®ããã«æ©èœããããç解ããããšã¯éèŠã§ãã 幞ããªããšã«ãè«çåŒã¯éåžžã«èª¬æãããã䜿ããããã§ãã
以äžã¯ãè«çåŒã§äœ¿çšãããè«çæŒç®åã§ãã
- çå·æŒç®åïŒ==ïŒã¯ããããã¯ããã«çããã§ããïŒããšãã質åã«çããŸãã å·ŠåŽã®2ã€ã®åŒãå³åŽã«æ¯èŒããçããå Žåã¯trueãè¿ããçãããªãå Žåã¯falseãè¿ããŸã ã å®éãçå€æŒç®åïŒ==ïŒã«ãã£ãŠè¿ãããå€ã¯è«çåŒã§ã
äŸãšããŠã次ã®2ã€ã®å€æ°ããããšããŸããint daysInWeek = 7; int monthsInYear = 12;
daysInWeek == 7 // true monthsInYear == 11 // false
- äžçåŒæŒç®åïŒïŒ=ïŒãããã¯ãããšçãããªãããšãã質åã«çããŸãã äž¡åŽã®ããã2ã€ã®åŒãæ¯èŒããæŒç®åïŒ==ïŒã«ãã£ãŠè¿ãããçµæãšå察ã®çµæãè¿ããŸãã
daysInWeek != 7 // false monthsInYear != 11 // true
- éžèšæŒç®åïŒ||ïŒã¯ããŸãã¯ããè¡šããè«çåŒã®ãããããçã®å Žåã«çãè¿ããŸãã
å·ŠåŽã®åŒã®å€ãtrueã®å ŽåãæŒç®åã¯trueãè¿ããå³åŽã®åŒãèŠãããšããããŸããã å·ŠåŽã®åŒãfalseãè¿ãå ŽåãæŒç®åã¯å³åŽã®åŒã®å€ãè¿ããŸãã ãã®æŒç®åã¯ããã·ã¢èªã®çµåãorãã«äŒŒãŠããŸãã
å·ŠåŽã®åŒ ãªãã¬ãŒã¿ãŒ å³åŽã®è¡šçŸ çµæ åœ || åœ åœ åœ || æ¬åœ æ¬åœ æ¬åœ || falseïŒåé¡ã§ã¯ãªãïŒ æ¬åœ æ¬åœ || trueïŒåé¡ã§ã¯ãªãïŒ æ¬åœ import std.stdio; void main() { // false "", true "" bool existsCoffee = false; bool existsTea = true; writeln("There is warm drink: ", existsCoffee || existsTea); }
- æ¥ç¶æŒç®åïŒ&&ïŒã¯ãandããè¡šããå·Šãšå³ã®äž¡æ¹ã®åŒãtrueãè¿ãå Žåã«trueãè¿ããŸã ã
å·ŠåŽã®åŒã®å€ãfalseã®å ŽåãæŒç®åã¯falseãè¿ããå³åŽã®åŒãèŠãããšããããŸããã å·ŠåŽã®åŒãtrueãè¿ãå Žå ãæŒç®åã¯å³åŽã®åŒã®å€ãè¿ããŸãã ãã®æŒç®åã¯ããã·ã¢èªã®ãandããšäŒŒãŠããŸããå·ŠåŽã®åŒ ãªãã¬ãŒã¿ãŒ å³åŽã®è¡šçŸ çµæ åœ && falseïŒåé¡ã§ã¯ãªãïŒ åœ åœ && trueïŒåé¡ã§ã¯ãªãïŒ åœ æ¬åœ && åœ åœ æ¬åœ && æ¬åœ æ¬åœ - å³å¯ãªéžèšæŒç®åïŒ^ïŒã¯ããäž¡æ¹ã§ã¯ãªããã©ã¡ããïŒããšãã質åã«çããŸãã 1ã€ã®åŒã®ã¿ãtrueãè¿ãããäž¡æ¹ã¯è¿ããªãå Žåããã®æŒç®åã¯trueãè¿ããŸã ã
ããšãã°ãã2人ã®å人ã®ãã¡1人ã ããæ¥ãå Žåããã§ã¹ãããŸãããšããããžãã¯ã§ãã ããã¯æ¬¡ã®ããã«ããã°ã©ã ã§ããŸããå·ŠåŽã®åŒ ãªãã¬ãŒã¿ãŒ å³åŽã®è¡šçŸ çµæ åœ ^ åœ åœ åœ ^ æ¬åœ æ¬åœ æ¬åœ ^ åœ æ¬åœ æ¬åœ ^ æ¬åœ åœ writeln("I will play chess: ", jimShowedUp ^ bobShowedUp);
- æŒç®åã¯å³å¯ã«ãããå°ãããïŒ<ïŒã¯ããããããå°ããã§ããïŒããšãã質åã«åçããŸãïŒãŸãã¯ããœãŒããããšãã«é«ããªããŸããïŒãïŒ
writeln("We beat: ", theirScore < ourScore);
- æŒç®åã¯å³å¯ã«ããã以äžã§ããïŒãïŒãŸãã¯ããœãŒãæã«äœããªããŸããïŒãïŒãšãã質åã«çããŸãã
writeln("They beat: ", theirScore > ourScore);
- æŒç®åã¯ãããã以äžã§ããïŒãïŒãŸãã¯ããœãŒããããšããããã¯ããé«ãã§ããããããšãåãäœçœ®ã«ãªããŸããïŒãïŒãšãã質åã«å€§ãŸãã«å°ãªãïŒ<=ïŒçããŸãã
writeln("We were not beaten: ", theirScore <= ourScore);
- ã©ã€ã¿ãŒã§ã¯ãªãæŒç®ålessïŒ> =ïŒã¯ãããã以äžã§ããïŒãïŒãŸãã¯ããœãŒãæãŸãã¯åãäœçœ®ã«ãããšãã«äœããªããŸããïŒããšãã質åã«çããŸããïŒ
writeln("We did not beat: ", theirScore >= ourScore);
- å転æŒç®åïŒïŒïŒã¯ããããã¯äœãã®å察ã§ãããšããæå³ã§ãã åã®æŒç®åãšã¯ç°ãªãã1ã€ã®åŒã®ã¿ã§æ©èœããåŒãfalseãè¿ãå Žåã¯trueãè¿ããåŒãtrueãè¿ãå Žåã¯falseãè¿ããŸã ã
writeln("I will walk: ", !existsBicycle);
åŒã®ã°ã«ãŒãå
ãããã®åŒãè©äŸ¡ãããé åºã¯ãæ¬åŒ§ã䜿çšããŠç€ºãããšãã§ããŸãã ã°ã«ãŒãåããããšãã§ããŸãã æ¬åŒ§å ã®åŒãããè€éãªåŒã«åé¡ãããå Žåããããã®åŒã®å€ã¯ãããããå«ãŸããåŒã§äœ¿çšãããåã«èšç®ãããŸãã ããšãã°ããã³ãŒããŒããè¶ããããŠã¯ãããŒããã³ãããã°ãç§ã¯å¹žãã§ãããšããè¡šçŸã¯ããããã次ã®ããã«ããã°ã©ã ã§ããŸãã
writeln("I am happy: ", (existsCoffee || existsTea) && (existsCookie || existsScone));
ãããã®ãã¹ããããåŒãæ¬åŒ§ã§å²ãŸããŠããªãå ŽåãDã®ã«ãŒã«ïŒCèšèªããç¶æ¿ãããïŒã«åŸã£ãŠæŒç®åã®åªå
é äœãèæ
®ããŠå®è¡ãããŸãã è«çç©æŒç®å&&ã¯éžèšæŒç®å||ãããåªå
床ãé«ããã ãæ¬åŒ§ãªãã®åŒã®èšè¿°ã¯æåŸ
ã©ããã«èšç®ãããŸããã writeln("I am happy: ", existsCoffee || existsTea && existsCookie || existsScone);
çµåæŒç®å&&ãæåã«å®è¡ãããåŒå
šäœã次ã®åŒãšæå³çã«åçã«ãªããŸãã writeln("I am happy: ", existsCoffee || (existsTea && existsCookie) || existsScone);
ããã«ã¯æ ¹æ¬çã«ç°ãªãæå³ããããŸãããã¯ãããŒããã³ãå
¥ã£ãã³ãŒããŒããè¶ãããã°ãç§ã¯å¹žãã§ããã
ããŒã«å ¥åã®èªã¿åã
äžèšã®ãã¹ãŠã®ããŒã«å€ã¯ãfalseãŸãã¯trueãšããŠåºåãããŸãã ããããããã¯éæ¹åã«ã¯æ©èœããŸããããããã®ãfalseãããã³ãtrueãè¡ã¯ãèªåçã«falseããã³trueãšããŠèªã¿åãããŸããã ãã®ããããã®å ¥åã¯æååãšããŠèªã¿åããã次ã«boolåã®å€ã«å€æãããå¿ èŠããããŸãã
以äžã®æŒç¿ã®1ã€ã§ãfalseããšãtrueããå ¥åããå¿ èŠããããããDã®æ©èœã䜿çšããå¿ èŠããããŸãããããã«ã€ããŠã¯ãŸã 説æããŠããŸããã 以äžã§ã¯ãæå®ãããæååå ¥åãããŒã«åããŒã¿ã«å€æããã¡ãœããã«ã€ããŠèª¬æããŸããã ãã®ã¡ãœããã¯ã std.convã¢ãžã¥ãŒã«ã§å®£èšãããŠããtoãå®è¡ããããšã§ãã®åé¡ã解決ããŸãã ïŒãfalseããŸãã¯ãtrueã以å€ãå ¥åãããšã ConvExceptionãšã©ãŒã衚瀺ãããå ŽåããããŸããïŒ
次ã®ããã°ã©ã ã®mainïŒïŒã«ããã³ãŒãã®ãã¹ãŠã®éšåããã®æ®µéã§æ確ã§ããããšãé¡ã£ãŠããŸãã read_boolïŒïŒã¯ãèšèªã®æ°ããå¯èœæ§ãããã¡ãœããã§ãã 圌ãäœãããŠããã®ãã説æããã³ã¡ã³ããæ¿å ¥ããŸãããããã®æ¹æ³ã¯ç¡èŠã§ããŸãã ããã§ããã³ã³ãã€ã«ãšæ£ããæäœã®ããã«ãããã°ã©ã ã³ãŒãã«å«ãŸããŠããå¿ èŠããããŸãã
æŒç¿
- äžèšã®ããã«ããããã®æŒç®å<ãš>ã䜿çšããŠãå€ãä»ã®å€ããã倧ããå Žåãšå°ããå Žåã決å®ããŸããããéã«ããããšãã質åã«çããæŒç®åã¯ãããŸããã§ããã
ããã°ã©ããŒã次ã®ã³ãŒããæžããŠã å€ã 10ãã20ã®éã§ãããã©ãããå€å¥ãããšä»®å®ããŸããããããã°ã©ã ã¯èª¬æããããã«ã³ã³ãã€ã«ãããªãããšã«æ³šæããŠãã ãããimport std.stdio; void main() { int value = 15; writeln("Is between: ", 10 < value < 20); // â }
writeln("Is between: ", (10 < value < 20)); // â
- ãã®åé¡ã®è§£æ±ºçãæ¢ããŠãããšããåãããã°ã©ããŒã¯ã次ã®æ¬åŒ§ã䜿çšãããšã³ãŒããã³ã³ãã€ã«ã§ããããšãçºèŠããŸããã
writeln("Is between: ", (10 < value) < 20); // â ,
int value = 21;
- ãããã¯éã«ããã®ãïŒããšãã質åã«çããè«çåŒä»£ããã«ããããã¯æå°å€ããã倧ãããæ倧å€ãããå°ãããïŒããšãã質åã«çããã¹ãã§ãã
ãã®ããžãã¯ã念é ã«çœ®ããŠãã®ããã°ã©ã ã®åŒãå€æŽãããšãæåŸ ã©ããã«ãtrueãã衚瀺ãããããšã«æ³šæããŠãã ããã ãã®è«çåŒãä»ã®å€ã§ãæ£ããæ©èœããããšããã¹ãããããšãã§ããŸãã ããšãã°ã å€ã50ãŸãã¯1ã®å Žåããã®ããã°ã©ã ã¯ãfalseããåºåãã å€ã12ã®å Žåããã®ããã°ã©ã ã¯ãtrueããåºåããŸãã - 次ã®æ¡ä»¶ã®ããããã«è©²åœããå ŽåãããŒãã«è¡ãããšãã§ãããšä»®å®ããŸãã
- ããŒããŸã§ã®è·é¢ã10 kmæªæºã§ã誰ããèªè»¢è»ãå©çšã§ããå Žåã
- 6æ³æªæºã§ãè»ãããããã®ãã¡ã®1人ãé転å 蚱蚌ãæã£ãŠããå Žåã
import std.stdio; import std.conv; import std.string; void main() { write("How many are we? "); int personCount; readf(" %s", &personCount); write("How many bicycles are there? "); int bicycleCount; readf(" %s", &bicycleCount); write("What is the distance to the beach? "); int distance; readf(" %s", &distance); bool existsCar = read_bool("Is there a car? "); bool existsLicense = read_bool("Is there a driver license? "); /* 'true' , 'true', : */ writeln("We are going to the beach: ", true); } /* , , . */ bool read_bool(string message) { // write(message, "(false or true) "); // . string input; while (input.length == 0) { input = chomp(readln()); } // bool result = to!bool(input); // return result; }
...ãœãªã¥ãŒã·ã§ã³
- ã³ã³ãã€ã©ãŒã¯æ¢ã«10 <valueãåŒãšããŠåãåã£ãŠããããã writelnã®åŒæ°ãšããŠåãå ¥ããããã«ããã®åŸã«ã³ã³ããåŸ ã¡ãŸãã åŒå šäœã§è§æ¬åŒ§ã䜿çšãããšãåãåŒã§éãè§æ¬åŒ§ãæåŸ ããããããæ©èœããŸããã
- ãã®åŒãïŒ10 <å€ïŒ<20ãšããŠã°ã«ãŒãåãããšãã³ã³ãã€ã«ãšã©ãŒãåé€ãããŸãããã®å Žåãæåã®éšåãèšç®ããããã®çµæã<20ãšæ¯èŒãããããã§ãã
10 <valueãªã©ã®è«çåŒã®å€ã¯falseãŸãã¯trueã«ãªãããšãããã£ãŠããŸã ã falseããã³trueã¯ãæŽæ°åŒã§ããããå€0ããã³1ãåããŸãã 次ã®ç« ã§èªååå€æãæ€èšããŸãã ãã®çµæããã®åŒã¯ãã¹ãŠ0 <20ãŸãã¯1 <20ã®ãããããšåçã«ãªããã©ã¡ããtrueãè¿ããŸã ã - ãæå°å€ãã倧ãããæ倧å€ããå°ããããšããè¡šçŸã¯ã次ã®ããã«ããã°ã©ã ã§ããŸãã
writeln("Is between: ", (value > 10) && (value < 20));
- ããã¹ãŠã®äººã«èªè»¢è»ã¯ãããŸãããã¯personCount <= bicycleCountçšåºŠãŸãã¯bicycleCount> = personCountãšããŠããã°ã©ã ã§ããŸãã ãã®è«çåŒã®æ®ãã®éšåã¯ãæŒç¿ããããã«ããã°ã©ã ã³ãŒãã«å€æã§ããŸãã
writeln("We are going to the beach: ", ((distance < 10) && (bicycleCount >= personCount)) || ((personCount <= 5) && existsCar && existsLicense) );