Loops and Conditional Statements
Loops and Conditional Statements
Statements
Conditional
Statements
In our daily life we do many things which depends on some kind of conditions for e.g.
If I study I will pass exams. If he is hungry he will eat etc.
So in programming languages also we can perform different sets of actions
depending on the circumstances.
There are three major decision making instructions:
If statement.
If else statement.
Switch statement.
If
statement
False
Condition
true
Statement
If
example
Code:
main( )
int num ;
printf ( “Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( “Nice Choice !" ) ;
}
If else
Statement
False
Condition 2nd Statement
True
1st Statement
If else
Example
We can write an entire if-else construct within either the body of the if
statement or the body of an else statement. This is called ‘nesting‘ of ifs.
Syntax:
if (expression)
{
if (expression) Authorization Authentication
{
statement;
}
else
{
statement;
}
} else
statement;
Exampl
e
Code:
if (first == second)
{ Console.WriteLine(“These two numbers are equal.”);
}
else
{
if (first > second)
{ Console.WriteLine(“The first number is bigger.”);
}
else
{
Console.WriteLine(“The second is bigger.”);
}
}
If ..
Else if
When you have multiple expressions to evaluate, you can use the if. Else if- else form of
the if statement.
Exampl
e
Code:
main( )
{
int m1, m2, m3, m4, m5, per ;
per = ( m1+ m2 + m3 + m4+ m5 ) / per ;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}
Switc
h
Another form of selection statement is the switch statement, which executes a set of logic depending
on the value of a given parameter.
It enables a program to select among several alternatives.
It works like this: The value of an expression is successively tested against a list of constants.
When a match is found, the statement sequence associated with that match is executed.
Switc
h
Switch Syntax:
switch(expression)
{
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default:
statement sequence
break;
}
Switch
Example
{
void Main(string[] args)
e
int a = 100; int b = 200;
switch (a)
{
}
case 100:
Console.WriteLine("Exact value of a is : {0}", a);
Console.WriteLine("This is part of outer switch "); switch (b)
Console.WriteLine("Exact value of b is : {0}", b);
{
Console.ReadLine();
case 200:
}
Console.WriteLine("This is part of inner switch "); break;
}
break;
Output:
This is part of outer switch
This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200
What is
loop?
A while loop will check a condition and then continues to execute a block of code as
long as the condition evaluates to a Boolean value of true or false.
while (Condition)
{
statements ;
}
Basic Working of While
Loop
false
Condition
true
Statement
While Loop
Example
Code:
int counter = 0;
while (counter < 10)
{
Console.WriteLine("Number : {0}", counter);
counter++;
}
Output:
It will print Numbers from 0 to 9.
Nested While
Loop
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
Nested While Loop
Example
Code: Output:
int i = 0; Value of i: 0
while (i < 2) Value of j: 1
{
Console.WriteLine("Value of i: {0}", i); Value of i: 1
int j = 1; Value of j: 1
i++;
while (j < 2)
{
Console.WriteLine("Value of j: {0}", j); j+
+;
}
}
Do While
Loop
Statement
True
Condition
False
Do While
Example
Code: Output:
class Program 12 x 1 = 12
{ 12 x 2 = 24
static void Main(string[] args) 12 x 3 = 36
{ 12 x 4 = 48
int table,i,res; 12 x 5 = 60
table=12; i=1; 12 x 6 = 72
do
12 x 7 = 84
{
res = table * 12 x 8 = 92
I; 12 x 9 = 108
Console.WriteLine("{0} x {1} = {2}", table, i, res); i+ 12 x 10 = 120
+;
}
while (i <= 10);
Console.ReadLine();
}
}
For
Loop
A for loop works like a while loop, except that the syntax of the for loop includes
initialization and condition modification.
for loops are appropriate when you know exactly how many times you want to
perform the statements within the loop.
The contents within the for loop parentheses hold three sections separated
by semicolons
for (<initializer list>; <Boolean expression>; <iterator list>)
{
<statements>
}
For Loop
Structure
Code: Output:
For( int i= 0; i<8 ; i++) 0
1
{
2
Console.WritleLine(i);
3
}
4
5
6
7
Nested For
Loop
Code:
int n = int.Parse(Console.ReadLine());
for(int row = 1; row <= n; row++) Output:
{ 1
for(int column = 1; column <= row;
12
column++)
{ ....