Branching and Loops Flashcards

1
Q

What does the IF branching allow you to do?

A

test for a condition…if the condition is TRUE, it runs the code inside the scope. if NOT TRUE, it skips over it.

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

What are two options available with the IF branching (tree)?

A

- Add 1 or more ELSE IF’s…they only run if the previous conditions were not met and the ELSE IF’s condition is TRUE.

- 1 final ELSE with no condition…it will always run if nothing else above did; it MUST always be the LAST statement of the IF tree

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

What does ToLower() do?

Write an example

A

it stores all data from an input string in lowercase

string userDay = Console.ReadLine().ToLower();

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

What does this code do?

if (userDay == “thursday”)

{
Console.WriteLine(“Only one more day to go!!!”);
}//end if

A

the code in the scope will run ONLY if the condition is TRUE

userDay must equal “thursday”…in lowercase

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

What does this code do…

** int userStrength = Convert.ToInt32(Console.ReadLine()); **

A

changes the input from the ReadLine() from a string to an int

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

if (userStrength > RABBIT_STRENGTH)
{
Console.WriteLine(“You brougt a holy hand grenade”
+ “ of Antioch and win!”);
}//end if
else if (userStrength == RABBIT_STRENGTH)
{
Console.WriteLine(“You chop off the rabbit’s tail, “
+ “but get nipped in the kneecaps.”);
}//end else if
else if (userStrength > (RABBIT_STRENGTH * .75))
{
Console.WriteLine(“You die a valiant death, but “
+ “put up a great fight.”);
}//end else
else if (userStrength > 0)
{
Console.WriteLine(“You became one of the bones!”);
}//end else if
else if (userStrength > 20)
{
Console.WriteLine(“This will never run!”);
}//end else if
else
{
Console.WriteLine(“Dude…you’re already dead…”);
}//end else

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

bool runMe = true;

if (runMe)
{
Console.WriteLine(“\nProgram running…\n”);
}//end if

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

** bool isAdmin = false;**

Console.WriteLine(“What is your username?”);
string username = Console.ReadLine().ToUpper();

if (username == “TONI” || username == “DUDE”)
{
isAdmin = true;
}//end if

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

**Console.WriteLine(“\nWould you like a game?”);
string userProgram = Console.ReadLine().ToUpper(); **

if (userProgram == “Y” || userProgram == “YES”)
{
Console.WriteLine(“PROGRAM RUNNING”);
** Console.WriteLine(“Type chess, frogger, or global war”); **
string userGames = Console.ReadLine().ToLower();

** if (userGames == “chess”)
{
Console.WriteLine(“We calculated that you would lose”**
** + “…so checkmate!”);**
** }//end else if chess**

** else if (userGames == “frogger”)
{
Console.WriteLine(“You’re too slow…SPLAT!”);
}//end else if frogger
else
{
Console.WriteLine(“Have a nice day! BOOM!”);
}//end else **

}//end if they typed yes or y

else if (userProgram == “N” || userProgram == “NO”)
{
Console.WriteLine(“**********DENIED**********”);

}//end if they typed no or n
else
{
Console.WriteLine(“Response not understood, “
+ “please restart.”);
}//end else

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

if (testScore >= 90)
{
Console.WriteLine(“Great job! You got an A!!”);
}//end if A

else if (testScore >= 80)
{
Console.WriteLine(“Fair job…got a B”);
}//end if B

else if (testScore >= 70)
{
Console.WriteLine(“You barely passed…C!”);
}//end if C

else if (testScore >= 60)
{
Console.WriteLine(“D? …You better study!”);
}//end if D

else if (testScore < 60) //could have also done else only with no condition
{
Console.WriteLine(“You should DROP OUT already!”);
}//end if F

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

What are switches?

A

another form of branching…not as flexible as IF;

doesn’t do ranges well

but GREAT for EXACT matching;

Also great for MENU options

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

What is the syntax for switch branching?

A
**switch (variablename)
 {
      case TEST:
      //code
      break;**
**     default
      //code
      break;**

}

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

Console.WriteLine(“Enter a number”);
short userNumber =
short.Parse(Console.ReadLine());

switch (userNumber)
{
case 1:
Console.WriteLine(“You typed 1”);
break;

            case 2:
             case 3:
             case 4:
             case 5:
             case 42:
                 Console.WriteLine("You picked 2-5 or 42");
             default:
                Console.WriteLine("Out of range");
                break;

        }//end switch
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the following mean…

IF YOU SEE THE ERROR CANNOT FALL THROUGH

A

a break is missing in a switch

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

How many looping options are there?

A

Do While

For

For Each

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

What is a do while loop?

A

best for when you want to run code an undeterminable number of times but at least once!!!

17
Q

What’s the syntax for a do while?

A

COUNTER
do

**{
     //code to run
     UPDATE
 }while (CONDITION);**
18
Q

** int cookie = 1;
do
{
Console.WriteLine(“Yum! You have had {0} cookies “, cookie);**

** cookie++;//UPDATE**

** } while (cookie <= 5);**

A
19
Q

bool repeat = true;

do
{
Console.WriteLine(“-=Wekk Ant Eeru Phone Company =-“);
Console.WriteLine(“B) Bill\nP) Payment\nS) Service”
+ “\nX) for Exit”);
string userChoice = Console.ReadLine().ToUpper();

Console.Clear();

switch (userChoice)
{
case “B”:
case “BILL”:
Console.WriteLine(“$500.00\n\n”);
break;

      case "P":
       case "PAYMENT":
       Console.WriteLine("Your payment is pending");
       break;

       case "S":
        case "SERVICE":
            Console.WriteLine("We will be there in 1-30 days."
              \+ "Someone must be present when we arrive.\n\n");
                         break;

        case "X":
         case "EXIT":
         Console.WriteLine("Good-bye!");

         repeat = false;
         break;

        default:
                Console.WriteLine("I pity the fooo that can't"
                  \+ " use my men.\n\nPlease try again.\n\n");
                  break;

            }//end switch

        } while (repeat);//CONDITION
A
20
Q

What’s the syntax for a for loop?

A

for (COUNTER; CONDITION; UPDATE)

**{
 //code to run
 }**
21
Q

** for (int COOKIE = 1; COOKIE <= 7; COOKIE++)
{
Console.WriteLine(“Yum! You have had “ + COOKIE);**

** }**

A
22
Q

Console.WriteLine(“\nHow many cookies do you want?”);

int userCookies = int.Parse(Console.ReadLine());

for (int cookie = 1; cookie <=userCookies; cookie++)
{
Console.WriteLine(“You at a cookie!” + cookie);
}

A
23
Q

string listofNames = “”;//typically called an empty string

Console.WriteLine(“\n\n\n\nREGISTRATION\n”
+ “How many people would you liek to “
+ “register for Doc Ock’s Emporium?”);
int totalReg = Convert.ToInt32(Console.ReadLine());

for (int reg = 1; reg <= totalReg; reg++)
{
Console.WriteLine(“Please enter a name: “);
listofNames += Console.ReadLine();

    listofNames += "\n";

        }
A
24
Q

What is this a definition of:

a specialized loop made for collections; it provides READ ONLY access

you can see the values in the collection, but you can NOT change them

A

foreach loop

25
Q

decimal[] cartPrices = { 12.99m, 2, 9.99m, 10, 20 };

** foreach (decimal price in cartPrices)
{
Console.WriteLine(“{0:c}”, price);**
** }**

A

**outputs the each price **

26
Q

decimal totalSale = 0;

Console.WriteLine(“\nThank you for purchasing from WeRProgrammers\n”);

**foreach (decimal price in cartPrices)
{
totalSale += price;

  Console.WriteLine("After adding {0:c}, the total is now: {1:c}", price, totalSale);**

}

A
27
Q

Define a LOOPING WHILE

A

While is an undeterminable # of times…

best for when you want to run code an indeterminable number of times

28
Q

What’s the syntax for a LOOPING WHILE

A

while (CONDITION)

**{
     //code to run
     UPDATE
 }**
29
Q

** int cookie = 1;**

while (cookie <= 5)//CONDITION…no SEMICOLON
{
//code to run
Console.WriteLine(“Yum! You have eaten “ + cookie);

** cookie++;**

}

A
30
Q

** int bags = 10;//COUNTER**

while (bags > 0) //CONDITION
{
Console.WriteLine(“You have {0} bags remaining”
+ “ You are now unloading one!”,
bags);
bags–;

** if (bags == 0)
{
Console.WriteLine(“\n\nYou’re DONE!! …now go write some code!”);
}//end if
}//end while**

A
31
Q

When is it most comman to use a while loop?

A

…with a bool for a counter

as this allows us to conditionally change the bool for when we want to exit the loop

32
Q

**When is it good to use a For loop versus a While loop? **

A

For loops are great for a specific number of times to loop

While loops are great for an indeterminable number of times