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 (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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