%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
C# conditional branching statement
1.
2. Conditional Branching Statements
Anoop K. C.
anoop@baabte.com
www.facebook.com/anoopbaabte
twitter.com/anoop_baabte
in.linkedin.com/in/anoopbaabte/
+91 9746854752
3. conditional statements allow us to take logical decisions about executing different
block of program to achieve the required logical output.
For example, conditional branching statements allow us to write logic such as, "If
you are over 25 years old, then you may rent a car.“
Here ”If you are over 25 years old” is a conditional statement which evaluates to
true or false
and ”you may rent a car” is the logic which has to be executed if the conditional
expression evaluates to true
There are two types of conditional branching statements in C#
The “if” statement
The “switch” statement
4. The “if” statement
The basic structure of the if statement is as follows
If (conditional statement)
{
//statements to execute
}
The statement inside the braces will execute if and only if the conditional
statement evaluates to true.
if we are using a series of mutually exclusive conditional statements we will use
the “if else” statement
5. The “if else” statement
Consider code snippet 1 and then code snippet 2
Both 1 and 2 does the same thing and output the same thing under identical
inputs
6. The “if else” statement - continued
But in the first code snippet , even when the first statement (number == 1)
evaluates to true, the compiler checks the other statements which is an unwanted
overhead which can affect the performance adversely.
But in the second code, since the first statement (number == 1) evaluates to true
the compiler ignores the next three “if” blocks.
if an expression in an if else statement evaluates to true, the compiler ignores the
following statements in that particular “if else block” and jumps to the line after
the “if else” statement.
We can also add an optional else statement at the end so that if none of the
conditions in the if, else if statements have been met, the code inside the else
block will be executed
We can also use conditional operators (and/or) in if statements in scenarios where
multiple expressions are to be evaluated to execute a code block.
7. The “switch” statement
After each code block a break statement is needed to go out of the switch
statement if the condition has been met and the corresponding code has been
executed
With a switch statement we can replace multiple if else statements.
8. The “switch” statement – contind.
We can also join multiple case statements inside a switch statements as shown in
the following code
9. The “goto” statement
The goto statement is used to shift the control of execution to any part of a
program marked with the label that succeeds the goto keyword.
10. The “goto” statement - continued
In the previous example, if the user enters an invalid entry, the program control
goes to the label marked as “start” and starts to execute the program from there ,
so that the program again prompts the user to enter a number between 1 and 5.
The scope of the goto statement is confined to the function in which it is defined
11. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
12. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com