Control Structures in C language – {Module-4}

Control Structures in C language
Control Structures in C language

Control Structures in C language – {Module-4}

1. If then else – in C language

 

This control structure is used when the programmer has to make some decision while writing a block of code.

Syntax:

If(condition)                                       // This condition is true

{

Block of code;                    // Then this block of code will be executed

}

else                                                        // otherwise

{

Block of code:                    // This block of code will be executed

}

So, Let us start writing some simple program using if then else control structure

We will write a program for determining that whether the student is pass or fail by accepting the marks

Logic:   First, we will declare a variable of int data type. After declaring the value we will accept the value from the user and then store it in that variable. The using the if then else control structure we will determine whether the student is pass or fail. Let’s start writing this simple code.

 

int i;                       // variable declared

printf(“Enter the number”);       // “Enter the number” will be declared on the screen

scanf(“%d”, &i):              // value accepted and stored in variable i

if(i > 40)               // if the condition is true i.e. if the marks stored in i is greater than 40

{

                printf(“Pass”);                  // then pass will be printed on the screen

}

else                       //            If the above condition is not true i.e. if the marks stored in i is less than 40                                                         then the block of code in else section will be executed

{

                printf(“Fail”);

}

 

Nested if then else – in C language

 

If the programmer has to use more than one condition while writing a program the nested if then else control structure is used.

Syntax:

If(condition)       // If this condition is true

{

Block of code;    // Then this block of code will be executed

}

else        //otherwise

{

if(condition)                       // If this condition is true in the else section

{

Block of code;    // Then this block of code will be executed

}

else                        // Otherwise

{

Block of code:    // This block of code will be executed

}

}

Now, we will write the same program written above but with multiple conditions

Logic: The logic will be same but in the nested if then else the first condition will be if the marks is greater than 40 then the student is pass, second condition if the marks are in between 50 and 55 then second class and third condition will be if the marks are greater than 55 then higher second class else the student is fail.

int i;                       // variable declared

printf(“Enter the number”);       // “Enter the number” will be declared on the screen

scanf(“%d”, &i):              // value accepted and stored in variable i

if(i > 40)               // if the condition is true i.e. if the marks stored in i is greater than 40

{

                printf(“Pass”);                  // If the above condition is true then “Pass” will be printed on the                                                                              screen

}

else       // Otherwise the next if condition will be written and executed in this else section

{

                if ( 50 < i < 55)    if the marks are in between 50 and 55 then the block of code in this section                                                       will be executed

                {

                                printf(“second class”); // “second class” will be printed on the screen.

                }

                else       // The next if condition will be written and executed in this else section

                {

                                if( i > 55)              // If the marks are above 55 the higher second class will be displayed

                                {

                                                printf(“Higher second class”);

                                }

                                else // If any of the above mentioned condition is not true the this else section will be                                              executed

                                {

                                                printf(“Fail”);

                                }              // This else section is closed

                }              // previous else section is closed

} // The first else section in closed

 

The concept of if then else and nested if then else will be cleared to you. Its time for you to write your first program.

Problem Statement: Write a program to know whether the year given by the user is a leap year or not.

In this problem statement we will use modulus (%) operator. The modulus operator will return the remainder

For example: 20%5 will return 4 (i.e. the remainder)

Conditions for leap year

Store the year given by user in some variable say x

There are three conditions

  1. if ( x%400 == 0) Then the year is leap year. “==” is the comparison operator. It compares the variable with the value.
  2. if (x%100 == 0) Then the year is a leap year.
  3. if (x%4 == 0) Then the year is leap is year.

else

not a leap year.

 

Hope this logic will be help you to develop the program.

If you have missed our Previous Modules then Read the Modules here by topic Wise:
1)   Basics of C language
2)   Data Types in C language
3)   Input and Output in C language

 

Now you have the knowledge about how to use if then else and nested if then else. For more problems on this control structure refer the book “C” by Dennis Ritchie.

About Suraj Pandita 22 Articles
Studying Computer Engineering from University of Pune.

1 Trackback / Pingback

  1. Different types of Data Structures

Leave a Reply

Your email address will not be published.


*