3. One-dimensional arrays
• An array is a special variable, which can hold more than one value at a time
int[] a = new int[6];
a[0] = 12;
a[1] = 2;
a[2] = 1;
a[3] = 232;
a[4] = 16;
a[5] = 99;
Console.WriteLine(a[5]);//prints '99'
4. if and if-else statements
Condition
Previous
calculations
Some
calculations
Some
calculations
True False
Other program
calculations
Condition
Previous
calculations
Some
calculations
True
Other programs
calculations
ifIf-else
5. switch statement
expression
code block 1
code block 2
code block 3
code block N
case 1
case 1
case 1
default
Console.WriteLine("Enter girl's name:");
string girlName = Console.ReadLine();
switch (girlName)
{
case "Kate":
Console.WriteLine("Just ignore her.");
break;
case "Samanta":
Console.WriteLine("Give her your phone number!");
break;
case "Melissa":
Console.WriteLine("She has a birthday pretty soon!");
break;
default:
Console.WriteLine("Sorry, I don't know {0}", girlName);
break;
} Console.WriteLine("Enter girl's name:");
string girlName = Console.ReadLine();
if (girlName == "Kate")
{
Console.WriteLine("Just ignore her.");
}
else if (girlName == "Samanta")
{
Console.WriteLine("Give her your phone number!");
}
else if (girlName == "Melissa")
{
Console.WriteLine("She has a birthday pretty soon!");
}
else
{
Console.WriteLine("Sorry, I don't know {0}", girlName);
}
6. Loop operators: while, do-while
Condition
Previous calculations
body
True
False
Other calculations
Condition
Previous calculations
body
True
False
Other calculations
int moneyInWallet = 100;
int iPhonePrice = 499;
while (moneyInWallet < iPhonePrice)
{
//keep on making money
moneyInWallet = moneyInWallet + 1;
}
while
int moneyInWallet = 0;
int iPhonePrice = 499;
do
{
//keep on making money
moneyInWallet++; //increment
}
while (moneyInWallet > iPhonePrice);
do-while
8. if and for demo
for condition
initialization
True
False
Counter change
Other calculations
If condition
Some actions
True
False
for (int index = 0; index <= 10; index++)
{
if (index%2 == 0)
{
Console.WriteLine($"Number {index} in an even number");
}
}