SLR1 Useful Code to learn Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What code counts the elements in an array?

A

[int] numberArray.Count()
[string] stringArray.Count()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What should the statement switch be used for?

A

Use the switch statement to select one of many code blocks to be executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Code for a switch:

A

switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
break;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the switch statement work?

A

This is how it works:

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is the break needed in a switch statement?

A

When C# reaches a break keyword, it breaks out of the switch block.

This will stop the execution of more code and case testing inside the block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

default code in switch code and why its needed:

A

default:
Console.WriteLine(“Looking forward to the Weekend.”);
break;

default keyword is optional and specifies some code to run if there is no case match

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

shorthand for if else statements:

A

variable = (condition) ? expressionTrue : expressionFalse;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

continue statement

A

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop

if (i == 4)
{
continue;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

creating an array

A

string[] cars;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to output the first elements of an array:

string[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};

A

Console.WriteLine(cars[0]);
// Outputs Volvo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Changing the first element in an array to Opel

string[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};

A

cars[0] = “Opel”;
Console.WriteLine(cars[0]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Printing the length of the array

string[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};

A

Console.WriteLine(cars.Length);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Declaring an array and adding new values to it (cars again)

A

// Declare an array
string[] cars;

// Add values, using new
cars = new string[] {“Volvo”, “BMW”, “Ford”};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Print all the elements in the array:

string[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};

A

for (int i = 0; i < cars.Length; i++)
{
Console.WriteLine(cars[i]);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What type of loop is used exclusively to loop through an array?

string[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};

A

a foreach loop

foreach (string i in cars)
{
Console.WriteLine(i);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly