KEMBAR78
C# conditional branching statement | PPTX
Conditional Branching Statements
Anoop K. C.
anoop@baabte.com
www.facebook.com/anoopbaabte
twitter.com/anoop_baabte
in.linkedin.com/in/anoopbaabte/
+91 9746854752
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
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
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
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.
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.
The โ€œswitchโ€ statement โ€“ contind.
We can also join multiple case statements inside a switch statements as shown in
the following code
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.
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
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
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

C# conditional branching statement

  • 2.
    Conditional Branching Statements AnoopK. C. anoop@baabte.com www.facebook.com/anoopbaabte twitter.com/anoop_baabte in.linkedin.com/in/anoopbaabte/ +91 9746854752
  • 3.
    conditional statements allowus 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 Thebasic 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 Aftereach 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 Thegoto 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