Saturday, 22 December 2018

Control Statements , Simple if statement


Control Statements :
   A program is not a linear sequence of instructions. According to the logic , it may diverge, repeat code or take decisions . The structure of a program which decides the order of program statements execution is called “ Control Structure or Control Statements ”.



Branching :
                         It is called as decision-making statement. These are used when a condition arises in a statement. The various branching statements are :
1.      if statement
2.      switch statement
3.      conditional control statement
1. if statement :
                                The “ if “ statement is a decision making statement which can handle conditions and a group of statements. These are categorized into 4 types. They are
1.      simple if statement
2.      if-else statement
3.      nested if statement
4.      else-if or ladder if statement
1. Simple if statement :
                                When only one condition occurs in a statement , then simple “if” statement is used. The flow chart and the syntax of “ if “ statement are shown below :

Here first of all condition will be checked. If the condition is true, then the statement-1 block will be executed and after execution of this block, statement-2 will be executed. If the condition is false then the statement-2 will be executed directly.
Ex:          #include<stdio.h>
                #include<conio.h>
                void main()
                {
                     float per;
                     char res[5];
                     clrscr();
                     printf(“Enter the percentage:”);
                     scanf(“%f”,&per);
                     if(per>=35)
                     {
                         printf(“\n Result is pass”);
                     }
                     printf(“\n Entered percentage is  %s”,per);
                     getch();
                }

No comments:

Post a Comment