homecode zoneQuick Codedownloadssubmit codeabout usfeedbackscontact ussitemap
 
Beginners
Intermediate
Proffessional
Array
Array & String
Looping
Branching
Constant Variable & Data type
Operators
Function
Structures & Unions
Pointers
Liabrary
Pro - Languages
C/C++
Java
Visual basic
asp
vb.net 2003/2005
asp.net
Quick Code
SUBMIT CODE
Mail List
LINKS
Submit LINK
 
ProgrammerGuide.net - C-Branching
Home » C/C++ Zone » Branching

Introduction

We have seen  that a C program is a set of statement which are normally executed sequentially in the order in which they appear. This happens when no option or no repetitions of certain calculation are necessary . However , in practice  , we have a number of situation where we many have to change the order of execution of statement based on certain conditions, or repeat group of statements until certain  specified condition are met.

C language possesses such decision - making capabilities by supporting the following statement

1. if statement
2. switch statement
3. conditional operator statement
4. goto statement

These statements are popularly known as decision - making statements. Since these statements 'control' the flow of execution , they are also known as control statements.

Decision Making With If Statement

The if statement is a powerful decision - making system statement and it used to control flow of execution of statement . It is basically a two - way decision statement and in conjunction with an expression .It allows the computer to evaluate the expression first and then , depending on whether the value of the expression (relation or condition) is 'true' ( or non-zero) or 'false' (zero), it transfers the control to a particular statement.

Some examples of decision making, using if statements are:

1. if (bank balance is zero)
borrow money

2. if (room is dark )
put on lights

The if statement may be implemented in a different form s depending on the complexity of condition to be tested. The different forms are:

1. Simple if statement
2. if ......else statement
3. Nested if ......else statement
4. else if statement.

Simple If Statement

The general form simple if statement is
 
if (test expression)
{
    statement - block;
}
statement - x;

The statement block may be single statement or a group statements if the test expression is true, the statement block will be executed : otherwise the statement block will be execution will jump to  the statement - x . Consider the following  segment of a program that is written for processing of marks obtained in an entrance examination.


if (category == SPORTS)
{
marks = marks + bonus _ marks;
}
printf ("%f " , marks);


The program tests the type of category of the student . If the student belongs to the SPORTS category , then additional bonus _ marks are added to his marks before they are printed. For other bonus_ marks are not added.
 

 



« Previous 1 2 3 Next »

Viewers: 8
Visitors:71402
Last Updated on:Sunday, January 17, 2010
Home | About us | Feedback | Contact us | Sitemap
Copyright © 2008 ProgrammerGuide.net. All rights reserved.
This site is best viewed in IE 6.0, Firefox 1.5 and above with screen resolution of 1024 X 768.