C Sharp Jn (3)
-
Upload
jahanullah -
Category
Technology
-
view
816 -
download
2
Transcript of C Sharp Jn (3)
![Page 1: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/1.jpg)
Software Development Training Program
Zeeshan Hanif
![Page 3: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/3.jpg)
Random Numbers .Net Framework provide a class Random
in System namespace that can be used to generate random numbers.
Random r = new Random(); int a = r.Next(); Console.WriteLine(a); any random number maximum of Int32
size
![Page 4: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/4.jpg)
Random Numbers Methods
Next(); Return an integer grater than or
equal to zero and less than Int32 maximum size.
Next(maxValue);Return an integer grater then or
equal to zero and less then maxValue
![Page 5: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/5.jpg)
Random Numbers Methods
Next(minValue,maxValue);Return an integer grater then
or equal to minValue and less then or equal to maxValue
r.Next();r.Next(10);
r.Next(5,10);
![Page 6: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/6.jpg)
Boolean expressions Expressions that have a boolean
result Relational Operators
== - equal to != - not equal to > - greater than >= - greater than or equal to < - less than <= - less than or equal to
![Page 7: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/7.jpg)
Boolean operators
Boolean AND (&) Boolean OR (|) && and ||
![Page 8: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/8.jpg)
Control Statements
Without control structures C# code executes in a sequential fashion. Every statement executes sequentially and if you want to skip any statement you can not do so.
start
end
Statement 1
Statement 1
Statement 1
![Page 9: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/9.jpg)
If StatementStart
Statements
IF
If block statements
Statements
truefals
e
End
![Page 10: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/10.jpg)
If Conditionpublic void main(string[] args){
int a=10;……Console.WriteLine(“Before if condition”);if(a>10){
Console.WriteLine(“A is greater than 10”);}Console.WriteLine(“After if condition”);……
}
![Page 11: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/11.jpg)
if and elsepublic void main(string[] args){
int a=10;Console.WriteLine(“Before if condition”);if(a>10){
Console.WriteLine(“A is greater than 10”);}else {
Console.WriteLine(“A is less than 10”);}Console.WriteLine(“After if condition”);
}
![Page 12: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/12.jpg)
Nested ‘if’ statementspublic void main(String[] args){
int a = 11, b = 5;if (b<10) { //First if condition Console.WriteLine(“Inside first if”);if(a>10) { //Nested if conditionConsole.WriteLine(“Inside Second if”);}Console.WriteLine(“After second if condition”);}
Console.WriteLine(“After First if condition”);}
![Page 13: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/13.jpg)
Matching Nested if and elsepublic void main(String[] args){
int a=11,b=5;if(b<10) {
Console.WriteLine (“Inside first if”);if(a>10)
Console.WriteLine(“Inside second if”);else
Console.WriteLine(“Else of second if”);}else Console.WriteLine(“Else of first if”);
}
![Page 14: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/14.jpg)
else-ifpublic void main(string[] args){
int a=10;Console.WriteLine(“Before if condition”);if(a>10){
Console.WriteLine(“A is greater than 10”);}else if(a>5){
Console.WriteLine(“A is greater than 5 but less then 10”);}else {
Console.WriteLine(“A is less than 5”); }
Console.WriteLine(“After if condition”);}
![Page 15: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/15.jpg)
Switch statement
Switch statement is ideal for testing a single expression against a series of possible values and executing the code associated with the matching case statement. If any of the case does not match then the optional default statement is executed.
![Page 16: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/16.jpg)
Switch Structure
Condition Statementstrue
false
Statements
Condition Statements
false
true
break
break
![Page 17: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/17.jpg)
Switch statementswitch(op){
case value1:(statements)break;
case value2:(statements)break;
case value3:(statements)break;
default:(statements)break;
}
![Page 18: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/18.jpg)
Switch statementint a = 5;switch(a){
case 2:Console.WriteLine(“Number is 2”);break;
case 5:Console.WriteLine(“Number is 5”);break;
default:Console.WriteLine(“Not Matched”);break;
}
![Page 19: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/19.jpg)
For loop
The for loop provides a means to repeat a section of code a designated number of times. The for loop is structured so that a section of code is repeated until some limit has been reached.
![Page 20: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/20.jpg)
initialize
Condition Statements
false
trueInc / dec
For structure
![Page 21: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/21.jpg)
For loop
public static void main(String[] args){
…..for(<init block> ; <condition> ; <increment>){
<statement(s)>}….
}
![Page 22: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/22.jpg)
Example
public static void main(String[] args){
…..for(int a=0 ; a<10 ; a++){
Console.WriteLine(“A = “+a);}
}Result of a ten times
![Page 23: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/23.jpg)
while Statement
Like the for loop, the while loop has a loop condition that controls the execution of the loop statement. If the boolean condition evaluates to true, the Statement is executed and the process starts over
![Page 24: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/24.jpg)
while Structure
Condition Statements
false
true
![Page 25: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/25.jpg)
While loop
public static void main(String[] args){
…..while(<condition>){
<statement(s)>}….
}
![Page 26: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/26.jpg)
Examplepublic static void main(string[] args){
…..int a = 1;while ( a < 10 ){ Console.WriteLine(a); a++;}…..
}
![Page 27: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/27.jpg)
While loop
The important thing to notice about the while loop is that its condition occurs before the body of the loop Statement. This means that if the condition initially evaluates to false, the Statement is never executed.
![Page 28: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/28.jpg)
do-while Statement
The do-while loop is very similar to the while loop, The major difference between the do-while loop and the while loop is that a do-while loop is guaranteed to execute at least once.
![Page 29: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/29.jpg)
do-while Structure
Statements
Condition
false
true
![Page 30: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/30.jpg)
do-whilepublic static void main(String[] args){
…..do{
<statement(s)>} while(<condition>);….
}
![Page 31: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/31.jpg)
do-while
The Statement is executed initially, and from then on it is executed as long as the condition evaluates to true.
![Page 32: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/32.jpg)
Examplepublic static void main(string[] args){
…..int a = 1;do{ Console.WriteLine(a);
a++;} while ( a < 10 );…..
}
![Page 33: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/33.jpg)
‘break’ and ‘continue’
‘break’: used to terminate and jump out of the loop
‘continue’: jumps to the next iteration of the loop
![Page 34: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/34.jpg)
Example (break)
for(int i = 0 ;i<10;i++){ if(i==5)
break; Console.WriteLine(i);}Result :0,1,2,3,4
![Page 35: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/35.jpg)
Example (continue)
for(int i = 0 ;i<10;i++){ if(i==5)
continue; Console.WriteLine(i);}Result :0,1,2,3,4,6,7,8,9
![Page 36: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/36.jpg)
Nested loopspublic static void main(string[] args){
…..for(<init block> ; <condition> ; <increment>){
for(<init block> ; <condition> ; <increment>)
{<statement(s)>
} //end of inner for loop } //end of outer for loop ….
}
![Page 37: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/37.jpg)
Nested loopsfor(int i=0 ; i<3 ; i++){
for(int j=0 ; j<2 ; j++){
Console.WriteLine(“I = “+i+”, J = “+j);} //end of inner for loop
} //end of outer for loop Result:I = 0, J = 0I = 0, J = 1I = 1, J = 0I = 1, J = 1I = 2, J = 0I = 2, J = 1
![Page 38: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/38.jpg)
Examples
Print the greatest among three nos. Print the table of 2 and 3
simultaneously Factorial of a given no. Finding prime nos.
![Page 39: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/39.jpg)
Examples
Exercise Write an application that creates a random number integer between 0 and 10. Then use a nested if to print out whether that number is between 0 and 2, 3 and 5, 6 and 8, 9 and 10.
![Page 40: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/40.jpg)
Examples
Exercise :Write a program that acts as a calculator for balancing a check book. It should take an initial balance and then a sequence of deposits and withdrawals. When done the program should print the final balance.
![Page 41: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/41.jpg)
Examples Write a program that prints the
following pattern. Your solution must use a loop. * * * * * * * * * * * * * * *
![Page 42: C Sharp Jn (3)](https://reader035.fdocuments.in/reader035/viewer/2022062418/555cdd70d8b42a4f2b8b54c8/html5/thumbnails/42.jpg)
Example Exercise : Write a program that simulates
rolling a pair of dice until the total on the dice comes up to be a given number. The number that you are rolling for is fixed in a variable. The number of times you have to roll the dice is the output of the program. You can assume that the parameter is one of the possible totals: 2, 3, ..., 12. Use your program that computes and prints the number of rolls it takes to get snake eyes. (Snake eyes means that the total showing on the dice is 2.)