{"id":1156,"date":"2022-02-24T19:07:45","date_gmt":"2022-02-24T19:07:45","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1156"},"modified":"2022-02-24T19:07:45","modified_gmt":"2022-02-24T19:07:45","slug":"expressions-statements-and-blocks","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/expressions-statements-and-blocks\/","title":{"rendered":"\u00a0Expressions, Statements and Blocks"},"content":{"rendered":"\n<p>In this tutorial, you will learn about Java expressions, Java statements, difference between expression and statement, and Java blocks with the help of examples.<\/p>\n\n\n\n<p>In previous chapters, we have used expressions, statements, and blocks without much explaining about them. Now that you know about variables, operators, and literals, it will be easier to understand these concepts.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"expressions\">Java Expressions<\/h2>\n\n\n\n<p>A Java expression consists of\u00a0variables,\u00a0operators,\u00a0<a href=\"https:\/\/www.programiz.com\/java-programming\/variables-primitive-data-types#literals\">literals<\/a>, and method calls. To know more about method calls, visit\u00a0Java methods. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int score; \nscore = 90;\n<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>score = 90<\/code>&nbsp;is an expression that returns an&nbsp;<code>int<\/code>. Consider another example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Double a = 2.2, b = 3.4, result;\nresult = a + b - 3.4;\n<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>a + b - 3.4<\/code>&nbsp;is an expression.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (number1 == number2)\n    System.out.println(\"Number 1 is larger than number 2\");\n<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>number1 == number2<\/code>&nbsp;is an expression that returns a boolean value. Similarly,&nbsp;<code>\"Number 1 is larger than number 2\"<\/code>&nbsp;is a string expression.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"statements\">Java Statements<\/h2>\n\n\n\n<p>In Java, each statement is a complete unit of execution. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int score = 9*5;<\/code><\/pre>\n\n\n\n<p>Here, we have a statement. The complete execution of this statement involves multiplying integers&nbsp;<code>9<\/code>&nbsp;and&nbsp;<code>5<\/code>&nbsp;and then assigning the result to the variable&nbsp;<code>score<\/code>.<\/p>\n\n\n\n<p>In the above statement, we have an expression&nbsp;<code>9 * 5<\/code>. In Java, expressions are part of statements.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"expression-statements\">Expression statements<\/h3>\n\n\n\n<p>We can convert an expression into a statement by terminating the expression with a&nbsp;<code>;<\/code>. These are known as expression statements. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ expression\nnumber = 10\n\/\/ statement\nnumber = 10;\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have an expression&nbsp;<code>number = 10<\/code>. Here, by adding a semicolon (<code>;<\/code>), we have converted the expression into a statement (<code>number = 10;<\/code>).<\/p>\n\n\n\n<p>Consider another example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ expression\n++number\n\/\/ statement\n++number;\n<\/code><\/pre>\n\n\n\n<p>Similarly,&nbsp;<code>++number<\/code>&nbsp;is an expression whereas&nbsp;<code>++number;<\/code>&nbsp;is a statement.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"declaration-statements\">Declaration Statements<\/h3>\n\n\n\n<p>In Java, declaration statements are used for declaring variables. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Double tax = 9.5;<\/code><\/pre>\n\n\n\n<p>The statement above declares a variable&nbsp;<var>tax<\/var>&nbsp;which is initialized to&nbsp;<code>9.5<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: There are control flow statements that are used in decision making and looping in Java. You will learn about control flow statements in later chapters.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"blocks\">Java Blocks<\/h2>\n\n\n\n<p>A block is a group of statements (zero or more) that is enclosed in curly braces&nbsp;<code>{ }<\/code>. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n    public static void main(String&#91;] args) {\n    \t\n        String band = \"Beatles\";\n    \t\n        if (band == \"Beatles\") { \/\/ start of block\n            System.out.print(\"Hey \");\n            System.out.print(\"Jude!\");\n        } \/\/ end of block\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Hey Jude!<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have a block&nbsp;<code>if {....}<\/code>.<\/p>\n\n\n\n<p>Here, inside the block we have two statements:<\/p>\n\n\n\n<ul><li><code>System.out.print(\"Hey \");<\/code><\/li><li><code>System.out.print(\"Jude!\");<\/code><\/li><\/ul>\n\n\n\n<p>However, a block may not have any statements. Consider the following examples,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nclass Main {\n    public static void main(String&#91;] args) {\n    \t\n        if (10 &gt; 5) { \/\/ start of block\t\n \n        } \/\/ end of block\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>This is a valid Java program. Here, we have a block&nbsp;<code>if {...}<\/code>. However, there is no any statement inside this block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class AssignmentOperator {\n    public static void main(String&#91;] args) {  \/\/ start of block \t\n\n    } \/\/ end of block\n}\n<\/code><\/pre>\n\n\n\n<p>Here, we have block&nbsp;<code>public static void main() {...}<\/code>. However, similar to the above example, this block does not have any statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about Java expressions, Java statements, difference between expression and statement, and Java blocks with the help of examples. In previous chapters, we have used expressions, statements, and blocks without much explaining about them. Now that you know about variables, operators, and literals, it will be easier to understand these [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[290],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1156"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1156"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1156\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}