{"id":1171,"date":"2012-02-12T15:01:59","date_gmt":"2012-02-12T09:31:59","guid":{"rendered":"http:\/\/www.automationrepository.com\/?p=1171"},"modified":"2012-02-12T15:02:36","modified_gmt":"2012-02-12T09:32:36","slug":"conditional-statements-in-vbscript","status":"publish","type":"post","link":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/","title":{"rendered":"Part 4: QTP and VBSCript | Conditional Statements in VBScript"},"content":{"rendered":"<div style=\"text-align: justify; font-family: Verdana;\">\n<p>Usually, the test cases you work on wouldn&#8217;t have a straight linear flow. You would normally come across many conditions in your test case flow and based on the results of these conditions you would be required to perform different actions. In VBScript this can be achieved using the following 2 conditional statements &#8211;<!--more--><\/p>\n<ul>\n<li><strong># If&#8230;Then&#8230;Else statement<\/strong><\/li>\n<li><strong># Select Case statement<\/strong><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"Decision making using VBScript Conditional Statements\" src=\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/conditional-statements-vbscript.png\" alt=\"Decision making using VBScript Conditional Statements\" width=\"282\" height=\"362\" \/>Let&#8217;s see how to use these conditional statements in our scripts.<\/p>\n<h3>If&#8230; Then&#8230; Else statement<\/h3>\n<p>The If Then Else statement evaluates whether a condition is true or false, and then based upon the result certain set of statements are run. Let&#8217;s see a basic example for this.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\ndtSomeDate = #01\/01\/2012#\r\n\r\n'Check if the given date was in the past\r\nIf dtSomeDate &lt; Now Then\r\n  msgbox &quot;The given date is in past&quot;\r\nEnd If\r\n<\/pre>\n<p><em> <\/em><br \/>\nIn the above example, only one condition was checked in the If statement. If you want, you can check multiple conditions using multiple ElseIf statements. Let&#8217;s see an example for this.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Code to check multiple conditions using Else If statement\r\n\r\nDim sMonth\r\n\r\n'Find the month from today's date\r\nsMonth = Month(Now)\r\n\r\nIf sMonth = 1 Then\r\n  msgbox &quot;Month is January&quot;\r\nElseIf sMonth = 2 Then\r\n  msgbox &quot;Month is February&quot;\r\nElseIf sMonth = 3 Then\r\n  msgbox &quot;Month is March&quot;\r\n  '..... so on ....\r\nEnd If\r\n<\/pre>\n<p><em> <\/em><br \/>\nYou can check multiple conditions within a single If statement using <strong>And<\/strong> &amp; <strong>Or<\/strong> operators.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Find if a given number is given number if even and is less than 100\r\n\r\nDim iNum\r\niNum = 50\r\n\r\nIf (iNum mod 2 = 0) and (iNum &lt; 100) Then\r\n  msgbox iNum &amp; &quot; is a even number less than 100&quot;\r\nEnd If\r\n<\/pre>\n<p><em> <\/em><br \/>\n<strong>Nested If Statements<\/strong> Nested If statement is where you can have an If statement within an If statement.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Find if a given number is given number if even and is less than 100\r\n\r\nDim iNum\r\niNum = 50\r\n\r\nIf iNum mod 2 = 0 Then\r\n  'Check If number is less than 100 or not\r\n  If iNum &lt; 100 Then\r\n    msgbox iNum &amp; &quot; is an even number less than 100&quot;\r\n  Else\r\n    msgbox iNum &amp; &quot; is an even number greater than 100&quot;\r\n  End If\r\nElse\r\n  msgbox iNum &amp; &quot; is an odd number&quot;\r\nEnd If\r\n<\/pre>\n<p><span><br \/>\n<\/span><\/p>\n<h3>Select Case statement<\/h3>\n<p>Select Case statement works on a single expression which is evaluated at the beginning on the Select Case statement. The result of the expression is then compared with the values for each Case in the structure. Select Case statement is similar to If Then Else but it makes the code more readable. Let&#8217;s see an example of Select Case statement.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDim iWeek\r\n\r\niWeek= 3\r\n\r\nSelect Case iWeek\r\n  Case 1\r\n    msgbox &quot;Today is Sunday&quot;\r\n  Case 2\r\n    msgbox &quot;Today is Monday&quot;\r\n  Case 3\r\n    msgbox &quot;Today is Teusday&quot;\r\n  Case 4\r\n    msgbox &quot;Today is Wednesday&quot;\r\n  Case 5\r\n    msgbox &quot;Today is Thursday&quot;\r\n  Case 6\r\n    msgbox &quot;Today is Friday&quot;\r\n  Case 7\r\n    msgbox &quot;Today is Saturday&quot;\r\n  Case Else\r\n    msgbox &quot;Invalid Number&quot;\r\nEnd Select\r\n<\/pre>\n<p><em><br \/>\nSimilar to nested if statements, you can also have nested select case statement. You can also create select case within an if statement and vice-verse.<\/em><\/p>\n<p>This was all about the conditional statements in VBScript. You can visit <a title=\"QTP VBScript Tutorials\" href=\"http:\/\/www.automationrepository.com\/category\/qtp-and-vbscript-tutorials\/\">QTP VBScript Tutorials<\/a> page to read all the tutorials on VBScript.<br \/>\n<span><br \/>\n<\/span><\/p>\n<\/div>\n<div style=\"font-family: Verdana,sans-serif; border: 1px solid #C8B560; padding: 3mm; background: #FFF8C6; text-align: center;\">\n<p><strong>If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.<\/strong><\/p>\n<form style=\"text-align: center;\" action=\"http:\/\/feedburner.google.com\/fb\/a\/mailverify\" method=\"post\" onsubmit=\"window.open('http:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=automationrepository\/feeds', 'popupwindow', 'scrollbars=yes,width=550,height=520');return true\" target=\"popupwindow\"><input style=\"width: 160px; height: 20px;\" onfocus=\"if (this.value == 'Enter Your Email Address') {this.value = '';}\" onblur=\"if (this.value == '') {this.value = 'Enter Your Email Address';}\" type=\"text\" value=\"Enter Your Email Address\" \/> <input type=\"hidden\" name=\"uri\" value=\"automationrepository\/feeds\" \/> <input type=\"hidden\" name=\"loc\" value=\"en_US\" \/> <input type=\"submit\" value=\"Join Us\" \/><\/p>\n<p style=\"text-align: justify;\">\n<\/form>\n<\/div>\n<p style=\"border: 1px solid #C38EC7; padding: 3mm; background: #EBDDE2;\">To check out more tutorials, visit our <a title=\"QTP Tutorials\" href=\"http:\/\/www.automationrepository.com\/tutorials-for-qtp-beginners\/\" target=\"_blank\">QTP Tutorials<\/a> page. You can also check the <a title=\"All Articles\" href=\"http:\/\/www.automationrepository.com\/archive\/\" target=\"_blank\">Archives<\/a> page to view the list of all our articles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usually, the test cases you work on wouldn&#8217;t have a straight linear flow. You would normally come across many conditions in your test case flow and based on the results of these conditions you would be required to perform different actions. In VBScript this can be achieved using the following 2 conditional statements &#8211;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64,37],"tags":[71,72],"class_list":["post-1171","post","type-post","status-publish","format-standard","hentry","category-qtp-and-vbscript-tutorials","category-qtp-tutorials-for-beginners","tag-if-then-else","tag-select-case"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Part 4: QTP and VBSCript | If Then Else and Select Case Statement<\/title>\n<meta name=\"description\" content=\"This article covers If then Else statement and Select Case statement. It also talks about checking multiple conditions within an if statement.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anish Pillai\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/\",\"name\":\"Part 4: QTP and VBSCript | If Then Else and Select Case Statement\",\"isPartOf\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/conditional-statements-vbscript.png\",\"datePublished\":\"2012-02-12T09:31:59+00:00\",\"dateModified\":\"2012-02-12T09:32:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\"},\"description\":\"This article covers If then Else statement and Select Case statement. It also talks about checking multiple conditions within an if statement.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#primaryimage\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/conditional-statements-vbscript.png\",\"contentUrl\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/conditional-statements-vbscript.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.automationrepository.com\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Part 4: QTP and VBSCript | Conditional Statements in VBScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/\",\"name\":\"XX\",\"description\":\"\\r\\nasas\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\",\"name\":\"Anish Pillai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"caption\":\"Anish Pillai\"},\"description\":\"Find more about Anish Pillai on Google+\",\"sameAs\":[\"http:\/\/www.automationrepository.com\"],\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Part 4: QTP and VBSCript | If Then Else and Select Case Statement","description":"This article covers If then Else statement and Select Case statement. It also talks about checking multiple conditions within an if statement.","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":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/","twitter_misc":{"Written by":"Anish Pillai","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/","url":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/","name":"Part 4: QTP and VBSCript | If Then Else and Select Case Statement","isPartOf":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#primaryimage"},"image":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/conditional-statements-vbscript.png","datePublished":"2012-02-12T09:31:59+00:00","dateModified":"2012-02-12T09:32:36+00:00","author":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20"},"description":"This article covers If then Else statement and Select Case statement. It also talks about checking multiple conditions within an if statement.","breadcrumb":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#primaryimage","url":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/conditional-statements-vbscript.png","contentUrl":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/conditional-statements-vbscript.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/02\/conditional-statements-in-vbscript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.automationrepository.com\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Part 4: QTP and VBSCript | Conditional Statements in VBScript"}]},{"@type":"WebSite","@id":"https:\/\/www.automationrepository.com\/wordpress\/#website","url":"https:\/\/www.automationrepository.com\/wordpress\/","name":"XX","description":"\r\nasas","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20","name":"Anish Pillai","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","caption":"Anish Pillai"},"description":"Find more about Anish Pillai on Google+","sameAs":["http:\/\/www.automationrepository.com"],"url":"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/"}]}},"_links":{"self":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1171"}],"version-history":[{"count":27,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1171\/revisions"}],"predecessor-version":[{"id":1204,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1171\/revisions\/1204"}],"wp:attachment":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}