Beruflich Dokumente
Kultur Dokumente
3.Conditional Statements in C#
Conditional statements are used when we want to execute a certain action based on the condition. It
means decision making statements require a few conditions, if the condition evaluates as true certain
statements executed otherwise some other statements gets executed. The different conditional
statements are following
If statement
If(condition)
{
statements executed if condition evaluates to true.
}
e.g.
int a = 10, b= 10;
if(a==b)
{
Console.WriteLine(“Both are equal”); // if a equals to b
}
If Else Statement
It is basically an “if” statement with an optional “else” statement if the condition evaluates to false.
If(condition)
{
Statements executed if condition is true.
}
else
{
Statements executed if the condition is false.
}
e.g.
int a=10, b= 20;
if (a == b)
{
Console.WriteLine(“Print Both are Equal”); // if condition is true a equals b
}
else
{
Console.WriteLine(“Print Both are not equal”); // if condition is false
}
If Else If Statement:
Similar to if else statement but with else if statement we can check multiple conditions. That is each
“else if” represents a separate condition.
If(condition1)
{
Statements executed if condition1 is true.
}
else if(condition2)
{
Statements executed if condition2 is true.
}
else
{
Statements executed if both conditions are false.
}
Nested If statements
Nested means making conditions inside other conditions. That is using “if” or “else if” inside another if
statements.
Int num = 30;
If(num > 10)
{
If(num == 20)
{
Console.WriteLine (“Print num is 20”);
}
Else
{
Console.WriteLine (“Print num is not 20”);
}
}
else
{
Print num is not greater than 10;
}
4. Difference between if and switch statement
In case of multi-conditional processing, basically there come two statements in thought they are switch
case and else if ladder. You can do whatever you want to do with both these statements but using
switch statement is an elegant solution to if-else if and nested if.
The if statements runs for a true value i.e. if you something get displayed on the screen you can simply
use it with any valid arguments, you can use it in looks for break, continue functions simply, but when
you go with switch, it just compares cases and executes the statement under the case provided by
switch expression. We can put a range expression while using if statements, but when we go with
switch, we can’t use range of an expression, we need to provide a single expression whatever it is
either integer etc.,
“If” is difficult to understand if number of conditions are more. The switch statement is easy to edit as
it has created the separate cases for different statements.
If(condition1)
{
Body of if
}
else if (condition2)
{
Body
}
else if(condition3)
{
Body
}
----------------more conditions----
Switch(expression)
{
Case1: statements; // if expression matches with the case1
Break;
Case2: statements; // if expression matches with the case1
Break;
Default:
Statements;// if no case match
}
5. Array and its types.
We can define Array as a collection of similar type of values which are stored in sequential order i.e.,
they stored in a contiguous memory location.
C# supports 2 types of arrays. They are
1. Single dimensional array
2. Multi-dimensional array
In the Single dimensional array, the data is arranged in the form of a row whereas in the Multi-
dimensional arrays the data is arranged in the form of rows and columns. Again, the multi-dimensional
arrays are of two types
Jagged array: whose rows and columns are not equal
Rectangular array: whose rows and columns are equal
We can access the values of an array using the index positions whereas the array index starts from 0
which means the first item of an array will be stored at 0th position and the position of the last item of
an array will be the total number of the item – 1.
Syntax:
// single-dimensional array of 10 integers.
int[] array1 = new int[10];
// Setting the values of the first array in the jagged array structure.
jarray[0] = new int[4] { 1, 2, 3, 4 }
jarray[1] = new int[3] { 5,6,7 }
6.Jagged Array
A jagged array can be defined as an array consisting of arrays. They are used to store arrays instead of
other data types. It is initialized by using two square brackets, where the first square bracket denotes
the size of the array that is being defined and the second bracket denotes the array dimension that is
going to be stored inside the jagged array.
Jagged Array Declaration
string[ ][ ] stringArr = new string[2][ ];
A jagged array can store multiple arrays with different lengths.
arrayJag[0] = new string [2] ;
arrayJag[1] = new string [3] ;
In the above example, we have initialized a string type jagged array with index “0” and “1” containing
an array of size defined inside the square bracket. The 0th index contains a string type array of length 2
and the index “1” contains a string type array of length 3.
Initialization at the time of declaration
arrayJag[0] = new string [2] {“apple”, “mango”};
arrayJag[1] = new string [3] {“orange”, “banana”, “guava”};
Finally we can say that a Jagged array is unique in itself as it stores arrays as values. These arrays are
quite similar to other arrays with the only difference being the type of value it stores.
7.Static and Non static Members
A class is a collection of members like variables, methods, Constructors etc. These
members will be of two different categories.
1) Non-static (or) Instance Methods
2) Static members
The members of a class which requires the object of a class for initialization or execution are known as
Instance members where as members of a class which do not require object of a class for initialization
or execution are known as static members.
Instance variables Vs Static Variables
1) A variable under a static block or a variable declared using static modifier are static variables, rest of
all are instance variables only.
e.g.
class Sample{
int x = 100; //instance variable
statc int y = 100; // static variable
static void Main()
{
int z=100; // static variable
Sample obj = new Sample(); // object creation
Console.WriteLine(obj.x) // accessing instance variable through object;
Console.WriteLine(Sample.y) // accessing static variable using class name
}
}
2)The initialization of a static variable gets done, if we execute the class in which the variable is
declared whereas instance variable gets initialized after creating the object of class either within the
class or in other class also.
3) The minimum and maximum number of times a static variable gets initialized will be one and only
one time whereas in 'n' case of instance it will be '0' if no objects are created or 'n' if 'n' objects are
created.
Note: -To access an instance member we use object of the class whereas to access static members we
use name of the class.
C# has a so many operators such as Arithmetic operators, Relational operators, Assignment operators,
Logical operators, Unary operators, etc. They can be used to evaluate a variable or perform an
operation on a variable to make a proper expression.
Relational Operators:
Any relation between the two operands is validated by using relational operators. Relational operators
return Boolean values. If the relation between two operands is successfully validated then it will return
“true” and if the validation fails then “false” will be returned. Relational operators are mainly used in
decision making or for defining conditions for loops.
Greater than operator: (denoted by “>”): Validates greater than the relation between operands.
Less than operator: (denoted by “<“): Validates less than the relation between operands.
Equals To operator: (denoted by “==”): Validates the equality of two operands.
Greater than or equals to (denoted by “>=”): Validates greater than or equals to the relation between
the two operands.
Less than or equals to (denoted by “<=”): Validates less than or equals to the relations between the
two operands.
Not equal: (denoted by “!=”): Validates not an equal relationship between the two operands.
int a = 10;
int b = 5;
bool validate;
validate = a > b; //1
Console.WriteLine(validate);
validate = a < b; //2
Console.WriteLine(validate);
validate = a == b; //3
Console.WriteLine(validate);
validate = a >= b; //4
Console.WriteLine(validate);
validate = a <= b; //5
Console.WriteLine(validate);
validate = a != b; //6
Console.WriteLine(validate);
Logical Operators
Logical operators are used for performing logical operations. Logical operators work with Boolean
expressions and return a Boolean value. Logical operators are used with the conditional operators in
loops and decision-making statements.
1) Logical AND Operator
Symbol: “&&”
AND operator returns true when both the values are true. If any of the value is false then it will return
false.
For example, A && B will return true if both A and B are true, if any or both of them are false then it
will return false.
2)Logical OR Operator
Symbol: “||”
OR operator returns true if any of the condition/operands is true. It will return false when both of the
operands are false.
For example, A || B returns true if the value of either of A or B is true. It will return false if both A and
B have false values.
3)Logical NOT Operator
Symbol: “!”
NOT operator is used to reverse the logical conclusion of any condition. If the condition is true then it
will return false and if the condition is false then it will return true.
Example, !(A||B) returns false if “A||B” returns true and will return true if “A||B” returns false.
B-2) Write an algorithm to find out whether given string is palindrome or not?
function palindrome(string)
start = 0
end = length of string - 1
while start less than end
if string[start] == string[end]
increment start by 1
decrement end by 1
else
return False
return True
C-1) Write a program to copy values from one array to another array using a loop?
using System;
namespace ArraysPractice
{
class CopyingArray
{
static void Main()
{
Console.WriteLine("Enter the Elements in Array ");
// Taking the array elements as a string in a single line separated with space
// converting each element into integer and copying into new array.
for (int i = 0; i < sarr1.Length; i++)
{
arr1[i] = int.Parse(sarr1[i]);
}
// Printing the copied array
for (int i = 0; i < arr1.Length; i++)
{
Console.Write(arr1[i] + " ");
}
}
}
}
output
Enter the Elements in Array
5734189
The new array is
5734189
Press any key to continue . . .
C-2) Write a function with reference arguments to reverse the given number?
using System;
namespace Practice
{
class ReverseNumber
{
static void ReverseNum(ref int num)
{
int sum = 0;
while(num > 0)
{
sum = sum * 10 + num % 10;
num /= 10;
}
num = sum;
}
static void Main()
{
Console.WriteLine("Enter a number ");
int number = int.Parse(Console.ReadLine());
ReverseNum(ref number); // passing reference of a variable
Console.WriteLine(number);
}
}
}
Enter a number
2456
6542
Press any key to continue . . .
C-3)Write a program using do..while to implement a menu drive program? .
using System;
namespace Practice
{
class Class1
{
static void Main()
{
int choice, n1=0 , n2=0 ;
do
{
Console.WriteLine("=== Menu ===\n");
Console.WriteLine("1. Addition\n");
Console.WriteLine("2. Subtraction\n");
Console.WriteLine("3. Exit\n");
Output:
THE END