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