Home » C and C++, Tech Corner

Control Statements(Decision Making & Branching)

20 July 2009 No Comments Posted By:Dileep
  • Normally, the statements in a program are executed in the order in which they appear in the program. This type of execution is called sequential execution.
  • Control structures – define the order of execution of statements.
  • Conditional control statements – involves both decision making and branching.
  1. if-statement.
  2. if-else statement.
  3. Nested-if-statement.
  4. switch statement.
  • if-statement – used to execute a statement or a set of statements conditionally.
  • Also called a one-way branching.
  • Syntax -

if (condition)
{
statement;
}

  • if-else statement – The if statement is used to execute only one action. If there are two statements to be executed alternatively, then if-else statement is used.
  • Two-way branching.
  • Syntax -

if (condition)
{
statement1;
}
else
{
statement2;
}

  • Nested-if statement – If there are more than two alternatives to select, then the nested-if statements are used.
  • Enclosing an if statement within another if statement is called the nested-if statement.
  • Syntax -

if (condition1)
{
if (condition2)
{
statement1;
}
else
{
statement2;
}
}
else if (condition3)
{
statement3;
}
else
{
statement4;
}

  • There are many ways to nest one if-else within the other.
  • switch statement – The switch statement provides a multiple way of branching.
  • It allows the user to select any one of the several alternatives, depending on the value of an expression.
  • Syntax -

switch (expression)
{
case label1 : block1;
break;
case label2 : block2;
break;
.
.
.
case labelmax : blockmax;
break;
case default : dblock;
break;
}

  • The expression is of type int or char.
  • Depending on the value of an expression, execution branches to a particular case label and then all the statements belonging to that case label are executed.
  • The break statement indicates the end of a particular case label and thereby the switch statement is terminated.
  • The case default is executed, when the value of an expression is not matched with any of the case labels.
  • The semicolons should not be placed at the end of switch (expression).
  • goto-statement – an unconditional control statement.
  • To transfer the control from one point to another in a C program.
  • Syntax -

    goto label;

  • Forward jump and backward jump.
  • Bachward jump will form a loop.
  • The use of goto statement in a structured programming language like C should be avoided. Use if and only if it is unavoidable.
  • The goto statement may create an infinite loop where the computer enters a permanent loop. The careful and cautious design would resolve such situations.
  • A program may contain any number of goto statements.
  • No two statements can have the same label.

<<Previous Post

Next Post>>

Related posts:

  1. Loop Control Structures(Decision Making & Looping)
  2. Preprocessor Statements
  3. Constants and Variables
  4. Operators & Expressions
  5. Functions

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>