Chapter 5,
CONTROL STATEMENTS AND LOOPS
Control statements are used to control the execution of the program/application. Usually depending on a condition.
The conditional control statements :
IF condition
The "if" condition is used to check if a condition is valid in a given instance of time(when the condition is been executed) usually the expression given in the condition is eveluated to a boolean value and checked if it is true or false. If the condition is evaluated to be true the statements inside the if clause is allowed to be executed, if the condition is evaluated to be false, the statements inside the "if" clause is skipped and the statement following immediate to the if clause is executed.
Structure of the if clause
if( <<conditional expression>> )
{
// the statements
}
// next statement in the program
Use of "else" condition: The else condition is used along with the if condition , if the condition in the if clause fails, the statements inside the else block is executed.
Structure of if combined with else
if(<<conditional expression>>)
{
// the if clause statements
}
else
{
// the else clause statements
}
Use of "else if" condition: The else if condition is used along eith if clause, this helps in making a series of checks before going for the final else only condition(yes the "else if " condition come in between the "if" and the "else" blocks)
Structure of if , else if and else
if( <<conditional expression>> )
{
// the if clause statements
}
else if( <<conditional expression>> )
{
// the else if statements
}
else if( <<conditional expression>> )
{
// the else if statements
}
.
.
.
else
{
// the else clause statements
}
The datatypes that can be used to perform this type of switch is booleans, enums, integers, and strings.
the structure is
switch( <<Parameter>> )
{
case <<case value1>> :
// the statements for this case
break;
case <<case value2>> :
// the statements for this case
break;
.
.
.
.
default:
// the statements for the default case
}
Here , in the switch case the parameter is checked with the first case, if it evaluates to be true, then the lines following it immediate are executed until the control encounters a break statement when it encounters this break statement the control is brougt out of the switch block, if it evaluates to false, then the next case(case 2 and so on) is evaluated in a similar fashion. there is also a special case called the default case, this case is executed if no other cases are evaluated to true.
More terms in control statements: There are few more control statements in C# these are also called branching statements
The "continue" statement is used to skip the following statements in a block , usually a loop and continue with the next iteration of the loop. will be explained in detail later.
The "goto" statement is used to take the control to a particular line of code in the program where a label is been defined. the goto is followed by the name of the label wherever the control has to be taken.
For example:
if( i > 10 )
{
goto finish; // goto is folowed by a label
}
else
{
i = i+1;
}
finish:
console.WriteLine(" now i is greater than 10 ");
here the control first enters and evaluates the condition which is an expression that evaluates to a boolean value. the loop will continue to execute the block of code as long as the expression evaluates to true. if it evaluates to false, the control never enters inside the loop i.e the block of code. and the execution starts from the line immediate next to the "}" .
The DO WHILE loop is similar to the while loop except for the fact that the boolean condition is checked at the end of the loop. meaning that the loop will be executed atleast once before the condition is checked hence its guranteed that the loop will be executed atleast once even if the condition fails in the very first iteration.
the stcucture and syntax is as follows:
do
{
// block of code
}
while( <<condition>> )
Here the control first goes through the block of code executes it , then enters the condition and checks if it evaluates to true. if it does then enters the block of code again, this happens until the condition evaluates to false. when the condition fails, the statement immediate after the while condition is executed.
The FOR loop is similar to the while loop except for the fact that , the loop holds the statements for the initializations, the condition check and the increments as well. hence this type of looping is usefull when we know the exact number of iterations we have to perform.
ht estaructure and the syntax is :
for( <list of initializations> ; <condition check > ; <list of increments > )
{
// block of code
}
all three sections are separated by a ";"
CONTROL STATEMENTS AND LOOPS
Control statements are used to control the execution of the program/application. Usually depending on a condition.
The conditional control statements :
IF condition
The "if" condition is used to check if a condition is valid in a given instance of time(when the condition is been executed) usually the expression given in the condition is eveluated to a boolean value and checked if it is true or false. If the condition is evaluated to be true the statements inside the if clause is allowed to be executed, if the condition is evaluated to be false, the statements inside the "if" clause is skipped and the statement following immediate to the if clause is executed.
Structure of the if clause
if( <<conditional expression>> )
{
// the statements
}
// next statement in the program
Use of "else" condition: The else condition is used along with the if condition , if the condition in the if clause fails, the statements inside the else block is executed.
Structure of if combined with else
if(<<conditional expression>>)
{
// the if clause statements
}
else
{
// the else clause statements
}
Use of "else if" condition: The else if condition is used along eith if clause, this helps in making a series of checks before going for the final else only condition(yes the "else if " condition come in between the "if" and the "else" blocks)
Structure of if , else if and else
if( <<conditional expression>> )
{
// the if clause statements
}
else if( <<conditional expression>> )
{
// the else if statements
}
else if( <<conditional expression>> )
{
// the else if statements
}
.
.
.
else
{
// the else clause statements
}
SWITCH condition
The switch statement is another type of a conditional control statement, based on the value of the passed parameter, the control is switched.The datatypes that can be used to perform this type of switch is booleans, enums, integers, and strings.
the structure is
switch( <<Parameter>> )
{
case <<case value1>> :
// the statements for this case
break;
case <<case value2>> :
// the statements for this case
break;
.
.
.
.
default:
// the statements for the default case
}
Here , in the switch case the parameter is checked with the first case, if it evaluates to be true, then the lines following it immediate are executed until the control encounters a break statement when it encounters this break statement the control is brougt out of the switch block, if it evaluates to false, then the next case(case 2 and so on) is evaluated in a similar fashion. there is also a special case called the default case, this case is executed if no other cases are evaluated to true.
More terms in control statements: There are few more control statements in C# these are also called branching statements
- Break
- Continue
- Goto
The "continue" statement is used to skip the following statements in a block , usually a loop and continue with the next iteration of the loop. will be explained in detail later.
The "goto" statement is used to take the control to a particular line of code in the program where a label is been defined. the goto is followed by the name of the label wherever the control has to be taken.
For example:
if( i > 10 )
{
goto finish; // goto is folowed by a label
}
else
{
i = i+1;
}
finish:
console.WriteLine(" now i is greater than 10 ");
Control statements - Loops:
The looping statements are used to iterate the execution of a block of statements until a certain condition is evaluated to be true. What do i mean by this?
Consider a situation where you need a certain number or a set of instructions to be executed again and again for a certain number of times to attain some result. how do u do it? this is where the use of loops come in. the name says it all, you loop through the same set of statements again and again performing some task.
There are different types of Loops:
- While
- Do while
- For
- Foreach
The WHILE loop will loop through a set of statements inside a block based on a boolean condition. the structure of the loop:
while( << condition >> )
{
// block of code
}
while( << condition >> )
{
// block of code
}
here the control first enters and evaluates the condition which is an expression that evaluates to a boolean value. the loop will continue to execute the block of code as long as the expression evaluates to true. if it evaluates to false, the control never enters inside the loop i.e the block of code. and the execution starts from the line immediate next to the "}" .
The DO WHILE loop is similar to the while loop except for the fact that the boolean condition is checked at the end of the loop. meaning that the loop will be executed atleast once before the condition is checked hence its guranteed that the loop will be executed atleast once even if the condition fails in the very first iteration.
the stcucture and syntax is as follows:
do
{
// block of code
}
while( <<condition>> )
Here the control first goes through the block of code executes it , then enters the condition and checks if it evaluates to true. if it does then enters the block of code again, this happens until the condition evaluates to false. when the condition fails, the statement immediate after the while condition is executed.
The FOR loop is similar to the while loop except for the fact that , the loop holds the statements for the initializations, the condition check and the increments as well. hence this type of looping is usefull when we know the exact number of iterations we have to perform.
ht estaructure and the syntax is :
for( <list of initializations> ; <condition check > ; <list of increments > )
{
// block of code
}
all three sections are separated by a ";"
The control first inters the initialization section and this happens only once in the life cycle of the loop. the control never enters this section again. the control enters the condition check section and checks if the condition evaluates to true if so it enters the Block of code and executes it, when it has completely executed the block , it enters the list of increments section and performs the increments, after the increments are done it again checks for the condition if true , it enters the block of code. else it skips the block and ends the loop.
The FOREACH loop works on datasets like the lists , arrays, array list etc. generally the datasets found in the System.Collections namespace. the foreach loop will execute the block of statements for each of the element in the dataset.
The stcucture / syntax is as follows
foreach( <datatype> <iterator name> in <the dataset of similar datatype or a list> )
{
// block of code
}
Here in the foreach loop , the loop is repeated for each of he elements in the list or the dataset. a iterator variable has to be declared inside the foreach loop, this iterator should be of the same datatype as that of the datatype of the list to be iterated.
No comments:
Post a Comment