Saturday, July 30, 2016

using if statement in c

If statement:

if statement allows us to interrupt the flow of the program. The sequence of the program will be changed when we use if statement at that function. If statement is mainly used to take the decisions in the function. 
Now let us examine about the TRUE and FALSE terminology in programming. If a statement evaluates to a Non Zero number then it is said to be a TRUE statement. similarly if a statement evaluates to Zero number then it is said to be FALSE statement. 

Relational operators:

Relational operators are those which can be used for comparing two elements. Here are some examples for relational operators:

less than < 
greater than >
less than equal to <=
greater than equal to >=
equal to ==
not equal to !=

Basic if syntax:

if(condition is true)
  execute the statement;
if (condition is false)
   ignore this statement;


here is a small example of if statement

if(4<5)
{
   printf("This line of code will be executed as four is less than five is true");
}

Else statement:

Sometimes we need to execute some other lines of code if the statement is not true. If the if statement is false else function is executed.

if-else syntax:

if(the condition is true)
{
 Execute this line of code;
}
else
{
  This line of code will be executed if condition is false;
}

Else-if statement:

Some times there is a necessity to use multiple conditions in which all may all evaluate true yet you want only one if statement to be executed true.

Basic syntax:

int main()
{
  if(condition is true)
   execute this code
else if(this comdition is true)
        execute this code;
}






No comments:

Post a Comment