{"id":73763,"date":"2023-11-25T17:03:59","date_gmt":"2023-11-25T15:03:59","guid":{"rendered":"https:\/\/codeacademy.bg\/uncategorized-bg\/konstanti\/"},"modified":"2024-04-08T20:04:56","modified_gmt":"2024-04-08T17:04:56","slug":"konstanti","status":"publish","type":"post","link":"https:\/\/codeacademy.bg\/en\/programirane\/konstanti\/","title":{"rendered":"Programming Basics: Constants. When the Value Needs to Remain Unchanged"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">When do we need to use constants? In programming, when we talk about data and values, we first think of variables and constants. Variables represent values that can change during the program&#8217;s execution, while constants are values that cannot be changed after they are defined. This article will look at constants in programming, what they are, how they are declared, and how they are related to variables. We will provide practical examples and explore their significance in creating well-functioning programs and well-written and readable code.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Constants in Programming \u2013 When the Value Should Not Change in the Code<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> What Are Constants?<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a01.1. Definition: Constants in programming are values that remain unchanged during the program&#8217;s execution. They are used to represent fixed data, such as numbers, text, or other values that should not be changed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a01.2. Why Are They Important?: Constants are important because they provide stability and clarity in the program code. They make the code more readable and help programmers understand the author&#8217;s intentions.<\/span><\/p>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\"> Types<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a02.1. Numeric: These are constants that represent numbers. For example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`c<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0const int numberOfDaysInAWeek = 7;\u00a0 \/\/ A constant for the number of days in a week<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a02.2. String Constants: These are used to represent strings of characters or text. For example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0const char* greeting = &#8220;Hello, World!&#8221;;\u00a0 \/\/ A string constant with a greeting<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a02.3. Boolean: These are constants representing boolean values &#8220;true&#8221; or &#8220;false.&#8221; For example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`python<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0const bool isOnline = true;\u00a0 \/\/ A boolean constant indicating whether the user is online<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`<\/span><\/p>\n<ol start=\"3\">\n<li><span style=\"font-weight: 400;\"> Declaring and Using:<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a03.1. Declaration: To declare a constant, you use the keyword `const`, followed by the type of the constant and its name. For example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`java<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0final double pi = 3.14159265359;\u00a0 \/\/ Declaring a constant for the number Pi<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a03.2. Usage: Constants are used in the same way as variables, but they cannot be changed after being defined. For example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`c<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int radius = 5;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0const double pi = 3.14159265359;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0double circleArea = pi * radius * radius;\u00a0 \/\/ Using the constant &#8220;pi&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`<\/span><\/p>\n<ol start=\"4\">\n<li><span style=\"font-weight: 400;\"> Why Do We Use Them?<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a04.1. Improve Code Readability: Constants make the code more readable and clear. When we see a constant, we understand that its value does not change and that it is important for the program.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a04.2. Prevent Errors: Using constants prevents accidental changes to values that should not be changed. This can prevent serious errors in the program.<\/span><\/p>\n<ol start=\"5\">\n<li><span style=\"font-weight: 400;\"> Code Examples:<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a05.1. Example in C++:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const int numberOfMonths = 12;\u00a0 \/\/ A constant for the number of months in a year<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int daysInMonth = 30;\u00a0 \/\/ Number of days in a month<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int daysInYear = numberOfMonths * daysInMonth;\u00a0 \/\/ Using the constant<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#8220;Number of days in a year: &#8221; &lt;&lt; daysInYear &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a05.2. Example in Python:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`python<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0DAYS_IN_WEEK = 7\u00a0 # A constant for the number of days in a week<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0days_in_month = 30\u00a0 # Number of days in a month<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0days_in_year = DAYS_IN_WEEK * days_in_month\u00a0 # Using the constant<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(&#8220;Number of days in a year:&#8221;, days_in_year)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Constants are an essential tool in programming that help us create stable, readable, and reliable programs. They represent values that should not be changed during the program&#8217;s execution and make the code more understandable. With the proper use of constants, we can prevent errors and improve the quality of our code.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When do we need to use constants? In programming, when we talk about data and values, we first think of variables and constants. Variables represent values that can change during the program&#8217;s execution, while constants are values that cannot be changed after they are defined. This article will look at constants in programming, what they [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":73144,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[97,98],"tags":[],"class_list":["post-73763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programirane","category-programirane-za-nachinaesthi","grve-entry-item","grve-blog-item"],"_links":{"self":[{"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/posts\/73763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/comments?post=73763"}],"version-history":[{"count":1,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/posts\/73763\/revisions"}],"predecessor-version":[{"id":73764,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/posts\/73763\/revisions\/73764"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/media\/73144"}],"wp:attachment":[{"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/media?parent=73763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/categories?post=73763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/tags?post=73763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}