<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://peda.net/:static/532/atom.xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Luku 5: If-ehtolauseet</title>
<id>https://peda.net/id/439832c0778</id>
<updated>2022-01-17T14:16:13+02:00</updated>
<link href="https://peda.net/id/439832c0778:atom" rel="self" />
<link href="https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet#top" rel="alternate" />
<logo>https://peda.net/:static/532/peda.net.logo.bg.svg</logo>
<rights type="html">&lt;div class=&quot;license&quot;&gt;Tämän sivun lisenssi &lt;a rel=&quot;license&quot; href=&quot;https://peda.net/info&quot;&gt;Peda.net-yleislisenssi&lt;/a&gt;&lt;/div&gt;&#10;</rights>

<entry>
<title>If-ehtolauseet</title>
<id>https://peda.net/id/6f095114778</id>
<updated>2022-01-17T14:22:24+02:00</updated>
<link href="https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet#top" />
<content type="html">&lt;p&gt;&lt;strong class=&quot;editor red&quot;&gt;&lt;b&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;If&lt;/span&gt;&lt;/b&gt;&lt;/strong&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;&lt;strong class=&quot;editor red&quot;&gt; &lt;/strong&gt;–rakenteen avulla voidaan esittää ehtoja, joiden mukaan koodia toteutetaan. &lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;Tavallisimmat ehdot ovat seuraavat:&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;table aria-rowcount=&quot;7&quot;&gt;&lt;!--filtered attribute: data-tablestyle=&quot;MsoTableGrid&quot;--&gt;&lt;!--filtered attribute: data-tablelook=&quot;1696&quot;--&gt;&#10;&lt;tbody&gt;&#10;&lt;tr aria-rowindex=&quot;1&quot;&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;65536&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;ehto&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;65536&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;merkitys&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;tr aria-rowindex=&quot;2&quot;&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a == b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a ja b ovat yhtä suuret&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;tr aria-rowindex=&quot;3&quot;&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a != b &lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a ja b eivät ole yhtä suuret&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;tr aria-rowindex=&quot;4&quot;&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a &amp;lt; b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a on pienempi kuin b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;tr aria-rowindex=&quot;5&quot;&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a &amp;lt;= b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a on pienempi tai yhtä suuri kuin b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;tr aria-rowindex=&quot;6&quot;&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a &amp;gt; b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a on suurempi kuin b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;tr aria-rowindex=&quot;7&quot;&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a &amp;gt;= b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;td&gt;&lt;!--filtered attribute: data-celllook=&quot;0&quot;--&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;a on suurempi kuin yhtäsuuri kuin b&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;Voimme tehdä esimerkiksi seuraavanlaisen ohjelman: Tietokone kysyy käyttäjältä jotakin lukua, ja kertoo, onko luku suurempi vai pienempi kuin 0. &lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt; &lt;br/&gt;&#10;&lt;br/&gt;&#10;&lt;a href=&quot;https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet/luku-5.1.jpg#top&quot; title=&quot;Luku 5.1.JPG&quot;&gt;&lt;img src=&quot;https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet/luku-5.1.jpg:file/photo/e113b6320c86241a92cd03b92ac75729e32b6e39/Luku%205.1.JPG&quot; alt=&quot;&quot; title=&quot;Luku 5.1.JPG&quot; class=&quot;inline&quot; loading=&quot;lazy&quot;/&gt;&lt;/a&gt;&lt;br/&gt;&#10;&lt;br/&gt;&#10;&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;Ensimmäisellä rivillä pyydetään käyttäjää antamaan jokin luku (muista nyt &lt;/span&gt;&lt;strong class=&quot;editor red&quot;&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;int&lt;/span&gt;&lt;/strong&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;-komento, joka kertoo, että kysytty asia halutaan lukuna).&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;Kun käyttäjä on syöttänyt jonkin luvun, ohjelma lukee rivin 3 if-lauseen: Jos luku on suurempaa kuin 0, tulostetaan teksti “Luku on suurempi kuin 0”. Huomaa kaksi tärkeää asiaa: if-komennon perään lisätään kaksoispiste ja if-komennon määritelmä (rivi 4) kirjoitetaan sisennettynä.&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;Nyt ohjelma tulostaa tekstin “Luku on suurempi kuin 0”, jos annettu luku on nollaa suurempi. Entä jos annettu luku onkin pienempi kuin 0? Tällöin ohjelma ei tulosta mitään. Ehtoon voidaan lisätä &lt;/span&gt;&lt;strong class=&quot;editor red&quot;&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;else&lt;/span&gt;&lt;/strong&gt;&lt;span&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;-komento, joka kertoo, mitä tehdään niissä tapauksissa, joissa if-komennon ehto ei toteudu:&lt;/span&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt; &lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;span&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt;&lt;a href=&quot;https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet/luku-5.2.jpg#top&quot; title=&quot;Luku 5.2.JPG&quot;&gt;&lt;img src=&quot;https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet/luku-5.2.jpg:file/photo/bbfa0ee8ff1a2e0af722572589b308933b58fbd6/Luku%205.2.JPG&quot; alt=&quot;&quot; title=&quot;Luku 5.2.JPG&quot; class=&quot;inline&quot; loading=&quot;lazy&quot;/&gt;&lt;/a&gt;&lt;br/&gt;&#10;&lt;br/&gt;&#10;&lt;span class=&quot;TextRun SCXW248203030 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;&lt;span class=&quot;NormalTextRun SCXW248203030 BCX0&quot;&gt;Entä jos halutaan antaa oma ehtonsa vielä sille, että annettu luku on 0? If-rakenteessa voi olla vain yksi &lt;/span&gt;&lt;/span&gt;&lt;strong class=&quot;editor red&quot;&gt;&lt;span class=&quot;TextRun SCXW248203030 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;&lt;span class=&quot;SpellingError SCXW248203030 BCX0&quot;&gt;if&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;span class=&quot;TextRun SCXW248203030 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;&lt;span class=&quot;NormalTextRun SCXW248203030 BCX0&quot;&gt;-komento ja yksi &lt;/span&gt;&lt;/span&gt;&lt;strong class=&quot;editor red&quot;&gt;&lt;span class=&quot;TextRun SCXW248203030 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;&lt;span class=&quot;SpellingError SCXW248203030 BCX0&quot;&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;span class=&quot;TextRun SCXW248203030 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;&lt;span class=&quot;NormalTextRun SCXW248203030 BCX0&quot;&gt;-komento, mutta väliin voi lisätä useamman &lt;/span&gt;&lt;/span&gt;&lt;strong class=&quot;editor red&quot;&gt;&lt;span class=&quot;TextRun SCXW248203030 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;&lt;span class=&quot;SpellingError SCXW248203030 BCX0&quot;&gt;elif&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;span class=&quot;TextRun SCXW248203030 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;&lt;span class=&quot;NormalTextRun SCXW248203030 BCX0&quot;&gt;-komennon:&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;EOP SCXW248203030 BCX0&quot;&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt; &lt;br/&gt;&#10;&lt;br/&gt;&#10;&lt;a href=&quot;https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet/luku-5.3.jpg#top&quot; title=&quot;Luku 5.3.JPG&quot;&gt;&lt;img src=&quot;https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet/luku-5.3.jpg:file/photo/f6941103113db3aff58759518a8e0a631fcf6c78/Luku%205.3.JPG&quot; alt=&quot;&quot; title=&quot;Luku 5.3.JPG&quot; class=&quot;inline&quot; loading=&quot;lazy&quot;/&gt;&lt;/a&gt;&lt;br/&gt;&#10;&lt;br/&gt;&#10;&lt;span class=&quot;TextRun SCXW142642056 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;&lt;span class=&quot;NormalTextRun SCXW142642056 BCX0&quot;&gt;Ehtoja voi yhdistellä &lt;/span&gt;&lt;/span&gt;&lt;strong class=&quot;editor red&quot;&gt;&lt;span class=&quot;TextRun SCXW142642056 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;&lt;span class=&quot;NormalTextRun SCXW142642056 BCX0&quot;&gt;and&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;span class=&quot;TextRun SCXW142642056 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;&lt;span class=&quot;NormalTextRun SCXW142642056 BCX0&quot;&gt; ja &lt;/span&gt;&lt;/span&gt;&lt;strong class=&quot;editor red&quot;&gt;&lt;span class=&quot;TextRun SCXW142642056 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;none&quot;--&gt;&lt;span class=&quot;SpellingError SCXW142642056 BCX0&quot;&gt;or&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;span class=&quot;TextRun SCXW142642056 BCX0&quot; lang=&quot;FI-FI&quot;&gt;&lt;!--filtered attribute: data-contrast=&quot;auto&quot;--&gt;&lt;span class=&quot;NormalTextRun SCXW142642056 BCX0&quot;&gt; -komentojen avulla. Voidaan esimerkiksi kysyä käyttäjältä kahta eri lukua, ja kertoa, ovatko i) Molemmat luvut positiivisia, ii) toinen positiivinen ja toinen negatiivinen vai iii) molemmat negatiivisia:&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;EOP SCXW142642056 BCX0&quot;&gt;&lt;!--filtered attribute: data-ccp-props=&quot;{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}&quot;--&gt; &lt;br/&gt;&#10;&lt;br/&gt;&#10;&lt;a href=&quot;https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet/luku-5.4.jpg#top&quot; title=&quot;Luku 5.4.JPG&quot;&gt;&lt;img src=&quot;https://peda.net/p/annakaisa.markkanen/python-ohjelmointi/luku-5-if-ehtolauseet/if-ehtolauseet/luku-5.4.jpg:file/photo/0e45d35604e72138874e8c401aa5db54d8415b28/Luku%205.4.JPG&quot; alt=&quot;&quot; title=&quot;Luku 5.4.JPG&quot; class=&quot;inline&quot; loading=&quot;lazy&quot;/&gt;&lt;/a&gt;&lt;/span&gt;&lt;br/&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&#10;</content>
<published>2022-01-17T14:17:26+02:00</published>
</entry>


</feed>