C#: Conditionals & Control Flow

Sometimes we'll want our programs to respond differently depending on user's choice. Control flow is a way of telling our program which statements
or instructions to perform.It's just a way of teaching our programs how to make decisions.Check out the example given below and see what happens when you run the code.

using System;
​class prg
{
 public static void Main(String []args)
 {
        int age=5;
        if(age<18) {
                Console.WriteLine("Sorry you cannot Vote!! ");
        }

        else {
                Console.WriteLine("You are eligible to Vote!!");
        }
 }
}
Output - Sorry you cannot Vote!!

Did you see that we set our integer variable "age=5", which is less than 18. So, when we checked our condition of voting, the appropriate message was printed!!

First things first: we need to learn how to compare numbers. We'll start by looking at the main comparison operators in C#, also known as Relational Operators. We all know that 5 is less than 18, but how do we express that?


The main comparisons we can make are:

<, less than (e.g., 6 < 7)
> greater than (e.g, 7 > 6)
== equal to (e.g., 2 == 2)
<= less than or equal to (e.g, 2 <= 3)
>= greater than or equal to (e.g., 3 >= 2)
!= not equal to (e.g., 6 != 3)

Before understanding further let us understand few terms

What is Operator?

These are some special symbols with universal meaning and they perform some predefined operations on values and these values can be variables or constants and operations can be mathematical, statistical or logical. In example given below + , - , = & ! are operators.

Example - 

a + b - c = z , a < b , a!=c

What is Operand?

Operands are values on which operators work. It can be variable or constants. In example given above a,b,c and z are operands.

What is Expression?

An expression is a combination of operators and operand.
Expression = Operands + Operators
Expression = a+b+c = z

Understanding Conditionals


Conditional statements are used to perform different actions based on conditions. Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. Just like other programming languages in C# also we have following conditional statements.


  • if statement - executes some code only if a specified condition is true
  • if...else statement - executes some code if a condition is true and another code if the condition is false
  • if...elseif....else statement - selects one of several blocks of code to be executed
  • switch statement - selects one of many blocks of code to be executed
Now, lets understand each of them one by one.

The if statement

The if statement is used to execute some code only if a specified condition is true. With an if statement, we're trying to find something that's true. If the thing we're testing is true, then we'll continue executing this section of the code.

int age=10;
        if(age<18) {
                Console.WriteLine("Sorry you cannot Vote!! ");
        }

Usually, you'll have a value and you'll want to do something because of it. For example, you are buying something from flipkart.com, and want to give everyone who buys item more than Rs. 500 a free home delivery facility. Here the if statement comes in.

Syntax


if(condition) {
               // code to be executed if condition is true;
        }


The if...else statement

So far, we've only been thinking about finding if something is true. But, sometimes, we'll want to take action if something is not true. else is your friend here! Use the if....else statement to execute some code if a condition is true and another code if the condition is false. It takes care of running code if the thing you're testing turns out to be false:

int bal=695;
        if(bal>500) {
                Console.WriteLine("Free home delivery!!");
        }

        else {
         Console.WriteLine("No Free home delivery!!");
        }

Syntax


if(condition) {
                // code to be executed if condition is true;
        }

        else {
         // code to be executed if condition is false;
        }

The if...elseif....else Statement

In the if....elseif...else statement several blocks of code is executed. For instance, take an example of if...else statement, suppose we have a shop in which we give big discount for people who buy more than 5 items and a small discount to those who buy less than 5 items. But then I think, "Hey, I don't want to give a discount to people buying just one item." What do we do now? This is where if...elseif...else statement comes in. elseif does just what it sounds like: it tacks an extra check onto your if/else statement.

int items=12;
        if(items>5) {
                Console.WriteLine("You get 10% discount!!");
        }

        else if(items==1) {
         Console.WriteLine("Sorry, no discount!");
        }

        else {
         Console.WriteLine("You get 5% discount!!");
        }

Syntax


if(condition) {
                // code to be executed if condition is true;
        }

else if(condition) {
         // code to be executed if condition is true;
        }

else {
         //code to be executed if condition is false;
        }

For practice visit following tutorials




Great work!! So far we have discussed if statements, if...else statements and also if...elseif...else statements. I think this is enough for today, in our next session we learn about the switch statements, also we'll learn about operators and its various types like relational operators, mathematical operators and logical operators in our next session.

If you like this post then don't forget to like our facebook and google plus fan pages for updates. If you like Kodestat then don't forget to mention us among your frinds. For complains, questions and your feedbacks mention us in comments. Thank You!!
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment