Pages

Wednesday, 27 July 2011

Decision Making, Branching & Looping




Introduction
Execution of a program is essentially sequential in nature. Statements are executed in the order of their appearance in a program. However, this causes a serious limitation on the programming capabilities. Consequently every programming language provides programming constructs that allow altering the sequence of execution. In this section we will consider, the programming constructs of this type provided with C language.
Control Statement
The statements that allow programmers to alter the sequential flow of execution of the program and control the flow are called control statements. Control statements enable the programmers to specify the order in which the various instructions in a program are to be executed by the computer. In short, they determine the flow of control in a program.

Why Control Statement?
Control statements are probably most desirable feature of any programming language. Consider writing program that prints " HELLO WORLD" 10 times. Without using any control programming construct the program would look like the one listed below:
#include<stdio.h>
main( )
{
printf ("HELLO WORLD");
printf ("HELLO WORLD") ;
printf ("HELLO WORLD");
printf ("HELLO WORLD");
printf ("HELLO WORLD");
printf ("HELLO WORLD");
printf ("HELLO WORLD");
printf ("HELLO WORLD");
printf ("HELLO WORLD");
printf ("HELLO WORLD");
Obviously this is not convenient way of programming. Without any control structure you will have to repeat printf statement as many times as you want them to print. Imagine what will you do if you have to print this statement 1000 times. Control statements let you write such programming problems in only few lines. Thus, using for loop (to be discussed later) the above program can be written as follows:
#include<stdio.h>
main(   )
{
for (i=l; i<=1000; i++) printf ("HELLO WORLD");
;"    }  
This program is just one liner but prints HELLO WORLD 1000 times.. Such is the power of control statements.
Consider another class of problems where you wish to execute certain statement only when some condition is true. Control statements allow you to select or reject statements to be executed depending on the truth value of a conditional expression.
But for the control statements a programming language would have only limited
application.
                           
C language provides 4 types of control statements, which are sufficient to form any desired form of execution control. These control statements are:

a)                Sequence control statements
b)                Decision control statements or conditional statement
c)                   Case control statements                             
d)              Repetition or loop control statements


c programmming online,basic of c language,structure of c programming,c language statements,Compiling a C Program,Pseudo Codes in c,basis data type in c ,variables in C Program,constants in C Program,c operators in C Program,special operators in C ,Decision Making,Branching & Looping in C Program,if statement in C Program,for loop in C Program

No comments:

Post a Comment

Post a Comment