Advertisement
fosterbl

L9ForLoopsDay2.java

Oct 7th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //Scope
  4. //Nested for loops
  5. //For loops to while loops
  6. //Strings
  7. public class L9ForLoopsDay2{
  8.    public static void main(String[] args){
  9.       //Declaring a variable inside of an if-statement, loop, or any block of code { }
  10.       //limits that variable to existing inside of that block of code ONLY
  11.       //The examples below are illegal. Try uncommenting them and running them to see
  12.       // //Ex1
  13. //       int a = 4;
  14. //       if( a >= 3 ){
  15. //          int b = 5;
  16. //       }
  17. //       System.out.println( b );
  18. //       //Ex2
  19. //       for(int i = 0; i < 3; i++){
  20. //          System.out.println("hi");
  21. //       }
  22. //       System.out.println( i );
  23.  
  24. //       //Nested For Loop example
  25. //       System.out.println( "i\tj" );
  26. //       for( int i = 1; i <= 2; i++ ){
  27. //          for( int j = 1; j <= 3; j++ ){//this loop is inside and happens more often
  28. //             System.out.println( i + "\t" + j );//see that j changes more often and when j goes past 3, i gets incremented and inner loop starts again
  29. //          }
  30. //       }
  31.  
  32.       //Print a 10 x 10 box of *
  33.       //or any of the other options in #18 on http://www.beginwithjava.com/java/loops/questions.html
  34.      
  35. //       //For loop to While loop
  36. //       for( int k = 0; k < 9; k++ ){
  37. //          System.out.println( k );
  38. //       }
  39. //      
  40. //       int k = 0;//initialization expression goes before
  41. //       while( k < 9 ){//control expression goes inside
  42. //          System.out.println( k );
  43. //          k++;//step expression goes at bottom of loop
  44. //       }
  45.  
  46. //       //Loop over the characters in a String
  47. //       Scanner kb = new Scanner(System.in);
  48. //       System.out.print("Enter a word: ");
  49. //       String word = kb.next();
  50. //       for(int i = 0; i < word.length(); i++){//start loop @ index 0, go up until but not including the length, count by 1
  51. //          String letter = word.substring(i, i+1);//substring(i,i+1) gets one letter at a time (first thing you want, first thing you don't want)
  52. //          System.out.println( letter );
  53. //       }
  54.  
  55.       //Write code to find how many vowels the user's name contains. The user inputs their name from the keyboard
  56.    }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement