Condition Statements | C-Sharp Programming
As we lead our life we have to take decisions. Similarly, as we make progress in C-Sharp programming and try to implement complicated logics, our program has to take decisions but how? we will see in this post,
Message Box asking if User want to Exit or Not | C-Sharp Code.
C-Sharp Introduction | Data types of C#
Some of our real life problems depend upon some conditions like if the highway is busy, I would take diversion. If you join our whatsapp group, I would send you some interesting post etc. Same as, we have to make our program a decision taker, a decision control instruction can be implement in C using :
- The if statement.
- The if-else statement.
- The switch Statement.
- The Conditional Operator.
we will discuss in this post only if and if-else statement, remaining two condition instruction will be discussed later.
THE IF STATEMENT:
C-sharp uses the keyword if to implement the decision control instruction. The general syntax of if statement is :
if(condition)
statement;
The keyword tells the compiler that what follows a decision control instruction. The condition is following the keyword is always enclosed within a pair of parenthesis. If the condition within the parenthesis is true the next statement is executed other wise it will skip the next statement and and goes to the next line. We can write the conditions using "Relational Operators", that is used to compare the two values greater, equal, greater than or less than etc. Following are expression of Relational operators.
x==y x is equal to y
x<=y x is less than and equal to y
x>=y x is greater than and equal to y.
x<y x is less than y
x>y x is greater than y
x!=y x is not equal to y
FLOW CHART OF if STATEMENT:
Now, lets do a simple problem:
Problem: While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000, if quantity and price per items are input through the keyboard, write a program to calculate total expenses.
Code:
Console.WriteLine("Input quantity");
int quantity=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Input Price of per items");
double price=Convert.ToDouble(Console.ReadLine());
double dis=0;
if(quantity>1000)
dis=0.1;
double total;
total=(quantity*price)-(quantity*rate*0.1);
Console.WriteLine(total);
The highlighted and italic part is the decision condition instruction and shows that if the quantity is more than 1000 then the discount is 10%/100=0.1 if the condition is true then the value of discount is 0.1 and if the condition false then the value of discount is 0, its is our simple program by using Decision condition instruction.
here are some examples of if.
if(x>10)
x=x+8;
if(10%2==0)
Console.WriteLine("Even number");
if(3+2%5!=2)
Console.WriteLine("This works");
here all the example is with relational operator now let see a example in which there is no relational operator
if(-5)
Console.WriteLine("Hello World");
in this example there is no relational operator, so how can we check that is it true or false ?, the answer is it is true, because in C-sharp programming the non- zero's is referred as true and 0 is referred as false so, the condition is true.
IF ELSE STATEMENT:
The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true, it does nothing when the expression evaluates false. we can execute one or group of statement if the expression evaluates to true, and another group of statement if the expression evaluates to false, this is the purpose of else statement.
FLOW CHART OF if-else STATEMENT:
Lets see a example:
Problem: if the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.
Code:
int ageOfRam,ageofShyaam,agrOfAjay;
Console.WriteLine("Enter Age of Ram");
ageOfRam=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Age of Shyaam");
ageOfShyaam=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Age of Ajay");
ageOfAjay=Convert.ToInt32(Console.ReadLine());
if(ageOfRam>ageOfShyaam && ageOfRam>ageOfAjay)
Console.ReadLine("Ram is Youngest");
else if(ageOfShyaam>ageOfRam && ageOfShyaam>ageOfAjay)
Console.ReadLine("Shyam is Youngest");
else if(ageOfAjay>ageOfShyaam && ageOfAjay>ageOfRam)
Console.ReadLine("Ajay is Youngest");
&& is the conditional operator "AND" if the age of ram is greater than age of shyaam and the age of Ajay then the output is Ram is Youngest , else if the age of Shyaam is greater than the age of Ram and Ajay then the output is Shyaam is youngest and else if the age of Ajay is greater than the age of shyaam and age of Ram then the output is Ajay is youngest is, here you can see that if the first condition is false then the compiler check for second condition if it is also false then compiler check for third condition if it is true then the statement is executed.
I hope You like it, Thank You
Like our facebook page Developer planet.
Condition Statements | C-Sharp Programming
Reviewed by Developer Planet
on
October 29, 2018
Rating:
Condition Statements >>>>> Download Now
ReplyDelete>>>>> Download Full
Condition Statements >>>>> Download LINK
>>>>> Download Now
Condition Statements >>>>> Download Full
>>>>> Download LINK