c# · level 1 · unit 6 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Ask an array what it holds, or loop to find the best.
To an array for one item, bring in a second toolbox with . Then arrays learn a new trick: fridge.Contains("jam") gives true if the item is there, false if not.
To find the biggest number, search with a loop. Keep the first item in a box called best. Each time you meet a bigger one, put that in best instead.
When the loop ends, best holds the biggest item. Swap > for < and the same trick finds the smallest.
using System;
using System.Linq;
string[] fridge = { "milk", "cheese", "jam" };
Console.WriteLine(fridge.Contains("jam"));
if (!fridge.Contains("eggs"))
{
Console.WriteLine("Add eggs to the shopping list.");
} Each using line sits at the top, before the blank line, one toolbox per line.
guess first
Find the best score. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
int[] scores = { 12, 30, 18 };
int best = scores[0];
foreach (int score in scores)
{
if (score > best)
{
best = score;
}
}
Console.WriteLine($"Best: {best}");
your turn
Your turn. Who is on the guest list?
Sam is knocking at the door. Add Sam to the guest list in the blank, with a capital S, so Contains lets them in. We will answer Sam.
output
press run to see what happens
fix it
The coldest day says 0, but no day was 0. Fix it!
output
press run to see what happens
coldeststarts at 0. Is any temperature in the array colder than 0?- Nothing beats 0, so the box never changes. Start with a real item from the array instead.
- Change line 4 to
int coldest = temps[0];, then press run.
nailed it
you can find things in an array now. :D
using System.Linq;brings in theContainstrick.Containsgivestruewhen the item is there.- Start
bestat the first item, then loop to beat it.
new words in your collection
- search
- System.Linq
This lesson isn’t saved. A free account remembers every lesson you finish.