{"id":1084,"date":"2012-04-06T16:38:54","date_gmt":"2012-04-06T08:38:54","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2017-11-06T20:19:42","modified_gmt":"2017-11-06T12:19:42","slug":"visual-basic-2010-lesson-9","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-9\/","title":{"rendered":"Visual Basic 2010 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-2010-lesson-8\/\">[Lesson 8]<\/a> <\/strong>&lt;&lt; <strong> <a href=\"..\/..\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-10\/\">[Lesson 10]<\/a> <\/strong><\/h4>\n<p>In this lesson, you will learn how to write Visual Basic 2010 code that can make decisions. It will process input from the user and control the program flow and outcomes based on the decisions.<\/p>\n<p>Decisions making is an important part of programming because it can solve practical problems intelligently and provide useful output to the user. For examples, we can write a VB program that can ask the computer to perform a certain task until a certain condition is met or a programme that will reject non-numeric data. In order to control the program flow and to make decisions, we will introduce the If&#8230;Then&#8230;Else control structure. To write VB code that involves If&#8230;Then&#8230;Else, we need to use the conditional operators and the logical operators.<\/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=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>9.1 Conditional Operators<\/h3>\n<p>The conditional operators are powerful tools that resemble mathematical operators. These operators allow a VB2010 program to compare data values and then decide what actions to take. For example, to execute an event or terminate an event. They are also known as numerical comparison operators. These operators are typically 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 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. In this case, using numerical comparison operators alone is not sufficient, we need to use the logical operators. The logical operators are shown in Table 9.2. The aforementioned operators are usually used to compare numerical data. However, you can also compare strings with the above operators. 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<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<\/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=\"1723562988\"><\/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 2010 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&#8230;.Then statement, If&#8230;.Then&#8230; Else statement and If&#8230;.Then&#8230;.ElseIf statement.<\/p>\n<h3>9.3(a) If&#8230;.Then Statement<\/h3>\n<p>This is the simplest control structure which asks the computer to perform a certain action specified by the Visual Basic 2010 expression if the condition is true. However, when the condition is false, no action will be performed. The general format for the if&#8230;then.. statement is<\/p>\n<pre style=\"font-size: 110%; width: 70%;\">If condition Then\r\n\r\nVisual Basic 2010 expression\r\n\r\nEnd If\r\n<\/pre>\n<h4>Example 9.1<\/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 As Integer\r\n myNumber = TextBox1.Text\r\nIf myNumber &gt; 100 Then\r\n Label2.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 only If&#8230;.Then the 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 2010 expression if the condition is true. And when the condition is false, if&#8230;then..an alternative action will be executed. The syntax of if&#8230;then&#8230;Else statement is<\/p>\n<pre style=\"font-size: 110%; width: 70%;\">If condition Then\r\n\r\nVisual Basic 2010 expression\r\n\r\nElse\r\n\r\nVisual Basic 2010 expression\r\n\r\nEnd If\r\n<\/pre>\n<h4>Example 9.2: Using If&#8230;Then&#8230;Else<\/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 As Integer\r\n myNumber = TextBox1.Text\r\nIf myNumber &gt; 100 Then\r\n Label2.Text = \" Congratulation! You win a lucky prize\"\r\nElse\r\n Label2.Text = \" Sorry, You dif not win any 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, 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 dif not win any prize&#8221; statement<\/p>\n<h4>Example 9.3<\/h4>\n<pre style=\"font-size: 110%; width: 70%;\">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\nIf myNumber &gt; 100 And myAge &gt; 60 Then\r\n Label2.Text = \" Congratulation! You win a lucky prize\"\r\nElse\r\n Label2.Text = \" Sorry, You did not win any prize\"\r\nEnd If\r\nEnd Sub\r\n<\/pre>\n<p>* This program uses the logical And operator 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 jus 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 syntax for the if&#8230;then.. Else statement is<\/p>\n<pre style=\"font-size: 110%; width: 70%;\">If condition Then\r\n Visual Basic 2010 expression\r\nElseIf condition Then\r\n Visual Basic 2010 expression\r\nElseIf condition Then\r\n Visual Basic 2010 expression\r\n\r\n.\r\n\r\nElse\r\n Visual Basic 2010 expression\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\nIf myNumber &gt;=80 Then\r\n Grade=\"A\"\r\nElseIf Mark&gt;=60 and Mark&lt;80 then\r\n Grade=\"B\"\r\nElseIf Mark&gt;=40 and Mark&lt;60 then\r\n Grade=\"C\"\r\nElse\r\n Grade=\"D\"\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=\"8075128701\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><strong><a href=\"..\/..\/index.php\/visual-basic-2010-lesson-8\/\">[Lesson 8]<\/a> <\/strong>&lt;&lt; <strong> <a href=\"..\/..\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-10\/\">[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 2010 code that can make decisions. It will process input from the user and control the program flow and outcomes based on the decisions. Decisions making is an important part of programming because it can solve practical &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-9\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2010 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-1084","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 2010 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 2010 lesson demonstrates the usage of If...Then...Else in Visual Basic 2010\" \/>\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\/vb2010\/vb2010_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 2010 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 2010 lesson demonstrates the usage of If...Then...Else in Visual Basic 2010\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_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-06T12:19:42+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-2010-lesson-9\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson9.html\",\"name\":\"Visual Basic 2010 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\":\"2012-04-06T08:38:54+00:00\",\"dateModified\":\"2017-11-06T12:19:42+00:00\",\"description\":\"This Visual Basic 2010 lesson demonstrates the usage of If...Then...Else in Visual Basic 2010\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson9.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson9.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson9.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2010 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 2010 Lesson 9- Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This Visual Basic 2010 lesson demonstrates the usage of If...Then...Else in Visual Basic 2010","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\/vb2010\/vb2010_lesson9.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2010 Lesson 9- Using If...Then...Else - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This Visual Basic 2010 lesson demonstrates the usage of If...Then...Else in Visual Basic 2010","og_url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_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-06T12:19:42+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-2010-lesson-9\/","url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson9.html","name":"Visual Basic 2010 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":"2012-04-06T08:38:54+00:00","dateModified":"2017-11-06T12:19:42+00:00","description":"This Visual Basic 2010 lesson demonstrates the usage of If...Then...Else in Visual Basic 2010","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson9.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson9.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson9.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2010 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\/1084","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=1084"}],"version-history":[{"count":60,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1084\/revisions"}],"predecessor-version":[{"id":12471,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1084\/revisions\/12471"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=1084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=1084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}