Decision Statement

If-statement

If-statement
The if statement is used to check a condition and if the condition is true, the statement in the if gets executed, if the condition is false, it does not enter the body of if.

Syntax:-
        if(condition)
         {
          statement 1;
          statement 2;
          ---------------
          statement n;
          }
NOTE:- No need to give parentheses as given in syntax of if, when only one statement is given in the body. Parentheses is given when more than one statement is in the body of if or else.

Example:- 
Write a program to enter candidate age and check if  his/her age is greater than 18.

              #include<stdio.h>
              void main()
                {
                   int age;
                   printf("Enter age of the candidate:");
                   scanf("%d", &age);
                   if(age>18)
                   printf("Age of candidate is greater than 18");
                 }
Here, parantheses is not used after if because only one statement is in the body of if.

If-else:-
The if-else statement is used to carry a logical test and then take one of two possible actions depending on the outcomes of the test. If the condition gets true, the if satement gets executed otherwise the else statement executes.

Syntax:-
       if(condition)
        {
          statement 1;
          statement 2;
          ---------------
          statement n;
         }
         else
          {
             statement 1;
             statement 2;
             ---------------
             statement n;
            }
Example:-
Write a program to enter candidate age and check if  his/her age is greater than 18 and print eligible for voting else print not eligible for voting.

              #include<stdio.h>
              void main()
                {
                   int age;
                   printf("Enter age of the candidate:");
                   scanf("%d", &age);
                   if(age>18)
                   printf("Eligible for voting");
                   else
                   printf("Not eligible for voting");
                  }

       QUESTIONS:-

Nested if:-
Nested if statement means an if statement inside another if statement.
Syntax:-
         if(condition1)
           {
              if(condition2)
                {
                  body;
                 }
                else
                  {
                     body;
                   }
               }
              else
               {
                 body;
                }

   QUESTIONS:-

If-else-if ladder:-
This statement is used when multiple conditions are given. It checks the condition from top to bottom. When it finds the conditions true, it executes that part and the rest part is bypassesd. if none of the conditions are true, then the final else will be executed.
Syntax:-
      if(condition)
        {
           body;
         }
        else if(condition)
          {
             body;
            }
        else if(condition)
          {
             body;
            }
         else
            {   
               body;
              }

      QUESTIONS:-

Switch Case:-
The switch statement allows us to execute one code block among many alternatives. The case which will get true will get executed and if no condition matches then the control goes to the default part.

Keyword break is used to terminate the processing of a particular case within a switch statement. If it is not used then also the program will not display any error but we will understand it through an example.

Syntax:-
       switch(integer expression)
          {
             case constant1:
              do this;
              break;
             case constant2:
              do this;
              break;
             case constant3:
              do this;
              break;
              -----------------
              default:
                do this;
             }


Post a Comment

0 Comments