Conditional Constructs
If –else / Switch case
Iterative Looping
Conditional Constructs
Ø Conditional constructs allow the selective execution of statements, depending on the value of expression associated with them.
Ø The if…else conditional construct is followed by a logical expression where data is compared and a decision is made on the basis of the result of the comparison.
Ø The following is the syntax of the if…else construct:
if (expression)
{
statements;
}
else
{
statements;
}
Ø The if…else constructs can be nested inside each other.
Ø When if…else construct is nested together, the construct is known as cascading if…else constructs.
Eg
How to find even and odd number?
public Void display()
{
Console.WriteLine(“Enter the Number”);
Number=Convert.ToInt32(Console.ReadLine());
if(Number%2==0)
{
Console.WriteLine(“Number is Even “+Number);
}
else
{
Console.WriteLine(“Number is Odd ”+Number);
}
}
The switch…case Construct
Ø The switch…case construct is used when there are multiple values for a variable.
Ø The following is the syntax of the switch…case construct:
switch (VariableName)
{
case ConstantExpression_1:
statements;
break;
……
default:
statements;
break;
}
Eg :- Create program display WeekOfDays in C#?
public void display()
{
Console.WriteLine(“Enter the Week No.”);
weekNo =Convert.ToInt32(Console.ReadLine());
swtich(weekNo)
{
case 1: Console.WriteLine(“Week Of Days Monday”); break;
case 2: Console.WriteLine(“Week Of Days Tuesday”); break;
case 3: Console.WriteLine(“Week Of Days Wednesday”); break;
case 4: Console.WriteLine(“Week Of Days Thursday”); break;
case 5: Console.WriteLine(“Week Of Days Friday”); break;
case 6: Console.WriteLine(“Week Of Days Saturday”); break;
case 7: Console.WriteLine(“Week Of Days Sunday”); break;
default: Console.WriteLine(“Invalid Entry”);break;
}
}
The while Loop
Ø The while loop construct is used to execute a block of statements for a definite number of times, depending on a condition.
Ø The following is the syntax of the while loop construct:
while (expression)
{
statements;
}
Eg:- Write program to display number from 100 to 1
public void display()
{
int i=0;
while(i
{
Console.WriteLine(“Number “+i);
i++;
}
}
The do…while Loop :
Ø The following figure shows the difference between the do…while and while loop construct.
Ø We can use this loop when we want that loop should execute at least once if the condition is false.
Eg:-
Public void display()
{
int i=0;
do
{
Console.WriteLine(“Number “+i);
i++;
}
while(i
}
The for Loop
Ø The for loop structure is used to execute a block of statements for a specific number of times.
Ø The following is the syntax of the for loop construct:
for (initialization; termination; increment/decrement)
{
statements
}
Execution of a complete for loop construct.
Eg
public void display()
{
for(int i=0;i
{
Console.WriteLine(“Value of i is “+i);
}
}
The break and continue Statements
Ø The break statement is used to exit from the loop and prevents the execution of the remaining loop.
Ø The continue statement is used to skip all the subsequent instructions and take the control back to the loop.
Foreach loop: is useful for traversing each items in an array or a collection of items and displayed one by one.
Syntax
foreach(variable type in collection){
// code block
}
Eg
foreach(int i in array_name)
{
Console.WriteLine(“Value is “+i);
}
Next Chapter-4 on Abstraction and Encapsulation
Kindly click on Follow me for more updates
Kindly share this link to your friends on Facebook,Whats up,Twitter,Google+ .
http://www.technotechmedia.com/2016/07/looping-in-c-chapter-3.html