Hello Everyone!
Welcome to my 4th post. First of all, I should thank my
parents, sister and my friends for supporting me and encouraging me.
In the previous post I covered topics like constants, variables
and data-types etc.
Basics of Programming in C (part-2):
In this post I am going to explain about Conditional statements. This post will take less than 5 minutes to completely read and understand it.
Understanding Conditional Statements
In order to understand conditional statements, first we need to
understand conditions.
In C, conditions are logical statements whose final value will be
either true(it is generally represented as integer 1 or any non-zero integer )
or false(it is represented as 0)
In logical statements, we need to use logical operators.
Some of the logical operators are:
·
== (if
L.H.S and R.H.S are equal then this operator returns true(1) else returns false(0))
Example: (2+1)==3 returns
true and 2==3 returns false
·
< (if
L.H.S is less than R.H.S then this operator returns true(1) else returns false(0))
Example: 2<3 returns
true and 5<4 returns false
·
> (if
L.H.S greater than R.H.S then this operator returns true(1) else returns false(0))
Example: 3>2 returns
true and 2>8 returns false
·
<= (if
L.H.S less than or equals to R.H.S then this operator returns true(1) else
returns false(0))
Example: 2<=3 returns
true and 4<=3 returns false
·
>= (if
L.H.S greater than or equals to R.H.S then this operator returns true(1) else
returns false(0))
Example: 4>=3 returns
true and 6>=10 returns false
·
!= (if
L.H.S and R.H.S are not equal then this operator returns true(1) else returns false(0))
Example: 2!=3 returns true
and (2+4)!=(3+3) returns false
In C, conditional statements are implemented through if-else statements.
In C, conditional statements are implemented through if-else statements.
Syntax :
if(condition-A)
{
Statements-A;
}
else if(Condition-B)
{
Statements-B;
}
else
{
Statements-C;
We use conditional statements to direct the direction of flow of
control of our program into respective parts of the program by using some
respective conditions.
When the flow of control reaches ‘if - else’ block, first it will check the condition that
is present inside the parenthesis of ‘if’ block. If the condition evaluates to
be true then the flow of control will go inside the ‘if’ block and execute the
statements in it. After that flow of control skips all the else-if blocks and
final else block and continues with the rest of the program.
If the ‘if ’condition evaluates to be false then compiler will
check the next else-if block condition. If else-if block condition is true then
those statements present in else-if block will be executed. If the else-if
condition is false then compiler will check for the next else-if condition if
it is present.
When ‘if’ condition and all the else-if conditions are false then
the statements inside the ‘else’ block are executed.
Note: we can use multiple 'else-if' blocks corresponding to one 'if' block but we can use only one 'else' block corresponding to one 'if' block. Using 'else-if' and 'else' blocks without any preceding 'if' block is an error. We can also use
nested if-else block.
Practice Question: write a program to print whether a given number is even or odd using if-else statements.
comment your solution in the comment section below.
That’s all for this post. In the next post we’ll discuss about Switch case statements.
comment your solution in the comment section below.
That’s all for this post. In the next post we’ll discuss about Switch case statements.
Share it with your co-geeks and help me to
improve by giving feedback in the comments section below.
Thank you.
Thanks to Binshumesh Sachan for helping me in editing.
Thanks alot to Nikhitha, Bhavani, Sadhana, Kailash varma, Devendhar, Manoj, Rakesh, Karthik, Koushik, Soma Sekhar, Saqlain, Jaswanth and all other who are sharing these posts with their Co-geeks. Once again thank you guys.
MYSELF SUJITH SAGAR. I AM A B.tech SECOND YEAR COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.
Linkedin profile: www.linkedin.com/in/sujith-sagar-gandi-2b3544179

No comments:
Post a Comment