Branching and Loops Flashcards
What does the IF branching allow you to do?
test for a condition…if the condition is TRUE, it runs the code inside the scope. if NOT TRUE, it skips over it.
What are two options available with the IF branching (tree)?
- 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
What does ToLower() do?
Write an example
it stores all data from an input string in lowercase
string userDay = Console.ReadLine().ToLower();
What does this code do?
if (userDay == “thursday”)
{
Console.WriteLine(“Only one more day to go!!!”);
}//end if
the code in the scope will run ONLY if the condition is TRUE
userDay must equal “thursday”…in lowercase
What does this code do…
** int userStrength = Convert.ToInt32(Console.ReadLine()); **
changes the input from the ReadLine() from a string to an int
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
bool runMe = true;
if (runMe)
{
Console.WriteLine(“\nProgram running…\n”);
}//end if
** bool isAdmin = false;**
Console.WriteLine(“What is your username?”);
string username = Console.ReadLine().ToUpper();
if (username == “TONI” || username == “DUDE”)
{
isAdmin = true;
}//end if
**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
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
What are switches?
another form of branching…not as flexible as IF;
doesn’t do ranges well
but GREAT for EXACT matching;
Also great for MENU options
What is the syntax for switch branching?
**switch (variablename) { case TEST: //code break;**
** default //code break;**
}
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
What does the following mean…
IF YOU SEE THE ERROR CANNOT FALL THROUGH
a break is missing in a switch
How many looping options are there?
Do While
For
For Each
What is a do while loop?
best for when you want to run code an undeterminable number of times but at least once!!!
What’s the syntax for a do while?
COUNTER
do
**{ //code to run UPDATE }while (CONDITION);**
** int cookie = 1;
do
{
Console.WriteLine(“Yum! You have had {0} cookies “, cookie);**
** cookie++;//UPDATE**
** } while (cookie <= 5);**
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
What’s the syntax for a for loop?
for (COUNTER; CONDITION; UPDATE)
**{ //code to run }**
** for (int COOKIE = 1; COOKIE <= 7; COOKIE++)
{
Console.WriteLine(“Yum! You have had “ + COOKIE);**
** }**
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);
}
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"; }
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
foreach loop
decimal[] cartPrices = { 12.99m, 2, 9.99m, 10, 20 };
** foreach (decimal price in cartPrices)
{
Console.WriteLine(“{0:c}”, price);**
** }**
**outputs the each price **
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);**
}
Define a LOOPING WHILE
While is an undeterminable # of times…
best for when you want to run code an indeterminable number of times
What’s the syntax for a LOOPING WHILE
while (CONDITION)
**{ //code to run UPDATE }**
** int cookie = 1;**
while (cookie <= 5)//CONDITION…no SEMICOLON
{
//code to run
Console.WriteLine(“Yum! You have eaten “ + cookie);
** cookie++;**
}
** 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**
When is it most comman to use a while loop?
…with a bool for a counter
as this allows us to conditionally change the bool for when we want to exit the loop
**When is it good to use a For loop versus a While loop? **
For loops are great for a specific number of times to loop
While loops are great for an indeterminable number of times