If, Else if and Else this three are used for check condition if condition will true then execute the statement otherwise next condition. ...

If ,Else if and Else condition

/
0 Comments
  • If, Else if and Else this three are used for check condition if condition will true then execute the statement otherwise next condition.
What is if?

  • If life were a straight line, it would be very boring. The same thing is true for programming. It would be too dull if the statements in your program could only be executed in the order in which they appear.
  • In fact, an important task of a program is to instruct the computer to branch (that is, jump) to different portions of the code and work on different jobs whenever the specified conditions are met.
  • However, in most cases, you don't know in advance what will come next. What you do know is that something is bound to happen if certain conditions are met. Therefore, you can just write down tasks and conditions in the program. The decisions of when to perform the tasks are made by the conditional branching statements. 
  • where a statement may consist of a single statement, a block of statements, or nothing (in the case of empty statements). The else clause is optional.

The if-else Statement 

  • In the if statement, when the conditional expression is logical TRUE, the computer will jump to the statements controlled by the if statement and execute them right away. If the expression is false, the computer will ignore those statements controlled by the if statement.
  • From time to time, you will want the computer to execute some other specified statements when the conditional expression of the if statement is logical FALSE. By doing so, you can use another conditional branching statement in C—the if-else statement.

The syntax of if, else if, and else is
 void main() {
  if(condition 1)  when 1 condition true starting of if else statement start with if condition
  {
   statement;
   }

else // when on condition true and end with else condition
  {
   statement;
   }

Nested ifs

  • A nested if is an if that is the target of another if or else. Nested ifs are very common in programming. In a nested if, an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. For example:
           if(i) {
           if(j) dosomething1();
           if(k) dosomething2(); /* this if */ 

           else dosomething3(); /* is associated with this else */ 
          } 
         else dosomething4(); /* associated with if(i) */
  • As noted, the final else is not associated with if(j) because it is not in the same block. Rather, the final else is associated with if(i). Also, the inner else is associated with if(k), which is the nearest if
  • You can use a nested if to further improve the magic number program by providing the player with feedback about a wrong guess.
  • The conditions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the conditions are true, the final else is executed. That is, if all other conditional tests fail, the last else statement is performed. If the final else is not present, no action takes place if all other conditions are false. 


The if-else if-else 

  • A common programming construct is the if-else-if ladder, sometimes called the if-else-if staircase because of its appearance. Its general form is 
           if (expression)
           statement; 

           else if (expression)
           statement; 
           else if (expression) statement;
           .
           .
           . 
           else statement;

  • The conditions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the conditions are true, the final else is executed. That is, if all other conditional tests fail, the last else statement is performed. If the final else is not present, no action takes place if all other conditions are false. 
  • Although the indentation of the preceding if-else-if ladder is technically correct, it can lead to overly deep indentation. 
Condition Operator:

  • == (double equal to) it uses check both are same
  • !=  (Not equal to) it is used to check both are not equal
  • <= (less than equal to) it is used to  less than or not
  • >= (more than equal to) it is used to more than or not
  • > more then
  • < less then
  • && it is used to check both conditions is true
  • ||  it is used to check anyone  conditions is true

Program :

#include<stdio.h>
#include<conio.h>
void main(){
int a;

printf("\n enter your marks");
scanf("%d"&a);
if(a<34)/*1st condition check */
{
printf("very poor");
}
else if(a>33 && a<70)/*2st condition check */
{
printf("\n good");
}


else if(a>70 && a<101)/*3st condition check */
{
printf("\n good");
}
else /*when no condition is true */
{
printf("\n good");
}
getch();
}

Output:



ForAny Kind of query, doubt And Error live message in cybersquad App . You will get full-time support.I will reply back within an hour.

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts