{"id":2342,"date":"2013-02-12T15:42:33","date_gmt":"2013-02-12T07:42:33","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2017-11-10T12:40:01","modified_gmt":"2017-11-10T04:40:01","slug":"visual-basic-2012-lesson-9-using-if-then-else","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-9-using-if-then-else\/","title":{"rendered":"Visual Basic 2012 Lesson 9- Using If&#8230;Then&#8230;Else"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-8-string-manipulation\/\">[Lesson 8]<\/a> <\/strong>&lt;&lt; <strong> <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS]<\/a> &gt;&gt; <a title=\"Vb2012 Lesson 10\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-10-using-select-case\/\">[Lesson 10]<\/a> <\/strong><\/h4>\n<p>In this lesson, you will learn how to write Visual Basic 2012 code that can make decisions when it processes input from the user and controls the program flow.<\/p>\n<h3>9.1 Decision Making using If&#8230;Then&#8230;Else<\/h3>\n<p>For example, we can write a Visual Basic 2012 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 use the If&#8230;Then&#8230;Else control structure. The if&#8230;Then&#8230;Else control structure employs the conditional operators and the logical operators to make decision<\/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=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>9.2 Conditional Operators<\/h3>\n<p>The conditional operators are powerful tools that resemble mathematical operators. These operators allow a Visual Basic 2012 program to compare data values and then decide what actions to take, whether to execute a program or terminate the program and more. They are also known as numerical comparison operators. Normally they are used to compare two values to see whether they are equal or one value is greater or less than the other value. The comparison will return a true or false result. These operators are shown in Table 9.1.<\/p>\n<h4>Table 9.1 Conditional Operators<\/h4>\n<div style=\"overflow-x:auto;\">\n<table style=\"width: 80%; margin-left: auto; margin-right: auto;\">\n<tbody>\n<tr>\n<th style=\"text-align: center;\">Operator<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u00a0=<\/td>\n<td>\u00a0Equal to<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u00a0&gt;<\/td>\n<td>\u00a0Greater than<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u00a0&lt;<\/td>\n<td>\u00a0Less than<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u00a0&gt;=<\/td>\n<td>\u00a0Equal to or Greater than<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">&lt;=<\/td>\n<td>\u00a0Less than or Equal to<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u00a0&lt;&gt;<\/td>\n<td>\u00a0Not equal to<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>9.2 Logical Operators<\/h3>\n<p>Sometimes we might need to make more than one comparisons before a decision can be made. In this case, using numerical comparison operators alone is not sufficient, we need to use the logical operators. These logical operators are shown in Table 9.2.<\/p>\n<h4>Table 9.2 Logical Operators<\/h4>\n<div style=\"overflow-x:auto;\">\n<table style=\"width: 80%; margin-left: auto; margin-right: auto;\">\n<tbody>\n<tr>\n<th style=\"text-align: center;\">Operator<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">And<\/td>\n<td>Both sides must be true<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Or<\/td>\n<td>One side or other must be true<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Xor<\/td>\n<td>One side or other must be true but not both<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Not<\/td>\n<td>Negates true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\/div><\/p>\n<p>* In making strings comparison, there are certain rules to follows: Upper case letters are less than lowercase letters, &#8220;A&#8221;&lt;&#8220;B&#8221;&lt;&#8220;C&#8221;&lt;&#8220;D&#8221;&#8230;&#8230;.&lt;&#8220;Z&#8221; and number are less than letters.<\/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=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>9.3 Using the If control structure with the Comparison Operators<\/h3>\n<p>To effectively control the Visual Basic 2012 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 <strong>If&#8230;.Then<\/strong> statement, <strong>If&#8230;.Then&#8230; Else <\/strong>statement and<strong> If&#8230;.Then&#8230;.ElseIf<\/strong> statement.<\/p>\n<h4>9.3(a) If&#8230;.Then Statement<\/h4>\n<p>This is the simplest control structure which instructs the computer to perform a certain action specified by the Visual Basic 2012 expression if the condition is true. However, when the condition is false, no action will be performed. The syntax for the if&#8230;then.. statement is<\/p>\n<pre style=\"font-size: 110%;\">If condition Then\r\n\r\nVisual Basic 2012  expression\r\n\r\nEnd If\r\n<\/pre>\n<p><strong>Example 9.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\nmyNumber = TextBox1.Text\r\nIf myNumber &gt; 100 Then\r\nLabel2.Text = \" You win a lucky prize\"\r\nEnd 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 &#8220;You win a lucky prize&#8221; statement. On the other hand, if the number entered is less than or equal to 100, you don&#8217;t see any display.<\/p>\n<h4>9.3(b) If&#8230;.Then&#8230;Else Statement<\/h4>\n<p>Using just If&#8230;.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&#8230;.Then&#8230;Else Statement. This control structure will ask the computer to perform a certain action specified by the Visual Basic 2012 expression if the condition is met. And when the condition is false, an alternative action will be executed.<\/p>\n<p>The syntax for the if&#8230;then&#8230; Else statement is<\/p>\n<pre style=\"font-size: 110%;\">If condition Then<span style=\"font-size: 16px;\">\r\nVisual Basic 2012 expression<\/span>\r\nElse\r\nVisual Basic 2012  expression\r\nEnd If\r\n<\/pre>\n<p><strong>Example 9.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 = \" Congratulation! You win a lucky prize\"\r\n Else\r\n  Label2.Text = \" Sorry, You dif not win any prize\"\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 &#8220;Congratulation! You win a lucky prize&#8221; will be shown. On the other hand, if the number entered is less than or equal to 100, you will see the &#8220;Sorry, You did not win any prize&#8221; statement<\/p>\n<h4>Example 9.3<\/h4>\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\r\n If myNumber &gt; 100 And myAge &gt; 60 Then\r\n  Label2.Text = \" Congratulation! You win a lucky prize\"\r\n Else\r\n  Label2.Text = \" Sorry, You did not win any prize\"\r\n End If\r\nEnd Sub\r\n<\/pre>\n<p>* This program use the logical operator <strong>And<\/strong> 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<h4>9.3(c) If&#8230;.Then&#8230;ElseIf Statement<\/h4>\n<p>If there are more than two alternative choices, using just If&#8230;.Then&#8230;.Else statement will not be enough. In order to provide more choices, we can use the If&#8230;.Then&#8230;ElseIf Statement. executed. The general format for the if&#8230;then&#8230; Else statement is<\/p>\n<pre style=\"font-size: 110%;\">If condition Then\r\n\r\nVisual Basic 2012  expression\r\n\r\nElseIf condition Then\r\n\r\nVisual Basic 2012  expression\r\n\r\nElseIf condition Then\r\n\r\nVisual Basic 2012  expression\r\n\r\n\r\n.\r\n\r\nElse\r\n\r\nVisual Basic 2012  expression\r\n\r\nEnd If\r\n<\/pre>\n<h4>Example 9.4<\/h4>\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 Mark = TextBox1.Text\r\n If myNumber &gt;=80 Then\r\n  Grade=\"A\"\r\n ElseIf Mark&gt;=60 and Mark&lt;80 then\r\n  Grade=\"B\"\r\n ElseIf Mark&gt;=40 and Mark&lt;60 then\r\n  Grade=\"C\"\r\n Else\r\n  Grade=\"D\"\r\n End 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=\"6598395509\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-8-string-manipulation\/\">[Lesson 8]<\/a> <\/strong>&lt;&lt; <strong> <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS]<\/a> &gt;&gt; <a title=\"VB2012 Lesson 10\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-10-using-select-case\/\">[Lesson 10]<\/a> <\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 8] &lt;&lt; [CONTENTS] &gt;&gt; [Lesson 10] In this lesson, you will learn how to write Visual Basic 2012 code that can make decisions when it processes input from the user and controls the program flow. 9.1 Decision Making using If&#8230;Then&#8230;Else For example, we can write a Visual Basic 2012 program that can ask the &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-9-using-if-then-else\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2012 Lesson 9- 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-2342","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 2012 Lesson 9- Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This Visual Basic 2012 tutorial lesson demonstrates the usage of If then else in VB2012 programming\" \/>\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\/vb2012\/vb2012_lesson9.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2012 Lesson 9- Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This Visual Basic 2012 tutorial lesson demonstrates the usage of If then else in VB2012 programming\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.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=\"2017-11-10T04:40:01+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-2012-lesson-9-using-if-then-else\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.html\",\"name\":\"Visual Basic 2012 Lesson 9- Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"datePublished\":\"2013-02-12T07:42:33+00:00\",\"dateModified\":\"2017-11-10T04:40:01+00:00\",\"description\":\"This Visual Basic 2012 tutorial lesson demonstrates the usage of If then else in VB2012 programming\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2012 Lesson 9- 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 2012 Lesson 9- Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This Visual Basic 2012 tutorial lesson demonstrates the usage of If then else in VB2012 programming","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\/vb2012\/vb2012_lesson9.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2012 Lesson 9- Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This Visual Basic 2012 tutorial lesson demonstrates the usage of If then else in VB2012 programming","og_url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.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":"2017-11-10T04:40:01+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-2012-lesson-9-using-if-then-else\/","url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.html","name":"Visual Basic 2012 Lesson 9- Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"datePublished":"2013-02-12T07:42:33+00:00","dateModified":"2017-11-10T04:40:01+00:00","description":"This Visual Basic 2012 tutorial lesson demonstrates the usage of If then else in VB2012 programming","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson9.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2012 Lesson 9- 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\/2342","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=2342"}],"version-history":[{"count":44,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2342\/revisions"}],"predecessor-version":[{"id":12469,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2342\/revisions\/12469"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=2342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=2342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=2342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}