Computer Programming - Decision Statements

Decision making is critical to computer programming. There will be many situations when you will be given two or more options and you will have to select an option based on the given conditions. For example, we want to print a remark about a student based on secured marks and following is the situation:
1.    Assume given marks are x for a student

2.    If given marks are more than 95 then
3.    Student is brilliant

4.    If given marks are less than 30 then 
5.    Student is poor

6.    If given marks are less than 95 and more than 30 then
7.    Student is average 
Now, question is how to write programming code to handle such situation. Almost all the programming languages provide conditional i.e., decision making statements which work based on the following flow diagram:
Decision making statements in C Let's write a C program with the help of if conditional statements to convert above given situation into programming code:
#include <stdio.h>

main()
{
   int x = 45;
   
   if( x > 95)
   {
      printf( "Student is brilliant\n");
   }
   
   if( x < 30)
   {
      printf( "Student is poor\n");
   }
   
   if( x < 95 && x > 30 )
   {
      printf( "Student is average\n");
   }
}
When above program is executed, it produces the following result:
Student is average
Above program makes use of if conditional statements. Here, first if statement checks whether given condition i.e., variable x is greater than 95 or not and if it finds condition is true, then the conditional body is entered to execute given statements. Here we have only one printf() statement to print a remark about the student.
Similar way, second if statement works. Finally, third if statement is executed, here we have following two conditions:
  • First condition is x > 95
  • Second condition is x < 30
Computer evaluates both the given conditions and then overall result is combined with the help of binary operator &&. If final result is true then conditional statement will be executed, otherwise no statement will be executed.
This tutorial will give you basic idea on various forms of if statements and an introduction of switch statements available in C programming language. Different programming languages provide different type of decision making statements but basic concept will remain same as explained in this tutorial.

if...else statement

An if statement can be followed by an optional else statement, which executes when the boolean expression is false. The syntax of an if...else statement in C programming language is:
if(boolean_expression)
{
   /* Statement(s) will execute if the boolean expression is true */
}
else
{
  /* Statement(s) will execute if the boolean expression is false */
}
Above syntax can be represented in the form of a flow diagram as shown below:
C if...else statement An if...else statement is useful when we have to take a decision out of two options. For example, if student secures more marks than 95, then student is brilliant otherwise no, such situation can be coded as follows:
#include <stdio.h>

main()
{
   int x = 45;
   
   if( x > 95)
   {
      printf( "Student is brilliant\n");
   }
   else
   {
      printf( "Student is not brilliant\n");
   }
}
When above program is executed, it produces the following result:
Student is not brilliant

if...elseif...else statement

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
When using if , else if , else statements, there are few points to keep in mind:
  • An if can have zero or one else's and it must come after any else if's.
  • An if can have zero to many else if's and they must come before the else.
  • Once an else if succeeds, none of the remaining else if's or else's will be tested.
The syntax of an if...else if...else statement in C programming language is:
if(boolean_expression 1)
{
   /* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
   /* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
   /* Executes when the boolean expression 3 is true */
}
else 
{
   /* Executes when the none of the above condition is true */
}
Now with the help of if...elseif...else statement, very first program can be coded as follows:
#include <stdio.h>

main()
{
   int x = 45;
   
   if( x > 95)
   {
      printf( "Student is brilliant\n");
   }
   else if( x < 30)
   {
      printf( "Student is poor\n");
   }
   else if( x < 95 && x > 30 )
   {
      printf( "Student is average\n");
   }
}
When above program is executed, it produces the following result:
Student is average

The switch statement

A switch statement is an alternative of if statements which allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. This has following syntax:
switch(expression){
    case ONE  :
       statement(s);
       break;
    case TWO:
       statement(s);
       break;
     ......
   
    default :
       statement(s);
}
The expression used in a switch statement must give an integer value, which will be compared for equality with different cases given. Wherever, expression value matches with case value, the body of that case will be executed and finally switch will be terminated using break statement. If break statement is not provided, then computer continues executing other statements available below to the matched case. If none of the cases matches, then default case body is executed.
Above syntax can be represented in the form of a flow diagram as shown below:
switch statement in C Now, let's consider another example where we want to write equivalent English word for the given number. Then, it can be coded as follows:
#include <stdio.h>

main()
{
   int x = 2;
   
   switch( x ){
       case 1 :
          printf( "One\n");
          break;
       case 2 :
          printf( "Two\n");
          break;
       case 3 :
          printf( "Three\n");
          break;
       case 4 :
          printf( "Four\n");
          break;
        default :
          printf( "None of the above...\n");
   }
}
When above program is executed, it produces the following result:
Two

Decisions in Java

Following is the equivalent program written in Java programming language. Java programming language also provides if, if...else, if...elseif...else and switch statements.
You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.
public class DemoJava
{    
   public static void main(String []args) 
   {
   
      int x = 45;
   
      if( x > 95)
      {
         System.out.println( "Student is brilliant");
      }
      else if( x < 30)
      {
         System.out.println( "Student is poor");
      }
      else if( x < 95 && x > 30 )
      {
         System.out.println( "Student is average");
      }
    
   }
}

Decisions in Python

Following is the equivalent program written in Python. Python provides if, if...else, if...elif...else and switch statements. Here, you must note that Python programming does not make use of curly braces for conditional body, instead it simply identifies the body of the block using indentation of the statements.
You can try to execute following program to see the output:
x = 45

if x > 95:
   print "Student is brilliant"
elif x < 30:
   print "Student is poor"
elif x < 95 and x > 30:
   print "Student is average"

print "The end"
When above program is executed, it produces the following result:
Student is average
The end 
Let's consider a situation when you want to write Hello, World! five times. Here is a simple C program to do the same:
#include <stdio.h> main() { printf( "Hello, World!\n"); printf( "Hello, World!\n"); printf( "Hello, World!\n"); printf( "Hello, World!\n"); printf( "Hello, World!\n"); } When above program is executed, it produces the following result:
Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! It was simple, but again let's consider another situation when you want to write Hello, World! thousand times, what you will do in such situation? Are we going to write printf() statement thousand times? No, not at all. Almost all the programming languages provide a concept called loop, which helps in executing one or more statements up to desired number of times. All high-level programming languages provide various forms of loops, which can be used to execute one or more statements repeatedly.
Let's write above C program with the help of a while loop and later we will discuss how does this loop work:
#include <stdio.h> main() { int i = 0; while ( i < 5 ) { printf( "Hello, World!\n"); i = i + 1; } } When above program is executed, it produces the following result:
Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Above program makes use of while loop, which is being used to execute a set of programming statements enclosed within {....}. Here, computer first checks whether given condition, i.e., variable "a" is less than 5 or not and if it finds condition is true then the loop body is entered to execute given statements. Here, we have the following two statements in the loop body:
  • First statement is printf() function, which prints Hello World!
  • Second statement is i = i + 1, which is used to increase the value of variable i
After executing all the statements given in the loop body, computer goes back to while( i < 5) and given condition, (i < 5), is checked again, and the loop is executed again if condition is true. This process repeats till the given condition remains true which means variable "a" has a value less than 5.
To conclude, a loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:
Loop Architecture This tutorial has been designed to present programming's basic concepts to non-programmers, so let's discuss about two important loops available in C programming language. Once you are clear about these two loops, then you can pickup C programming tutorial or a reference book and check what are other loops available in C and how do they work.

The while Loop

A while loop available in C Programming language has following syntax:
while ( condition ) { /*....while loop body ....*/ } Above code can be represented in the form of a flow diagram as shown below:
while loop in C There are following important points to note about a while loop:
  • A while loop starts with a keyword while followed by a condition enclosed in ( ).
  • Further to while() statement you will have body of the loop enclosed in curly braces {...}.
  • A while loop body can have one or more lines of source code to be executed repeatedly.
  • If while loop body has just one line, then its optional to use curly braces {...}.
  • A while loop keeps executing its body till given condition is true. As soon as condition becomes fast, while loop comes out and continue executing from immediate next statement after while loop body.
  • A condition is usually a relational statement, which is evaluated to either true or false values. A value equal to zero is treated as false and any non-zero value works like a true for the condition.

The do...while Loop

If you have noted while loop, it checks given condition before it executes given statements of the code. C programming provides another form of loop, which is called do...while loop and allows to execute a loop body before checking given condition. This has following syntax:
do { /*....do...while loop body ....*/ } while ( condition ); Above code can be represented in the form of a flow diagram as shown below:
do...while loop in C If you will write above example using do...while loop, then Hello, World will produce the same result:
#include <stdio.h> main() { int i = 0; do { printf( "Hello, World!\n"); i = i + 1; }while ( i < 5 ); } When above program is executed, it produces the following result:
Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!

The break statement

When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. The syntax for a break statement in C is as follows:
break; A break statement can be represented in the form of a flow diagram as shown below:
c break statement Following is a variant of the above program, but it will come out after printing Hello World! only three times:
#include <stdio.h> main() { int i = 0; do { printf( "Hello, World!\n"); i = i + 1; if( i == 3 ) { break; } }while ( i < 5 ); } When above program is executed, it produces the following result:
Hello, World! Hello, World! Hello, World!

The continue statement

The continue statement in C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. The syntax for a continue statement in C is as follows:
continue; A continue statement can be represented in the form of a flow diagram as shown below:
C continue statement Following is a variant of the above program but it will skip printing when variable has a value equal to 3:
#include <stdio.h> main() { int i = 0; do { if( i == 3 ) { i = i + 1; continue; } printf( "Hello, World!\n"); i = i + 1; }while ( i < 5 ); } When above program is executed, it produces the following result:
Hello, World! Hello, World! Hello, World! Hello, World!

Loops in Java

Following is the equivalent program written in Java programming language. Java programming language also provides while and do...while loops. Following program will be used to print Hello, World! five times as we did in case of C Programming:
You can try to execute following program to see the output, which must be identical to the result generated by the above example.
public class DemoJava { public static void main(String []args) { int i = 0; while ( i < 5 ) { System.out.println("Hello, World!"); i = i + 1; } } } The break and continue statements in Java programming work very similar way, what they work in C programming.

Loops in Python

Following is the equivalent program written in Python. Python also provides while and do...while loops. Following program will be used to print Hello, World! five times as we did in case of C Programming. Here you must note that Python programming does not make use of curly braces for loop body, instead it simply identifies the body of the loop using indentation of the statements.
You can try to execute following program to see the output. To show the difference I used one more print statement, which will be executed when loop will be over.
i = 0 while (i < 5): print "Hello, World!" i = i + 1 print "Loop ends" When above program is executed, it produces the following result:
Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Loop ends The break and continue statements in Python programming work very similar way as they work in C programming.