TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Math.Max and Math.Min

Use the Math.Max and Math.Min Functions to compare arguments and get the larger or smaller one.
Math.Max, Math.Min. These functions find the largest, and smallest, values in the data. We provide an example. It searches an array in a single pass for the smallest and largest elements. It uses these useful but simple functions.
Example. We create an array of seven integer values upon the managed heap. Next we initialize the largest and smallest Integer variables. We set them to Integer.MinValue and Integer.MaxValue so that they will be reassigned as the loop iterates.Integer

For-Each: Here we continually assign largest and smallest. You can call Max and Min with any two integers.

For Each, For

Tip: If you use the integer you are assigning as an argument, the Integer variable will only increase (for Max) or decrease (for Min).

VB.NET program that uses Math.Max and Math.Min Module Module1 Sub Main() ' Find largest and smallest elements in array. Dim vals As Integer() = {1, 4, 100, -100, 200, 4, 6} Dim largest As Integer = Integer.MinValue Dim smallest As Integer = Integer.MaxValue For Each element As Integer In vals largest = Math.Max(largest, element) smallest = Math.Min(smallest, element) Next Console.WriteLine(largest) Console.WriteLine(smallest) End Sub End Module Output 200 -100
Performance. There are other ways to determine the maximum and minimum values in an array of Integers but some of these would require two separate passes through the array. With a single pass, we must read the elements in the array half as many times.
Summary. The Math.Max and Math.Min functions are a declarative way of writing an if-statement that tests the two values against each other. Because they result in shorter code, they are often preferable.

However: If other statements must be used, the if-statement is better because fewer branches are required with one selection statement.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf