Decision constucts

Overview

While writing C programs, the programmer may need to choose a path to execute one or more statements through the program based on a certain criterion; decision constructs provide a way to meet his requirements. 

Control Structures

"Control structures are statements used o control he flow of execution in a program or function. The C control structures enable us to group individual instructions into a single logical unit with one entry point and exit point.

Program instruction can be organized into three kinds of control structures o control execution flow i.e. sequence, selection, repetition. All programs, whether simple or complex, use these control structures to implement the program logic.

Until now we have been using only sequential flow, which is also called default flow. In case of sequence structures, instructions are executed in the same order in which they are specified in the program. A compound statement refers the group of statements enclosed in the opening and closing braces such as
{
         statement 1; 
         statement 2;
               .
               .
               .

         statement n;
}

control flows from statement 1 to statement n in a logical sequence. Here I shall discuss the selection structure in detail, solutions of some problems require steps with two or more alternative course of action. A selection structure chooses which statement or a block of statements is to execute. In C, there are two basic selection statements:

  • if-else
  • switch 
there are some variations of if-else structure which will be discussed here. The third control structure is repetition, which is also called iteration or loop. It is a control structure, which repeats a statement or a group of statement in a program. C provides different type of loop structures to be used in various situations. These includes for loop, while loop, do-while loop.

IF STATEMENT:

if is one of the keywords in C language. It is used to select a path flow in a program based on a condition. A condition is an expression that either evaluates to either evaluates to true (usually represented by 1) or false (represented by 0). The result of this evaluation can be assigned to a variable. For example, consider the following program:

#include <stdio.h>
void main ()
{
     int age, status;
 
     print f ("Enter the age :" );
     scan f ("%d", &age);
   
     status = (age > 60);
     print f (''status = %d", status);
}
The value of the variable status will be 1 if the age is greater 60, otherwise status will be 0. Here's the output of the program:

Case 1 (When age is less than 60)                   Case 2 (When age is greater than 60)
Enter the age :45                                            Enter the age :78
Status=0                                                        Status = 1

Simple if Statement:

if statement is the simplest form of decision constructs. It allows a statement or a set of statement to be executed conditionally. The general form of simple if statement is:
if (condition) 
{
           statement 1;
           statement 2;  
                 .
                 .
                 .
           statement n;
}

The statement(s) in the block of if statement are executed if the condition is true; otherwise these are skipped. If there are more statements in if block then these should be enclosed in braces as a compound statement. However, in case of a single statement the braces are optional.











Comments

Popular posts from this blog

Data Base Design Process

Data Base

VLOOKUP