2.5 STATEMENTS IN C PROGRAMMING

 A program consists of statements. These statements may be a variable declaration, arithmetical expression, logical expression, call of a function etc. According to the nature of statements belong to different category. These categories are:

  • · Sequential Statement C Programming Language
  • · Conditional Statement
  • · Iterative/Loop Statement

2.5.1 Sequential Statement

Sequential statements are those statements in a program that executes one by one in a given sequence. For example following statements:

1. float a,b;

2. printf("Give the first number:");

3. scanf("%f",&a);

4. printf("Give the second number:");

5. scanf("%f",&b);

Above five statements are part of sequential statements of a C program. These statements will be executed one by one in a C program.

2.5.2 Conditional Statements

Many time while writing program you will need to take some decisions. These decisions are to be taken based on certain conditions. For example suppose you have to find whether a person can vote or not. For this you need to compare the age of the person with the minimum age required to qualify for voting right. 

Age >= Age required for voting

You can say that a program can be much more powerful if you control the order in which statements are run. Some time you may need to select between optional sections of a program. Suppose you are required to prepare two lists of candidates as given below:

1. Who will be appearing in examination

2. Who will not be appearing in examination

On the basis of percentage of attendance of student. The condition is that if a student is having 75 % or more attendance will be appearing in the examination otherwise not. To decide whether a student will be appearing in examination or not, you need to compare his percentage of attendance with 75 (the percentage of attendance required to appear in exam), and then make a decision about what to do next.

In C there are some conditional statements:

· if

· ifelse

· ifelse

if

· swithchcase

are there for performing conditional operation. Now we will discuss about if, ifelse,

and

ifelse

if, and switchcase

one by one. Below is the flow chart of if statement.

structure of if statement:

if( condition)

{ statement(s)

}

Here if condition is true then only statement(s) in { } will be executed. In other words you can say that control will reach in { } only after checking condition and finding it true.

/* if evaluated expression is not 0 */

if (expression)

{

/* then execute this block */

}

/* Continue other operations in sequence*/

/*Example program to test whether a given number is greater than 10 */

#include <stdio.h>

void main(void)

{

int num = 20; /* initialize num by 20*/

if( num > 10)

{

printf(“The number is greater than 10\n”);

}

printf(“The number is:%d”,num);

return 0;

}

The output of this program will be:

The number is greater than 10

The number is:20

Now let us see if –else statement. This statement is used if you have to choose one from two given set of statement(s).The structure of ifelse

is

if(consition)

{

/* if condition is true this block will execute*/

statement(s) C Programming Language

}e

lse

{/

* condition is false this block will execute */

statement(s)

}

ifelse

also work similar to if statement. The only difference between if and ifelse

is that

in if after finding condition true a set of statement(s) is executed and in ifelse

for true

value of condition one set of statement(s) is executed and for false value of condition another set of statement(s) is executed. Below is the flowchart for ifelse statement.


You can say that to decide between two courses of action ifelse is used. For example to decide whether a student has passed an exam with a pass mark of 45 you can write c code:

/*

if (Marks >= 45)

{

printf("The Student is Pass\n");

else

{

printf("The Student is Fail\n");

}

Another conditional statement is ifelseifelse.

This statement is also known as ladder if statement. Flowchart for ifelse ifelse is given below.

This conditional statement is used in situations where you wish to make a multiway decision based on certain conditions. The way of doing such type of testing is by using several elseif statements. This works by cascading more than one comparison. As soon as a comparison gives a true result, the following statement(s) or block is executed, and after that no further comparisons are performed. You have already seen one example of comparison where you found whether a student is pass or not. Suppose you have find the grade of a student on the basis of marks of the student then you need to do many comparison. In the following example we will find the grades depending on the exam result.
if (Marks >= 75)
{
printf("Passed and Grade is A\n");
}
else if (Marks >= 60)

{ C Programming Language

printf("Passed and Grade is B\n");

}

else if (Marks >= 45)

{

printf("Passed and Grade is C\n");

}

else

{

printf("Failed\n");

}

In this example, all comparisons are testing a single variable called Marks. There may be cases where you have to do multiple comparisons on more than one variable. While doing programming the same pattern can be used with more or fewer else if's. Note that finally only else may be left out as last block. Now, let us see one more very useful statement used for multi way decision making. This statement is switchcase.

This statement work similar to ifelseif statement.

The switchcase 

Statement

This statement is for the multi way decision. It is well structured, but it is having one limitation that it can be used only in certain cases where only one variable is tested for condition checking. All branches of the decision must depend on the value of that variable only. Also the variable used in condition checking must be an integral type (int, long, short or char).

In switchcase statement each possible value of the variable can control a single branch and a final, catch all leftover cases. You should use default branch (final) for catching all unspecified cases.

Now, let us see the example below. This will clarify you the concept of switchcase.

This example takes an objective which converts an integer into a text description you can say at this program is working to estimate the number given as input.

int number;

/* Estimate a number as none, one, two, more, many */

switch(number)

{

case 0 :

printf("The number is : None\n");

break;

case 1 :

printf("The number is :One\n");

break;

case 2 :

printf("The number is : Two\n");

break;

case 3 :

case 4 :

case 5 :

printf("The number is : More than Two\n");

break;

default :

printf("The number is : Many\n");

break;

}

}

In the above example, each case is listed with some corresponding action.For example case 0 is associated with following action:

printf("The number is : None\n");

break;

Here, the break statement is a new thing for you to notice. The break statement prevents any further statements from being executed by leaving the switch statement(block) , in other words, you can say that break statement is used for transferring control to the next block from the current block. The break statement is generally used to exit from a loop or a switch, control passing to the first statement beyond the loop or a switch block. You can see that the case 3 and case 4 have no following break, those continue on allowing the same action for several values (3,4,5) of number.

The similarity between if and switch statement is that both allow a programmer to make a selection from a number of possible actions.

2.5.3 Iterative Statements

You may need to write same code more than one in your program. For example if you have to print numbers from 1 to 10 and one number in a line, then in general you will need 10 prinf( ) statements in your program one printf for printing one number. 

There is concept of loop in C programming. Conceptually a loop is like a circle. If you walk on border of a circle after completing a round you will reach to the same point.

There may be a set of statements in a program which are executed several time. Such statements are called iterative statements. This concept helps in writing such program where same code is repeated several time. You can only write once that code in a loop and run the loop as many time as required. In the next section we will discuss about for and while loop of C.

Loops in C

In C you have a choice of three types of loop while, dowhile and for. Before you use any looping statement it is necessary to decide the condition which take control of the program out of the loop. Once, control of program enters into a loop it repeats the statement(s) in side the loop. The control comes out of the loop after test condition becomes false. You can say that a loop runs till a test condition is true and stops when test condition become false. Here, you may ask that how a test condition becomes false from a true value? While a loop runs, value of variables involve in test condition also may change and that leads to change the test condition from true to false.

The for Loop

The for loop is frequently used. It is very flexible in terms of condition check and number of iterations. You can say that the for loop works well where the number of iterations of the loop is known before the loop is entered.

Structure of for loop is:

for( initial condition; test condition; increment or decrement)

{ statement(s);

} C Programming Language

The beginning of the for loop consists of three parts and they are separated by

semicolons. The first part is run before the loop is entered and is usually the initialization

of the loop variable. The second is a test condition. The for loop will exit when this

condition returns false.The third and final statement runs every time the loop body is

completed. This is usually an increment or decrement of the loop counter and on the basis

of this counter value test condition retain true or become false.

Now let us see a program which uses for loop for printing numbers 1to 10.

#include <stdio.h>

void main(void)

{

int i;

for( i=1; i<= 10; i++)

{

printf(“Number: %d”,i);

}

return 0;

}

When you will run this program following output will come:

Number: 1

Number: 2

Number: 3

Number: 4

Number: 5

Number: 6

Number: 7

Number: 8

Number: 8

Number: 10

You are getting this output because:

printf(“Number: %d”,i);

statement in this program is inside a for loop which is running 10 times.

You may write a program using for loop for finding the sum of a series:

1+2+3+4+……………..+n.This program will be like:

//Programe for finding sum of series: 1+2+3+4+……………..+n

#include <stdio.h>

void main(void)

{

int i,n,sum=0;

printf(“Give the Number of n:”);

scanf(%d”,&n);

for( i=1; i<= n; i++)

{

sum = sum+i;

}

printf(“The sum of the series is: %d”,sum);

return 0;

}

If you run this program and give 5 as value of n then it will print:

The sum of the series is:15

The while Loop

Similar to for loop in the while loop also you can keep repeating an action until an associated test condition returns false. The while loop is useful where the programmer (the person writes/design the program) does not know in advance how many times the loop will be repeated (iterated).For example in a situation where a loop terminates after specific input from the user which make test condition false. 

For example in while loop if a test condition is askin for the acceptance (Yes or No) from the users to decided about continuity of the loop, then for terminating (coming out of the loop) the loop user has to give value No. The syntax of while loop is:

while( condition)

{ statements(s)

}

By seeing the flowchart of whileloop you will get clearer picture of working of this loop.

Below is the Flowchart for whileloop:


Now, you may write a program using while loop for finding the sum of a series:

1+2+3+4+……………..+n.This program will be like:

//Programe for finding sum of series: 1+2+3+4+……………..+n

#include <stdio.h>

void main(void)

{

int i,=1;

int n,sum=0;

printf(“Give the Number of n:”);

scanf(%d”,&n);

while ( i<= n)

{

sum = sum+i;

i=i+1;

}

printf(“The sum of the series is: %d”,sum);

return 0;

}

If you run this program and give 7 as value of n then it will print:

The sum of the series is:28

The dowhile

Loop

The dowhile

loops is similar, but the test occurs after the loop body is executed. This ensures that the loop body is run at least once. The while loop repeats a statement until the test at the top proves false. The do while loop is frequently used where data is to be read.

In such process read the data, then test and verifies the data, and loops back to read again if it was undesirable.

The syntax of dowhile loop is:

do

{

statements(s)

}

while( condition)

By seeing the flowchart of dowhile

loop you will get clearer picture of working of this

loop.

Below is the Flow Chart for dowhileloop:


Syntax of dowhile

loop:

do

{

statement(s)

}

while (condition)

For example the statements :

printf("Enter 1 for exit from loop");

scanf("%d", &input_value);

will repeatedly run inside the dowhile

loop given below until you give/read 1 as

input_value .

do

{

printf("Enter 1 for exit from loop");

scanf("%d", &input_value);

}

while (input_value != 1)

Now let us see a program which uses do while loop for printing numbers 1to 10.

#include <stdio.h>

void main(void)

{

int i=1;

do

{

printf(“Number is: %d”,i);

i=i+1;

}

while( i<=10)

return 0;

}

When you will run this program following output will come:

Number is: 1

Number is: 2

Number is: 3

Number is: 4

Number is: 5

Number is: 6

Number is: 7

Number is: 8

Number is: 8

Number is: 10

Sometimes a need arises to have many variables of the same type. For example to store marks of different subjects of a students one need to have as many variable as the number of subjects. In such situations array variable is very useful. In next section you will learn arrays.


Comments

Popular posts from this blog

3.8 SECURE NETWORK DEVICES

3.5 SECURITY ISSUES FOR SMALL AND MEDIUM SIZED BUSINESSES

3.6 TOOLS FOR NETWORK SECURITY