Published on


Published in: Technology, Health & Medicine
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Software Development Training Program Mr. Zia Khan, and Mr. Zeeshan Hanif
  2. 2. DotNet-101 Lecture 4 Zeeshan Hanif [email_address] [email_address]
  3. 3. Random Numbers <ul><li>.Net Framework provide a class Random in System namespace that can be used to generate random numbers. </li></ul><ul><li>Random r = new Random(); </li></ul><ul><li>int a = r.Next(); </li></ul><ul><li>Console.WriteLine(a); </li></ul><ul><li>any random number maximum of Int32 size </li></ul>
  4. 4. Random Numbers <ul><li>Methods </li></ul><ul><li>Next(); </li></ul><ul><li>Return an integer grater than or equal to zero and less than Int32 maximum size. </li></ul><ul><li>Next(maxValue); </li></ul><ul><li>Return an integer grater then or equal to zero and less then maxValue </li></ul>
  5. 5. Random Numbers <ul><li>Methods </li></ul><ul><li>Next(minValue,maxValue); </li></ul><ul><li>Return an integer grater then or equal to minValue and less then or equal to maxValue </li></ul><ul><li>r.Next(); </li></ul><ul><li>r.Next(10); </li></ul><ul><li>r.Next(5,10); </li></ul>
  6. 6. Boolean expressions <ul><li>Expressions that have a boolean result </li></ul><ul><li>Relational Operators </li></ul><ul><ul><li>== - equal to </li></ul></ul><ul><ul><li>!= - not equal to </li></ul></ul><ul><ul><li>> - greater than </li></ul></ul><ul><ul><li>>= - greater than or equal to </li></ul></ul><ul><ul><li>< - less than </li></ul></ul><ul><ul><li><= - less than or equal to </li></ul></ul>
  7. 7. Boolean operators <ul><li>Boolean AND (&) </li></ul><ul><li>Boolean OR (|) </li></ul><ul><li>&& and || </li></ul>
  8. 8. Control Statements <ul><li>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. </li></ul>start end Statement 1 Statement 1 Statement 1
  9. 9. If Statement Start Statements IF If block statements Statements true false End
  10. 10. If Condition <ul><li>public void main(string[] args) </li></ul><ul><li>{ </li></ul><ul><li>int a =10; </li></ul><ul><li>…… </li></ul><ul><li>Console.WriteLine(“Before if condition”); </li></ul><ul><li>if(a>10) </li></ul><ul><li>{ </li></ul><ul><li>Console.WriteLine(“A is greater than 10”); </li></ul><ul><li>} </li></ul><ul><li>Console.WriteLine(“After if condition”); </li></ul><ul><li>…… </li></ul><ul><li>} </li></ul>
  11. 11. if and else <ul><li>public void main(string[] args) </li></ul><ul><li>{ </li></ul><ul><li>int a =10; </li></ul><ul><li>Console.WriteLine(“Before if condition”); </li></ul><ul><li>if(a>10){ </li></ul><ul><li>Console.WriteLine(“A is greater than 10”); </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>Console.WriteLine(“A is less than 10”); </li></ul><ul><li>} </li></ul><ul><li>Console.WriteLine(“After if condition”); </li></ul><ul><li>} </li></ul>
  12. 12. Nested ‘if’ statements <ul><li>public void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>int a = 11, b = 5; </li></ul><ul><li>if (b<10) { //First if condition </li></ul><ul><li>Console.WriteLine(“Inside first if”); </li></ul><ul><li>if(a>10) { //Nested if condition </li></ul><ul><li>Console.WriteLine(“Inside Second if”); </li></ul><ul><li>} </li></ul><ul><li>Console.WriteLine(“After second if condition”); </li></ul><ul><li>} </li></ul><ul><li>Console.WriteLine(“After First if condition”); </li></ul><ul><li>} </li></ul>
  13. 13. Matching Nested if and else <ul><li>public void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>int a=11,b=5; </li></ul><ul><li>if(b<10) { </li></ul><ul><li>Console.WriteLine (“Inside first if”); </li></ul><ul><li>if(a>10) </li></ul><ul><li>Console.WriteLine(“Inside second if”); </li></ul><ul><li>else </li></ul><ul><li>Console.WriteLine(“Else of second if”); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li> Console.WriteLine(“Else of first if”); </li></ul><ul><li>} </li></ul>
  14. 14. else-if <ul><li>public void main(string[] args) </li></ul><ul><li>{ </li></ul><ul><li>int a =10; </li></ul><ul><li>Console.WriteLine(“Before if condition”); </li></ul><ul><li>if(a>10){ </li></ul><ul><li>Console.WriteLine(“A is greater than 10”); </li></ul><ul><li>} </li></ul><ul><li>else if(a>5){ </li></ul><ul><li>Console.WriteLine(“A is greater than 5 but less then 10”); </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>Console.WriteLine(“A is less than 5”); </li></ul><ul><li>} </li></ul><ul><li>Console.WriteLine(“After if condition”); </li></ul><ul><li>} </li></ul>
  15. 15. Switch statement <ul><li>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. </li></ul>
  16. 16. Switch Structure Condition Statements true false Statements Condition Statements false true break break
  17. 17. Switch statement <ul><li>switch( op ) </li></ul><ul><li>{ </li></ul><ul><li>case value1 : </li></ul><ul><li>(statements) </li></ul><ul><li>break; </li></ul><ul><li>case value2 : </li></ul><ul><li>(statements) </li></ul><ul><li>break; </li></ul><ul><li>case value3 : </li></ul><ul><li>(statements) </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>(statements) </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul>
  18. 18. Switch statement <ul><li>int a = 5; </li></ul><ul><li>switch(a) </li></ul><ul><li>{ </li></ul><ul><li>case 2: </li></ul><ul><li>Console.WriteLine(“Number is 2”); </li></ul><ul><li>break; </li></ul><ul><li>case 5: </li></ul><ul><li>Console.WriteLine(“Number is 5”); </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>Console.WriteLine(“Not Matched”); </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul>
  19. 19. For loop <ul><li>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. </li></ul>
  20. 20. initialize Condition Statements false true Inc / dec For structure
  21. 21. For loop <ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>… .. </li></ul><ul><li>for( < init block> ; < condition> ; < increment> ) </li></ul><ul><li>{ </li></ul><ul><li><statement(s)> </li></ul><ul><li>} </li></ul><ul><li>… . </li></ul><ul><li>} </li></ul>
  22. 22. Example <ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>… .. </li></ul><ul><li>for( int a=0 ; a<10 ; a++ ) </li></ul><ul><li>{ </li></ul><ul><li>Console.WriteLine(“A = “+a); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Result of a ten times </li></ul>
  23. 23. while Statement <ul><li>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 </li></ul>
  24. 24. while Structure Condition Statements false true
  25. 25. While loop <ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>… .. </li></ul><ul><li>while( < condition> ) </li></ul><ul><li>{ </li></ul><ul><li><statement(s)> </li></ul><ul><li>} </li></ul><ul><li>… . </li></ul><ul><li>} </li></ul>
  26. 26. Example <ul><li>public static void main(string[] args) </li></ul><ul><li>{ </li></ul><ul><li>… .. </li></ul><ul><li>int a = 1; while ( a < 10 ) { Console.WriteLine(a); </li></ul><ul><li> a++; } </li></ul><ul><li>… .. </li></ul><ul><li>} </li></ul>
  27. 27. While loop <ul><li>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. </li></ul>
  28. 28. do-while Statement <ul><li>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. </li></ul>
  29. 29. do-while Structure Statements Condition false true
  30. 30. do-while <ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>… .. </li></ul><ul><li>do </li></ul><ul><li>{ </li></ul><ul><li><statement(s)> </li></ul><ul><li>} </li></ul><ul><li>while( < condition> ); </li></ul><ul><li>… . </li></ul><ul><li>} </li></ul>
  31. 31. do-while <ul><li>The Statement is executed initially, and from then on it is executed as long as the condition evaluates to true. </li></ul>
  32. 32. Example <ul><li>public static void main(string[] args) </li></ul><ul><li>{ </li></ul><ul><li>… .. </li></ul><ul><li>int a = 1; do </li></ul><ul><li>{ Console.WriteLine(a); </li></ul><ul><li>a++; } while ( a < 10 ); </li></ul><ul><li>… .. </li></ul><ul><li>} </li></ul>
  33. 33. ‘ break’ and ‘continue’ <ul><li>‘ break’: used to terminate and jump out of the loop </li></ul><ul><li>‘ continue’: jumps to the next iteration of the loop </li></ul>
  34. 34. Example (break) <ul><li>for(int i = 0 ;i<10;i++){ </li></ul><ul><li>if(i==5) </li></ul><ul><li>break ; </li></ul><ul><li>Console.WriteLine(i); </li></ul><ul><li>} </li></ul><ul><li>Result : </li></ul><ul><li>0,1,2,3,4 </li></ul>
  35. 35. Example (continue) <ul><li>for(int i = 0 ;i<10;i++){ </li></ul><ul><li>if(i==5) </li></ul><ul><li>continue ; </li></ul><ul><li>Console.WriteLine(i); </li></ul><ul><li>} </li></ul><ul><li>Result : </li></ul><ul><li>0,1,2,3,4,6,7,8,9 </li></ul>
  36. 36. Nested loops <ul><li>public static void main(string[] args) </li></ul><ul><li>{ </li></ul><ul><li>… .. </li></ul><ul><li>for( < init block> ; < condition> ; < increment> ) </li></ul><ul><li>{ </li></ul><ul><li>for( < init block> ; < condition> ; < increment> ) </li></ul><ul><li>{ </li></ul><ul><li><statement(s)> </li></ul><ul><li>} //end of inner for loop </li></ul><ul><li>} //end of outer for loop </li></ul><ul><li>… . </li></ul><ul><li>} </li></ul>
  37. 37. Nested loops <ul><li>for( int i=0 ; i<3 ; i++ ) </li></ul><ul><li>{ </li></ul><ul><li>for( int j=0 ; j<2 ; j++ ) </li></ul><ul><li>{ </li></ul><ul><li>Console.WriteLine(“I = “+i+”, J = “+j); </li></ul><ul><li>} //end of inner for loop </li></ul><ul><li>} //end of outer for loop </li></ul><ul><li>Result: </li></ul><ul><li>I = 0, J = 0 </li></ul><ul><li>I = 0, J = 1 </li></ul><ul><li>I = 1, J = 0 </li></ul><ul><li>I = 1, J = 1 </li></ul><ul><li>I = 2, J = 0 </li></ul><ul><li>I = 2, J = 1 </li></ul>
  38. 38. Examples <ul><li>Print the greatest among three nos. </li></ul><ul><li>Print the table of 2 and 3 simultaneously </li></ul><ul><li>Factorial of a given no. </li></ul><ul><li>Finding prime nos. </li></ul>
  39. 39. Examples <ul><li>Exercise </li></ul><ul><li>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. </li></ul>
  40. 40. Examples <ul><li>Exercise : </li></ul><ul><li>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. </li></ul>
  41. 41. Examples <ul><li>Write a program that prints the following pattern. Your solution must use a loop. </li></ul><ul><li>* * * * * * * * * * * * * * * </li></ul><ul><li>  </li></ul>
  42. 42. Example <ul><li>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.) </li></ul>