{"id":6667,"date":"2026-04-13T11:50:46","date_gmt":"2026-04-13T11:50:46","guid":{"rendered":"http:\/\/the-codest.localhost\/dictionary\/ternary-operator\/"},"modified":"2026-04-13T11:50:50","modified_gmt":"2026-04-13T11:50:50","slug":"ternaarne-operaator","status":"publish","type":"dictionary","link":"https:\/\/thecodest.co\/et\/dictionary\/ternary-operator\/","title":{"rendered":"Ternaarne operaator"},"content":{"rendered":"<h1>Ternaarne operaator<\/h1>\n<p>Ternaarne operaator, mida tuntakse ka tingimusoperaatorina, on l\u00fchendatud viis if-else avalduse kirjutamiseks \u00fches reas <a href=\"https:\/\/thecodest.co\/et\/dictionary\/what-is-code-refactoring\/\">kood<\/a>. See v\u00f5tab kolm operandi: tingimus, v\u00e4\u00e4rtus, mis tagastatakse, kui tingimus on t\u00f5ene, ja v\u00e4\u00e4rtus, mis tagastatakse, kui tingimus on vale. Ternaarse operaatori s\u00fcntaks on j\u00e4rgmine:<\/p>\n<p><code>tingimus ? value_if_true : value_if_false<\/code><\/p>\n<p>K\u00f5igepealt hinnatakse tingimust. Kui see on t\u00f5ene, tagastatakse v\u00e4ljend value_if_true. Kui see on vale, tagastatakse v\u00e4ljend value_if_false. Kolmikoperaatorit kasutatakse sageli kui \u00fclevaatlikumat alternatiivi if-else avaldisele olukordades, kus tingimus on lihtne ja saadud kood on kergesti loetav.<\/p>\n<p>N\u00e4iteks vaadelge j\u00e4rgmist if-else avaldust:<\/p>\n<p><code>if (x &gt; 0) { y = x; } else { y = -x; }<\/code><\/p>\n<p>Seda saab \u00fcmber kirjutada, kasutades kolmikoperaatorit j\u00e4rgmiselt:<\/p>\n<p><code>y = (x &gt; 0) ? x : -x;<\/code><\/p>\n<p>Kolmikoperaatorit saab ka keerulisemate tingimuste k\u00e4sitlemiseks sisseehitada. N\u00e4iteks:<\/p>\n<p><code>tulemus = (x &gt; 0) ? \"Positiivne\" : (x &lt; 0) ? &quot;Negatiivne&quot; : &quot;Null&quot;;<\/code><\/p>\n<p>See kood kontrollib, kas x on suurem kui 0. Kui on, siis tagastatakse v\u00e4\u00e4rtus \"Positiivne\". Kui mitte, siis kontrollitakse, kas x on v\u00e4iksem kui 0. Kui on, siis tagastatakse v\u00e4\u00e4rtus \"Negatiivne\". Kui x ei ole suurem ega v\u00e4iksem kui 0, tagastatakse v\u00e4\u00e4rtus \"Zero\".<\/p>\n<p>Kuigi ternaarne operaator v\u00f5ib m\u00f5nel juhul muuta koodi \u00fclevaatlikumaks ja kergemini loetavaks, v\u00f5ib see liigse v\u00f5i ebasobiva kasutamise korral muuta koodi ka raskemini m\u00f5istetavaks. Oluline on kasutada kolmikoperaatorit m\u00f5istlikult ja seada koodi selgus ja hooldatavus l\u00fchiduse asemel esikohale.<\/p>","protected":false},"excerpt":{"rendered":"<p>Tutvu ternaarse operaatoriga, mis on l\u00fchike viis if-else avalduste kirjutamiseks \u00fches koodireas. Kasutage seda m\u00f5istlikult l\u00fchikese ja loetava koodi saamiseks.<\/p>","protected":false},"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"class_list":["post-6667","dictionary","type-dictionary","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Ternary Operator - The Codest<\/title>\n<meta name=\"description\" content=\"Learn about the ternary operator, a shorthand way of writing if-else statements in a single line of code. Use it judiciously for concise, readable code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thecodest.co\/et\/sonastik\/ternaarne-operaator\/\" \/>\n<meta property=\"og:locale\" content=\"et_EE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ternary Operator\" \/>\n<meta property=\"og:description\" content=\"Learn about the ternary operator, a shorthand way of writing if-else statements in a single line of code. Use it judiciously for concise, readable code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thecodest.co\/et\/sonastik\/ternaarne-operaator\/\" \/>\n<meta property=\"og:site_name\" content=\"The Codest\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-13T11:50:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thecodest.co\/app\/uploads\/2024\/06\/cover-image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minut\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/\",\"name\":\"Ternary Operator - The Codest\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\"},\"datePublished\":\"2026-04-13T11:50:46+00:00\",\"dateModified\":\"2026-04-13T11:50:50+00:00\",\"description\":\"Learn about the ternary operator, a shorthand way of writing if-else statements in a single line of code. Use it judiciously for concise, readable code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/#breadcrumb\"},\"inLanguage\":\"et\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thecodest.co\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dictionary\",\"item\":\"https:\\\/\\\/thecodest.co\\\/sv\\\/ordbok\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Ternary Operator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"name\":\"The Codest\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thecodest.co\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"et\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"et\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"width\":144,\"height\":36,\"caption\":\"The Codest\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/pl.linkedin.com\\\/company\\\/codest\",\"https:\\\/\\\/clutch.co\\\/profile\\\/codest\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Ternaarne operaator - The Codest","description":"Tutvu ternaarse operaatoriga, mis on l\u00fchike viis if-else avalduste kirjutamiseks \u00fches koodireas. Kasutage seda m\u00f5istlikult l\u00fchikese ja loetava koodi saamiseks.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thecodest.co\/et\/sonastik\/ternaarne-operaator\/","og_locale":"et_EE","og_type":"article","og_title":"Ternary Operator","og_description":"Learn about the ternary operator, a shorthand way of writing if-else statements in a single line of code. Use it judiciously for concise, readable code.","og_url":"https:\/\/thecodest.co\/et\/sonastik\/ternaarne-operaator\/","og_site_name":"The Codest","article_modified_time":"2026-04-13T11:50:50+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/thecodest.co\/app\/uploads\/2024\/06\/cover-image.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minut"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/","url":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/","name":"Ternaarne operaator - The Codest","isPartOf":{"@id":"https:\/\/thecodest.co\/#website"},"datePublished":"2026-04-13T11:50:46+00:00","dateModified":"2026-04-13T11:50:50+00:00","description":"Tutvu ternaarse operaatoriga, mis on l\u00fchike viis if-else avalduste kirjutamiseks \u00fches koodireas. Kasutage seda m\u00f5istlikult l\u00fchikese ja loetava koodi saamiseks.","breadcrumb":{"@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb"},"inLanguage":"et","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/dictionary\/ternary-operator\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thecodest.co\/"},{"@type":"ListItem","position":2,"name":"Dictionary","item":"https:\/\/thecodest.co\/sv\/ordbok\/"},{"@type":"ListItem","position":3,"name":"Ternary Operator"}]},{"@type":"WebSite","@id":"https:\/\/thecodest.co\/#website","url":"https:\/\/thecodest.co\/","name":"The Codest","description":"","publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thecodest.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"et"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"et","@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","width":144,"height":36,"caption":"The Codest"},"image":{"@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/pl.linkedin.com\/company\/codest","https:\/\/clutch.co\/profile\/codest"]}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/et\/wp-json\/wp\/v2\/dictionary\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/et\/wp-json\/wp\/v2\/dictionary"}],"about":[{"href":"https:\/\/thecodest.co\/et\/wp-json\/wp\/v2\/types\/dictionary"}],"version-history":[{"count":1,"href":"https:\/\/thecodest.co\/et\/wp-json\/wp\/v2\/dictionary\/6667\/revisions"}],"predecessor-version":[{"id":10932,"href":"https:\/\/thecodest.co\/et\/wp-json\/wp\/v2\/dictionary\/6667\/revisions\/10932"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/et\/wp-json\/wp\/v2\/media?parent=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}