Decision Making – Branching-C Programming


C Programming – Decision Making – Branching

In this tutorial you will learn about C Programming – Decision Making, Branching, if Statement, The If else construct, Compound Relational tests, Nested if Statement, The ELSE If Ladder, The Switch Statement and The GOTO statement.

Branching

The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the flow of the sequence of instructions. C language provides statements that can alter the flow of a sequence of instructions. These statements are called control statements. These statements help to jump from one part of the program to another. The control transfer may be conditional or unconditional.

if Statement:

The simplest form of the control statement is the If statement. It is very frequently used in decision making and allowing the flow of program execution.

The If structure has the following syntax

if (condition)
statement; 

The statement is any valid C’ language statement and the condition is any valid C’ language expression, frequently logical operators are used in the condition statement. The condition part should not end with a semicolon, since the condition and statement should be put together as a single statement. The command says if the condition is true then perform the following statement or If the condition is fake the computer skips the statement and moves on to the next instruction in the program.

Example program

Sample Code

  • # include <stdio.h> //Include the stdio.h file
  • void main () // start of the program
  • {
  •      int numbers   // declare the variables
  •      printf (“Type a number:”)  // message to the user
  •      scanf (“%d”, &number)  // read the number from standard input
  •      if (number < 0)  // check whether the number is a negative number
  •      number = -number   // if it is negative then convert it into positive
  •      printf (“The absolute value is %d \n”, number)  // print the value
  • }
  • The above program checks the value of the input number to see if it is less than zero. If it is then the following program statement which negates the value of the number is executed. If the value of the number is not less than zero, we do not want to negate it then this statement is automatically skipped. The absolute number is then displayed by the program, and program execution ends.

    The If else construct:

    The syntax of the If else construct is as follows:-

    The if else is actually just on extension of the general format of if statement. If the result of the condition is true, then program statement 1 is executed, otherwise program statement 2 will be executed. If any case either program statement 1 is executed or program statement 2 is executed but not both when writing programs this else statement is so frequently required that almost all programming languages provide a special construct to handle this situation.

    Sample Code

    1. #include <stdio.h>  //include the stdio.h header file in your program
    2.   void main ()   // start of the main
    3.   {
    4.     int num  // declare variable num as integer
    5.     printf (“Enter the number”)  // message to the user
    6.     scanf (“%d”, &num)   // read the input number from keyboard
    7.     if (num < 0)   // check whether number is less than zero
    8.        printf (“The number is negative”)   // if it is less than zero then it is negative
    9.     else  // else statement
    10.        printf (“The number is positive”)  // if it is more than zero then the given number is positive
    11. }
    12.  

     

    In the above program the If statement checks whether the given number is less than 0. If it is less than zero then it is negative therefore the condition becomes true then the statement The number is negative is executed. If the number is not less than zero the If else construct skips the first statement and prints the second statement declaring that the number is positive.

    Compound Relational tests:

    C language provides the mechanisms necessary to perform compound relational tests. A compound relational test is simple one or more simple relational tests joined together by either the logical AND or the logical OR operators. These operators are represented by the character pairs && // respectively. The compound operators can be used to form complex expressions in C.

    Syntax

    a>if (condition1 && condition2 && condition3)
    b> if (condition1 //condition2 // condition3) 

    The syntax in the statement ‘a’ represents a complex if statement which combines different conditions using the and operator in this case if all the conditions are true only then the whole statement is considered to be true. Even if one condition is false the whole if statement is considered to be false.

    The statement ‘b’ uses the logical operator or (//) to group different expression to be checked. In this case if any one of the expression if found to be true the whole expression considered to be true, we can also uses the mixed expressions using logical operators and and or together.

    Nested if Statement

    The if statement may itself contain another if statement is known as nested if statement.

    Syntax:

     if (condition1)
       if (condition2)
      
       statement-1;
       else
          statement-2;
       else
          statement-3;

    The if statement may be nested as deeply as you need to nest it. One block of code will only be executed if two conditions are true. Condition 1 is tested first and then condition 2 is tested. The second if condition is nested in the first. The second if condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement.

    Sample Code

    1. #include <stdio.h> //includes the stdio.h file to your program
    2.    main () //start of main function
    3.    {
    4.      int a,b,c,big //declaration of variables
    5.      printf (“Enter three numbers”) //message to the user
    6.      scanf (“%d %d %d”, &a, &b, &c) //Read variables a,b,c,
    7.      if (a > b) // check whether a is greater than b if true then
    8.      if (a > c) // check whether a is greater than c
    9.         big = a // assign a to big
    10.      else big = c // assign c to big
    11.      else if (b > c) // if the condition (a > b) fails check whether b is greater than c
    12.         big = b // assign b to big
    13.      else big = c // assign c to big
    14.      printf (“Largest of %d, %d & %d = %d”, a,b,c,big) //print the given numbers along with the largest number
    15.    }

     

    In the above program the statement if (a>c) is nested within the if (a>b). If the first If condition if (a>b)

    If (a>b) is true only then the second if statement if (a>b) is executed. If the first if condition is executed to be false then the program control shifts to the statement after corresponding else statement.

    Sample Code

    1. #include <stdio.h> //Includes stdio.h file to your program
    2. void main () // start of the program
    3. {
    4.    int year, rem_4, rem_100, rem_400 // variable declaration
    5.  
    6.    printf (“Enter the year to be tested”) // message for user
    7.    scanf (“%d”, &year) // Read the year from standard input.
    8.  
    9.    rem_4 = year % 4 //find the remainder of year by 4
    10.    rem_100 = year % 100 //find the remainder of year by 100
    11.    rem_400 = year % 400 //find the remainder of year by 400
    12.  
    13.    if ((rem_4 == 0 && rem_100 != 0) rem_400 == 0)  
    14.       //apply if condition 5 check whether remainder is zero
    15.       printf (“It is a leap year. \n”) // print true condition
    16.    else
    17.       printf (“No. It is not a leap year. \n”) //print the false condition
    18. }

    The above program checks whether the given year is a leap year or not. The year given is divided by 4,100 and 400 respectively and its remainder is collected in the variables rem_4, rem_100 and rem_400. A if condition statements checks whether the remainders are zero. If remainder is zero then the year is a leap year. Here either the year – y 400 is to be zero or both the year – 4 and year – by 100 has to be zero, then the year is a leap year.

    Advertisements

    2 responses to “Decision Making – Branching-C Programming

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s