Docsity

Comparing If-Else and Switch Case Statements in C# Programming, Exercises of Object Oriented Programming

Toggle sidebar
bg1
Ogana, Alexis Ann M.
BSCPE 1-1
OOP
Using System;
Namespace MyApplication
{
Class Program
{
Static void Main(string[] args)
{
Console.WriteLine(“~~YHAMM’S FRESH DRIED FISH~~”);
Console.WriteLine(“ “);
Console.WriteLine(“Free Delivery around South City Homes”);
Console.WriteLine(“ “);
Console.WriteLine(“A1 Boneless Danggit => 1300 (per 1kg)”);
Console.WriteLine(“A2 Sweet Pusit => 1300 (per 1kg)”);
Console.WriteLine(“A3 Daing Salay => 400 (per 1kg)”);
Console.WriteLine(“A4 Dalagang Bukid => 400 (per 1kg)”);
Console.WriteLine(“A5 Lupoy => 400 (per 1kg)”);
Console.WriteLine(“ “);
Console.WriteLine(“What is your order? “);
String o = Console.ReadLine();
Switch (o)
pf3
pf4
pf5

Partial preview of the text

Download Comparing If-Else and Switch Case Statements in C# Programming and more Exercises Object Oriented Programming in PDF only on Docsity!

Ogana, Alexis Ann M. BSCPE 1- OOP Using System; Namespace MyApplication { Class Program { Static void Main(string[] args) { Console.WriteLine(“YHAMM’S FRESH DRIED FISH”); Console.WriteLine(“ “); Console.WriteLine(“Free Delivery around South City Homes”); Console.WriteLine(“ “); Console.WriteLine(“A1 Boneless Danggit => 1300 (per 1kg)”); Console.WriteLine(“A2 Sweet Pusit => 1300 (per 1kg)”); Console.WriteLine(“A3 Daing Salay => 400 (per 1kg)”); Console.WriteLine(“A4 Dalagang Bukid => 400 (per 1kg)”); Console.WriteLine(“A5 Lupoy => 400 (per 1kg)”); Console.WriteLine(“ “); Console.WriteLine(“What is your order? “); String o = Console.ReadLine(); Switch (o)

Case “A1”: Console.WriteLine(“Your order is A1 Boneless Danggit “); Console.WriteLine(“Your money:”); String a = Console.ReadLine(); If (float.Parse(a) < 1300) { Console.WriteLine(“Money is not sufficient”); } Else { Console.WriteLine(float.Parse(a)-1300+” Change”); Console.WriteLine(“Thank You for supporting our small business!”); } Break; Case “A2”: Console.WriteLine(“Your order is A2 Sweet Pusit”); Console.WriteLine(“Your money:”); String b = Console.ReadLine(); If (float.Parse(b) < 1300) { Console.WriteLine(“Money is not sufficient”); } Else { Console.WriteLine(float.Parse(b)-1300+” Change”); Console.WriteLine(“Thank You for supporting our small business!”); } Break; Case “A3”:

If (float.Parse(e) < 400) { Console.WriteLine(“Money is not sufficient”); } Else { Console.WriteLine(float.Parse(e)-400+” Change”); Console.WriteLine(“Thank You for supporting our small business!”); } Break; Default: Console.WriteLine(“”); Break; } } } }

As a newbie in programming I always get confused about which conditional statement should I use between if-else and switch case statements. After examining the two statements, I found if-else to be easier to comprehend because it appears to be more straightforward to use. You can also use numerous logical operations in the if-else scenario, which you can’t do in the switch case. Switch case condition needs to be constant and you can’t have multiple objects to compare either unlike in if-else you can put a bundle of things in there.