Conditional Statements Flashcards

1
Q

Explain the three different conditional statements and their differences

A

C# has three conditional structures: if statements, if…else, and switch. An if statement performs an action if a condition is true. An if…else statement performs an action if a condition is true or another action is the condition is false. A switch statement performs one of many different actions depending on the value of the expression.

Similarities: 1) They all control the flow of the program, and 2) Used to evaluate conditions.

Differences:
A if selects or ignores a single action, an if…else selects between two actions, and a switch selects between multiple actions.

An if or if…else statement can use logical operators , which switch cannot.

In general, a switch is preferable when you check for many values. It provides a clearer code than an if…else.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a method that finds the larger number

A

int num1=5;
int num2=10;

if(num1<num2){
Console.WriteLine($”{num1} is less than {num2}”);
} else {
Console.WriteLine($”{num1} is greater than {num2}”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the three different conditional statements with examples

A

An if statement performs (selects) an action if a condition is true. It consists of a condition enclosed in (…). The condition is a boolean expression, which is evaluated to be true or false. If true, the code block inside the {…} is executed, if false, it is skipped.

Example:
int num=5;
if(num < 100) {
Console.WriteLine(“less than 100”);
}

An if…else statement perform an action is a condition is true or a different action if the condition is false. It consists of a condition enclosed in (…), which is a boolean expression that is evaluated to be true or false. If true, the code block inside the {…} is executed. If false, the code block inside the second set of {…} is executed.

Example:
int temperature=15;
if(temperature < 15) {
Console.WriteLine(“It is cold today”);
} else {
Console.WriteLine(“It is warm today”);
}

A switch statement compares the value of an expression against the values of multiple cases, searching for a match. When a match is found, the code block inside that case is executed, and the following break statement terminates the switch. If no match is found, the code block inside the default case is executed. In C#, if you forget a break statement, the control will “fall through” to the next case(s). This means that after executing the code for a matched case, it will continue executing the code for the following cases until it reaches a break statement or until the end is reached.

Example:
string employeeType=””;
int salary=20000;
double totalSalary=0;

switch(employeeType){
case “FullTime”:
totalSalary= salary+(salary*0.1);
break;

case “PartTime”:
totalSalary= salary+(salary*0.05);
break;

default:
totalSalary=salary;
break;
}
Console.WriteLine($”Salary with bonus is {totalSalary}”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly