Clear and Unclear Windows 1
Branching Statements
The if / else if / else branching statement if ( bool-expression ) { statement(s) } else
The if / else if / else branching statement if ( bool-expression1 ) { statement(s) } else if (bool-expression2 ) else
Your task… New project named branches Write a quick and simple calculator program: Display the message “If statement calculator…” Ask the user for a number (save it) Ask the user for a 2nd number (save it) Ask the user what math operation to do ( +, -, *, / ) Provide an error message if the user doesn’t enter a valid operator Use an if /else if /else to display the answer
The switch branching statement switch ( variable ) { case <option 1>: <code> break; case <option 2>: default: }
The switch branching statement switch ( variable ) { case <option 1>: <code> break; case <option 2>: default: } switch ( userName) { case “Korben Dallas”: Console.WriteLine(“LeeLoo loves you”); break; case “Zorg”: Console.WriteLine(“LeeLoo will destroy you”); default: Console.WriteLine(“Be careful!”); }
Your task continued… Create a new project named switch: Do the same thing again, write a simple calculator program Use a switch instead of an if Display the message “Switch statement calculator…”
Clear and Unclear Windows