Wednesday, March 4, 2009

C#-Ch 4: Control Statements - Part I

4. Control Statements – Part 1

Control statements are used to change the normal top-down sequence or linear sequence of program execution. If a block of statements has to be skipped we use the control statements to bypass this block. Similarly, if a block of statements has to be repeated many times, we use control statements that will cause this repetition to take place.

C# has the following types of control statements:

1. Selection statements:
a. If statement
b. If … else statement
c. Switch … case statement
d. ? : operator
2. Iterative statements
a. for loop
b. while loop
c. do loop
d. foreach statement

In this section we discuss the selection statements.

if statement:
The if statement executes a given set of statements based on the condition given by the programmer.

Syntax:
if (boolean expression)
statement-1

If boolean expression is true, then statement-1 is executed, otherwise statement-1 is skipped.

if – else statement:
The if – else statement is an extension of the simple if statement.

Syntax:
if (boolean expression)
statement-1
else
statement-2

If boolean expression is true, then statement-1 is executed, otherwise statement-2 is executed. Statement-1 and statement-2 may be a single statement or a set of statements. In case it is a set of statements, then these are enclosed in between the pair of braces { and }.

Nesting of if statements:
Nesting is the inclusion of one statement within another. If one if statement is placed inside another if statement we say that the if statements are nested.
Nesting can be done in the if section or in the else section.

Example:
if (condition1)
{
//action statements-1
}
else
{
if (condition2)
{
//action statements - 2
}
else
{
//action statements - 3
}
}

A complete if …else statement is nested within the else section of the outermost if statement. This nested if statement is shown in bold in the above code.

If condition-1 is true, the first if statement is true and action statements-1 are executed after which control exits the outermost else statement.

If condition-2 is false, control passes to the first if condition inside the else (i.e. condition-2 is checked). If condition-2 is true, then action statements -2 are executed and control exits the if …else structure. This corresponds to the case: condition-1 false and condition-2 true.

If condition-2 is false, control passes to the action statements-3. After executing action statements-3, control exits from the if…else structure. This is equivalent to the case: condition-1 false and condition-2 false.

Stacking if statements
Stacking if statements combines the else part with another if statement. The following example illustrates this:


Example:
if (cond-1)
{
action-1
}
else if (cond-2)
{
action-2
}
else
{
action-3
}
action-4
}


Switch-case statement:
The switch case statement is a selection statement. It is used when there are a number of possible values to be tested and multiple if-else statements become difficult to use.

Syntax :

switch (value)
{
case result1:
// do statements for result 1
break;
case result2:
// do statements for result 2
break;
case result3:
// do statements for result 3
break;
. . . . .
default:
// do statements for default case
break
}

We observe the following in switch – case statement:
1. There is no condition to check.
2. A value is used to branch to the appropriate code.
3. Two case statements cannot have the same value.
4. The value mentioned in the switch statement may be a variable or an expression.
5. The value is compared to each value in the case statement. If the value matches, the code corresponding to that case is executed.
6. If the value does not match any of the values given in case statement, then control flows to the default case.
7. If default case is not mentioned, then control goes to the statement immediately after the switch statement.
8. Each case is terminated by the break statement. This statement signifies the end of the code of that case. If break statement is not given, compiler generates an error.

Fallthrough in switch statement:
In C, C++, and Java, break statement may be omitted in a case block. The control moves to the next block without any errors. This is called as “fallthrough”. But in C#, this fallthrough is not permitted if the case block contains executable code. However, if the code block is empty, then falltrough is permitted in C#. If we want two consecutive case blocks to be executed one after the other, we must force control to go to the next case by using the goto statement

IMPORTANT QUESTIONS
1. What is the use of control statements in programming languages?
2. Explain the following selection statement: if … else in C#
3. When is a switch – case statement used in C# programs? Explain the concept of “fallthrough”.
4. Is the following if statement valid? If so, what is the value of x after this code executes?

int x = 2;
int y = 3;

if (x == 2) if (y < 3) x = 5; else x = 9;
5. In what way does the switch statement differ from the if statement? When is it preferable to use the switch statement?
6. Explain nested if statements with the help of an example.
7. Explain the concept of stacking of if statements with the help of an example.

PROGRAMMING EXERCISES
1. Write a program to add all the even numbers from 0 to 20. Use simple if and goto statements to form a loop of operations.
2. Write a program that accepts an integer between 1 and 7, and based on the input, it prints the day of the week. Assume Monday = 1, etc. If input is not in the range 1 to 7, print an appropriate error message and terminate the program.

No comments:

Post a Comment