Like this presentation? Why not share!

# C Sharp Jn (3)

## by jahanullah on May 14, 2009

• 858 views

### Views

Total Views
858
Views on SlideShare
857
Embed Views
1

Likes
1
20
0

### 1 Embed1

 http://www.slideshare.net 1

### Categories

Uploaded via SlideShare as Microsoft PowerPoint

## C Sharp Jn (3)Presentation Transcript

• Software Development Training Program Zeeshan Hanif
• 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
• 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
• 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);
• 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
• Boolean operators
• Boolean AND (&)
• Boolean OR (|)
• && and ||
• 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
• If Statement Start Statements IF If block statements Statements true false End
• If Condition
• public 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”);
• ……
• }
• if and else
• public 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”);
• }
• Nested ‘if’ statements
• public 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 condition
• Console.WriteLine(“Inside Second if”);
• }
• Console.WriteLine(“After second if condition”);
• }
• Console.WriteLine(“After First if condition”);
• }
• Matching Nested if and else
• public 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”);
• }
• else-if
• public 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”);
• }
• 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.
• Switch Structure Condition Statements true false Statements Condition Statements false true break break
• Switch statement
• switch( op )
• {
• case value1 :
• (statements)
• break;
• case value2 :
• (statements)
• break;
• case value3 :
• (statements)
• break;
• default:
• (statements)
• break;
• }
• Switch statement
• int 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;
• }
• 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.
• initialize Condition Statements false true Inc / dec For structure
• For loop
• public static void main(String[] args)
• {
• … ..
• for( < init block> ; < condition> ; < increment> )
• {
• <statement(s)>
• }
• … .
• }
• Example
• public static void main(String[] args)
• {
• … ..
• for( int a=0 ; a<10 ; a++ )
• {
• Console.WriteLine(“A = “+a);
• }
• }
• Result of a ten times
• 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 e valuates to true, the Statement is executed and the process starts over
• while Structure Condition Statements false true
• While loop
• public static void main(String[] args)
• {
• … ..
• while( < condition> )
• {
• <statement(s)>
• }
• … .
• }
• Example
• public static void main(string[] args)
• {
• … ..
• int a = 1; while ( a < 10 ) { Console.WriteLine(a);
• a++; }
• … ..
• }
• 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.
• 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.
• do-while Structure Statements Condition false true
• do-while
• public static void main(String[] args)
• {
• … ..
• do
• {
• <statement(s)>
• }
• while( < condition> );
• … .
• }
• do-while
• The Statement is executed initially, and from then on it is executed as long as the condition evaluates to true.
• Example
• public static void main(string[] args)
• {
• … ..
• int a = 1; do
• { Console.WriteLine(a);
• a++; } while ( a < 10 );
• … ..
• }
• ‘ break’ and ‘continue’
• ‘ break’: used to terminate and jump out of the loop
• ‘ continue’: jumps to the next iteration of the loop
• Example (break)
• for(int i = 0 ;i<10;i++){
• if(i==5)
• break ;
• Console.WriteLine(i);
• }
• Result :
• 0,1,2,3,4
• 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
• Nested loops
• public 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
• … .
• }
• Nested loops
• for( 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 = 0
• I = 0, J = 1
• I = 1, J = 0
• I = 1, J = 1
• I = 2, J = 0
• I = 2, J = 1
• Examples
• Print the greatest among three nos.
• Print the table of 2 and 3 simultaneously
• Factorial of a given no.
• Finding prime nos.
• 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.
• 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.
• Examples
• Write a program that prints the following pattern. Your solution must use a loop.
• * * * * * * * * * * * * * * *
•
• 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.)