Conditional Branching Statements
Anoop K. C.
anoop@baabte.com
www.facebook.com/anoopbaabte
twitter.com/anoop_baabte
in.lin...
conditional statements allow us to take logical decisions about executing different
block of program to achieve the requir...
The “if” statement
The basic structure of the if statement is as follows
If (conditional statement)
{
//statements to exec...
The “if else” statement
Consider code snippet 1 and then code snippet 2
Both 1 and 2 does the same thing and output the sa...
The “if else” statement - continued
But in the first code snippet , even when the first statement (number == 1)
evaluates ...
The “switch” statement
After each code block a break statement is needed to go out of the switch
statement if the conditio...
The “switch” statement – contind.
We can also join multiple case statements inside a switch statements as shown in
the fol...
The “goto” statement
The goto statement is used to shift the control of execution to any part of a
program marked with the...
The “goto” statement - continued
In the previous example, if the user enters an invalid entry, the program control
goes to...
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ s...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, N...
C# conditional branching statement
Upcoming SlideShare
Loading in …5
×

C# conditional branching statement

615 views

Published on

This slide is about conditional branching statements in C#

Published in: Software, Technology, Travel
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
615
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

C# conditional branching statement

  1. 1. Conditional Branching Statements Anoop K. C. anoop@baabte.com www.facebook.com/anoopbaabte twitter.com/anoop_baabte in.linkedin.com/in/anoopbaabte/ +91 9746854752
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. 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.
  6. 6. 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.
  7. 7. The “switch” statement – contind. We can also join multiple case statements inside a switch statements as shown in the following code
  8. 8. 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.
  9. 9. 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
  10. 10. 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
  11. 11. 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

×