{"id":4541,"date":"2014-01-14T19:04:00","date_gmt":"2014-01-14T11:04:00","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=4541"},"modified":"2018-02-17T15:53:19","modified_gmt":"2018-02-17T07:53:19","slug":"visual-basic-2013-lesson-13-making-decisions-using-if-then-else","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-13-making-decisions-using-if-then-else\/","title":{"rendered":"Visual Basic 2013 Lesson 13:  Using If&#8230;Then&#8230;Else"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-12-string-manipulation\/\">[Lesson 12] <\/a>&lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-14-making-decisions-using-select-case\/\">[Lesson 14]<\/a><\/h4>\n<p>In the previous lessons, we have learned how to write code that accepts input from the user and displays the output. However, we cannot control the program flow. In this lesson, we shall learn how to write VB2013 code that can make decisions and control the program flow.<\/p>\n<p>Decision-making process is an\u00a0integral part of programming in Visual Basic 2013. The ability to make decision helps solve problems intelligently and provide useful output to the user. For example, we can write a Visual Basic 2013 program that can ask the computer to perform a certain task until a certain condition is met or a program that will reject non-numeric data. In order to control the program flow and to make decisions, we need to use the conditional operators and the logical operators together with the If&#8230;Then&#8230;Else control structure.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h2>13.1 Conditional Operators<\/h2>\n<p>The conditional operators are powerful tools that resemble mathematical operators. These operators allow a Visual Basic 2013 program to compare data values and then decides what actions to take, whether to execute a program or terminate the program. They are also known as numerical comparison operators. They are used to compare two values to see whether they are equal, one value is greater or less than the other value. The comparison will return a true or false result. These operators are shown in the Table 13.1.<\/p>\n<h4>Table 13.1 Conditional Operators<\/h4>\n<div style=\"overflow-x: auto;\">\n<table>\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>\u00a0=<\/td>\n<td>\u00a0Equal to<\/td>\n<\/tr>\n<tr>\n<td>\u00a0&gt;<\/td>\n<td>\u00a0Greater than<\/td>\n<\/tr>\n<tr>\n<td>\u00a0&lt;<\/td>\n<td>\u00a0Less than<\/td>\n<\/tr>\n<tr>\n<td>\u00a0&gt;=<\/td>\n<td>\u00a0Equal to or Greater than<\/td>\n<\/tr>\n<tr>\n<td>&lt;=<\/td>\n<td>\u00a0Less than or Equal to<\/td>\n<\/tr>\n<tr>\n<td>\u00a0&lt;&gt;<\/td>\n<td>\u00a0Not equal to<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h2>13.2 Logical Operators<\/h2>\n<p>Sometimes we might need to make more than one comparisons before a decision can be made and an action taken. In this case, using numerical comparison operators alone is not sufficient, we need to use additional operators, and they are the logical operators.<\/p>\n<p>The logical operators are shown in the Table 13.2.<\/p>\n<h4>Table 13.2 Logical Operators<\/h4>\n<div style=\"overflow-x: auto;\">\n<table>\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>And<\/td>\n<td>Both sides must be true<\/td>\n<\/tr>\n<tr>\n<td>Or<\/td>\n<td>One side or other must be true<\/td>\n<\/tr>\n<tr>\n<td>Xor<\/td>\n<td>One side or other must be true but not both<\/td>\n<\/tr>\n<tr>\n<td>Not<\/td>\n<td>Negates true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>the above operators can be used to compare numerical data as well as non-numeric data such as text(string). \u00a0In making strings comparison, there are certain rules to follows: Upper case letters are less than lowercase letters, \u201cA\u201d&lt;\u201dB\u201d&lt;\u201dC\u201d&lt;\u201dD\u201d\u2026\u2026.&lt;\u201dZ\u201d and number are less than letters.<\/p>\n<h2>13.3 Using the If control structure with the Comparison Operators<\/h2>\n<p>To effectively control the Visual Basic 2013 program flow, we shall use the If control structure together with the conditional operators and logical operators. There are basically three types of If control structures, namely If\u2026.Then statement, If\u2026Then\u2026Else statement and If\u2026Then\u2026ElseIf statement.<\/p>\n<h3>13.3(a) If\u2026.Then Statement<\/h3>\n<p>This is the simplest control structure which instructs the computer to perform a certain action specified by the Visual Basic 2013 expression if the condition is true. However, when the condition is false, no action will be performed. The syntax for the if\u2026then statement is<\/p>\n<pre style=\"font-size: 110%;\">If condition Then\r\n\r\nVisual Basic 2013 expressions\r\n\r\nEnd If\r\n<\/pre>\n<p><strong>Example 13.1<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\nDim myNumber As Integer\r\n myNumber = TextBox1.Text\r\n If myNumber &gt; 100 Then\r\n Label2.Text = \u201d You win a lucky prize\u201d\r\n End If\r\nEnd Sub\r\n<\/pre>\n<p>* When you run the program and enter a number that is greater than 100, you will see the \u201cYou win a lucky prize\u201d statement. On the other hand, if the number entered is less than or equal to 100, you don\u2019t see any display.<\/p>\n<h3>13.3(b) If\u2026Then\u2026Else Statement<\/h3>\n<p>Using just If\u2026.Then statement is not very useful in programming and it does not provide choices for the users. In order to provide a choice, we can use the If\u2026.Then\u2026Else Statement. This control structure will ask the computer to perform a certain action specified by the Visual Basic 2013 expression if the condition is met. And when the condition is false, an alternative action will be executed. The syntax for the if\u2026then&#8230; Else statement is<\/p>\n<p><strong>Example 13.2<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\nDim myNumber As Integer\r\n myNumber = TextBox1.Text\r\n If myNumber &gt; 100 Then\r\n  Label2.Text = \u201d Congratulation! You win a lucky prize\u201d\r\n Else\r\n  Label2.Text = \u201d Sorry, You did not win any prize\u201d\r\n End If\r\nEnd Sub\r\n<\/pre>\n<p>* When you run the program and enter a number that is greater than 100, the statement \u201cCongratulation! You win a lucky prize\u201d will be shown. On the other hand, if the number entered is less than or equal to 100, you will see the \u201cSorry, You did not win any prize\u201d statement<\/p>\n<p><strong>Example 13.3<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\nDim myNumber, MyAge As Integer\r\n myNumber = TextBox1.Text\r\n MyAge = TextBox2.Text\r\n If myNumber &gt; 100 And myAge &gt; 60 Then\r\n  Label2.Text = \u201d Congratulation! You win a lucky prize\u201d\r\n Else\r\n  Label2.Text = \u201d Sorry, You did not win any prize\u201d\r\nEnd If\r\nEnd Sub\r\n<\/pre>\n<p>* This program uses the logical operator And besides the conditional operators. This means that both the conditions must be fulfilled in order for the conditions to be true, otherwise, the second block of code will be executed. In this example, the number entered must be more than 100 and the age must be more than 60 in order to win a lucky prize, any one of the above conditions not fulfilled will disqualify the user from winning a prize.<\/p>\n<h3>13.3(c) If\u2026Then\u2026ElseIf Statement<\/h3>\n<p>If there are more than two alternative choices, using just If\u2026Then\u2026Else statement will not be enough. In order to provide more choices, we can use the If\u2026.Then\u2026ElseIf Statement.The general structure for the if\u2026then&#8230; Elseif statement is<\/p>\n<pre style=\"font-size: 110%;\">If condition Then\r\nVisual Basic 2013 expression\r\nElseIf condition Then\r\nVisual Basic 2013 expression\r\nElseIf condition Then\r\nVisual Basic 2013 expression\r\n\r\n.\r\n.\r\n\r\nElse\r\nVisual Basic 2013 expression\r\nEnd If\r\n<\/pre>\n<p><strong>Example 13.4<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\nDim Mark As Integer\r\nDim Grade as String\r\n\r\nMark = TextBox1.Text\r\nIf myNumber &gt;=80 Then\r\n Grade=\u201dA\u201d\r\nElseIf Mark&gt;=60 And Mark &lt;80\r\n Grade=\"B\"\r\nElseIf\u00a0Mark&gt;=40 And Mark &lt;60\r\n Grade=\u201dC\u201d\r\nElse\r\nGrade=\u201dD\u201d\r\nEnd If\r\nEnd Sub\r\n<\/pre>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"2306771905\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-12-string-manipulation\/\">[Lesson 12]\u00a0<\/a>&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents]<\/a>\u00a0&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-14-making-decisions-using-select-case\/\">[Lesson 14]<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 12] &lt;&lt; [Contents] &gt;&gt; [Lesson 14] In the previous lessons, we have learned how to write code that accepts input from the user and displays the output. However, we cannot control the program flow. In this lesson, we shall learn how to write VB2013 code that can make decisions and control the program flow. &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-13-making-decisions-using-if-then-else\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2013 Lesson 13:  Using If&#8230;Then&#8230;Else<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-4541","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2013 Lesson 13: Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This article explains the usage of if...then...else in visual basic 2013\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2013 Lesson 13: Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This article explains the usage of if...then...else in visual basic 2013\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2018-02-17T07:53:19+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-13-making-decisions-using-if-then-else\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html\",\"name\":\"Visual Basic 2013 Lesson 13: Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"datePublished\":\"2014-01-14T11:04:00+00:00\",\"dateModified\":\"2018-02-17T07:53:19+00:00\",\"description\":\"This article explains the usage of if...then...else in visual basic 2013\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2013 Lesson 13: Using If&#8230;Then&#8230;Else\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2013 Lesson 13: Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This article explains the usage of if...then...else in visual basic 2013","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":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2013 Lesson 13: Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This article explains the usage of if...then...else in visual basic 2013","og_url":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-02-17T07:53:19+00:00","twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-13-making-decisions-using-if-then-else\/","url":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html","name":"Visual Basic 2013 Lesson 13: Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"datePublished":"2014-01-14T11:04:00+00:00","dateModified":"2018-02-17T07:53:19+00:00","description":"This article explains the usage of if...then...else in visual basic 2013","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson13.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2013 Lesson 13: Using If&#8230;Then&#8230;Else"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=4541"}],"version-history":[{"count":67,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4541\/revisions"}],"predecessor-version":[{"id":12468,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4541\/revisions\/12468"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=4541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=4541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=4541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}