Examples of linear search
Visual Studio 2010 (Similar to VB5, VB6 and Subsequent Visual Basic.NET languages)
In this example it is presumed that the array/list has already been populated.
In this example a label is used to output the result. It is possible to use other objects for output.
item_found = falsecounter = 0desired_item = inputbox (“Please enter the item that you would like to search for”, “Search Value”)DoIf desired_item = nation(counter) Thenitem_found = truelabelresult.Text = "The program found " & desired_item & " at position " & counter & " within the nation array."Elsecounter = counter + 1End IfLoop until counter = 10 Or item_found = trueIf item_found = false Thenlabelresult.Text = "The program could not find " & desired_item & " within the nation array"End IfLiveCode
Remember - Live code will index from 1 rather than 0!
In this example it is presumed that the array/list has already been populated.
put false into itemfoundput 1 into counterask "Please enter the item that you would like to search for"put it into desireditemrepeat until counter = 11 or itemfound = trueif desireditem = nation[counter] thenput true into itemfoundput “The program found " && desired_item && ”at position” && counter && “within the nation array.” into field "output"elseput counter + 1 into counterend Ifend repeatif itemfound = false thenput “The program could not find " && desired_item “within the nation array.” into field "output"end ifPython – Version 3.x
In this example it is presumed that the array/list has already been populated.
item_found = Falsecounter = 0desired_item = str(input(“Please enter the item that you would like to search for”))for counter in range (10):while counter ˂ 10 and not (item_found):If nation_list[counter] == desired_item:item_found = Trueprint ("The program found ”, desired_item, “ at position ”, counter, “ within the nation array.”)if item_found = False:print (“The program could not find ”, desired_item, “ within the nation array”.)Java
In this example it is presumed that the array/list has already been populated.
boolean found = false;int counter = 0;desired_item= JOptionPane.showInputDialog("Please enter the item that you would like to search for");do{if(nation[counter]=desired_item){Found = true;System.out.println("The program found " + desired_item + “at position ” + desired_item + “ of the nation array”);{counter ++} while (counter˂10 or found = false);if(found=false){System.out.println("The program did not find " + desired_item + “ within the nation array.”);}True BASIC
Remember – True Basic will index from 1 rather than 0!
In this example it is presumed that the array/list has already been populated.
LET found = 0LET counter = 1INPUT PROMPT “Please enter the item that you would like to search for”: desired_itemDOIF desired_item = nation(counter) THENLET found = 1PRINT "The program found ”; desired_item; “ at position”; counter; “of the nation array."ELSEcounter = counter + 1END IFLOOP UNTIL counter = 11 OR found = 1IF found = 0 THENPRINT "The program did not find ”; desired_item; “ within the nation array”END IFXojo (Formerly REALbasic)
In this example it is presumed that the array/list has already been populated.
The example presumes that you have access to a pre-designed method for creating an InputBox.
A static text box called ‘output’ has been used to output the minimum value.
item_found = falsecounter = 0desired_item = val(InputBox("Please enter the item that you would like to search for")DoIf nation(counter)=desired_item Thenitem_found = trueoutput.text = “The program found ” + desired_item + “ at position ” + counter + “ of the nation array.”Elsecounter = counter + 1End IfLoop Until item_found = true or counter = 10If item_found = false Thenoutput.text = “The program did not find ” + desired_item + “ within the nation array.”EndPascal
In this example it is presumed that the array/list has already been populated.
beginfound:=false;counter:=0;Writeln (‘Please enter the item that you would like to search for’)Readln(desired_item);repeatif (nation[counter] = desired_item) then found:=true;writeln(output, 'The program found ’, desired_item, ‘ at position ’, counter, ‘ of the nation array.’);elsecounter:=counter+1;until (found = true) or (counter = 10) ;if found:=false thenwriteln(output, 'The program did not find ’, desired_item, ‘ in the nation array.’);end;Comal
In this example it is presumed that the array/list has already been populated.
found := FALSEcounter% := 0PRINT "Please enter a score between 0 and 50";INPUT desired_itemREPEATIF nation (counter%) = desired_item THEN found := TRUEPRINT “The program found ”, desired_item%, “ at position ”, counter%, “of the nation array.”ELSEcounter% := counter% + 1END IFUNTIL (found = TRUE) OR (counter% = 10)IF found:= FALSE THENPRINT “The program did not find ”, desired_item%, “ within the nation array.”END IF