In: Computer Science
C# Programming
Explain in your own words how using the control selection ( decision) structure is useful in programming in general. Provide Example
Control selection statements
They help in testing the conditions provided by the user
If a particular condition is true then it will execute some set of statements
If condition evaluates to false it will execute some other set of statements
Helps in making decisions based on some set of conditions
Used to control the flow of execution of statements
Some decision making structures are
If
If else
Nested if else
Switch
If statement has some set of Boolean expressions which are followed by some statements
Similarly each has it's own functions and can be used based on the program we are writing
I will execute the code and edit my answer
example of if :
using System; namespace Demo { public class A { public static void Main(string[] args) { int c=7; if(c==7) Console.WriteLine("Value is 7"); } } }
example of else :
using System;
namespace Demo {
public class A {
public static void Main(string[] args)
{
int c=7;
if(c==7)
Console.WriteLine("Value is 7");
else
{
Console.WriteLine("Value is not 7");
}
}
}
}
we can use elseif if we have more than 2conditions
switch example
namespace Constants { public class swi { public static void Main(string[] args) { char grade = 'A'; switch (grade) { case 'A': Console.WriteLine("A grade"); break; case 'B': Console.WriteLine("B grade"); break; default: Console.WriteLine("Failed"); break; } } } }
Thus switch is used.Similarly we can add more cases based on the code.