Sunday 1 April 2018

Control Structure in C

The decision control structure in C can be implemented using
 1. The if Statement 
 2. The if - else Statement
 3. The Nested if - else Statement

The if Statement

  The general form of it statement looks like this :
   
     if(this condition is true)
            execute this statement;

The if statement by itself will execute a single statement or a group of statement when the condition following if is true.
  
The simple example of a if statement is :
    if(varName = = 20)
      printf("Value of the variable is 20");

We can use the block to specify the statement to pre executed if the given condition is true.
 if(varName = = 20)
 {
    printf("Value of the variable is 20");
    printf("Print what ever you want !!!");
 }

The if - else Statement

  The if statement by itself will execute a single statement or a group of statements when the condition following if is true. It does nothing when the condition is false. If the condition is false then a group of statements can be executed using else statement.
The following program illustrates this
 /* Calculation of gross salary */
   main( )
   {
    float bs, gs, da, hra;
    printf("Enter basic salary");
    scanf("%f", &bs);
    if(bs<1500)
    { 
          hra = bs * 10/100;
          da = bs * 90/100;
     }
    else
     {
           hra = 500;
           da = bs * 98/100;
      }
      gs = bs+hra+da;
      printf("gross salary = Rs. /.f" , gs);
     }

The Nested if - else Statement

   It we write an entire if - else construct within the body of the if statement or the body of an else statement. This is called nesting of if . For example

    if(condition)
    {
         if(condition)
          {
             do this;
          }
          else
          {
            do this;
            and this;
          }
         else
             do this;
    }

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (741) Python Coding Challenge (192) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses