Normally a set of statements in a program is executed sequentially but normally that's not what we want. We might want to to change the order of execution of statements based on certain conditions.
When the branching is based on a particular condition, it is known as conditional branching. If branching takes place without any decision, it is known as unconditional branching.
C# language has the following control or decision-making statements:
if
statementswitch
statement- Conditional operator statement
The if statement can be implemented in different forms depending on the complexity of the conditions to be tested.
-
Simple if Statement: In this program we have used simple if statement to count students that is meeting the criteria
weight < 50 && height > 170
. You may ignore the for loop as we are going to learn about it in the next lesson.One thing that should be noted is that in case of C programming language
a == b
resulted to a integer value i.e 1 or 0 so error such asa = b
was undected by the compiler. But in case of C# the codea == b
results to a boolean value. So error detection is definite.
flowchart TD
Start--Entry--> B{boolean<br/> expression?}
B--False--> D
B--True--> C[Statement<br/>block]
C --> D[Statement-x]
D--nextstatement--> END
- The if..else Statement: If the if expression results to true, then the the first block statement(s) are executed; otherwise, the second block statement(s) are executed. In either case, either first or second block will be executed, not both. In this program either even or odd are found out easily using if..else statement.
flowchart TD
Start--Entry--> B{boolean<br/> expression?}
B--False--> D[False-block<br/> Statements]
B--True--> C[True-block<br/>Statements]
C--> E[Sratement-x]
D--> E[Statement-x]
- Nested if..else Statement: When a series of decision are involved, we may have to use more than if..else statement in nested forms. In the program largest value out of 3 values is found out using the nested if..else statement.
flowchart TD
A( )--Entry--> B{Test<br/> condition1?}
B--False--> D[Statements3]
B--True--> C{Test<br/>Condition2<br/>?}
C--True--> E[Sratement-1]
C--False--> G[Sratement-2]
E-->H(( ))
G-->H
D-->H
H--> F[Statement-x]
F--Next Statement--> I( )
- The else..if Ladder: There is another way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if. In this program we are grading students based on some rules. Please follow the comments in the code for better understandng of the grading scheme.
We can use if statements to make a selection scheme. But, the complexity of such a program increases dramatically when the alternative icrease. The program becomes difficult to read and follow. That's where switch statements are usefull. The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, the statements that are associated with it are executed.
The expression must be an integer type or char or string type.
-
Using switch..case: The same program that was solved using the else..if ladder is solved using the switch..case statements. It can be seen that the scheme simplifies the program a lot.
A cloth showroom has announced the following seasonal discounts on purchase of items:
PURCHASE AMOUNT | DISCOUNT | |
---|---|---|
MILL CLOTH | HANDLOOM ITEMS | |
0 - 100 | - | 5.0% |
101 - 200 | 5.0% | 7.5% |
201 - 300 | 7.5% | 10.0% |
Above 300 | 10.0% | 15.0% |
Write a program using switch and if statements to compute the net amount to be paid by a customer