{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Unit 4 Iteration Notes\n", "Unit 4 Iteration Notes\n", "\n", "- toc: true\n", "- layout: post\n", "- description: Unit 4 Iteration Notes\n", "- categories: [jupyter]\n", "- image: /images/collegeboardlogo.png\n", "- title: Unit 4 Iteration Notes\n", "- author: Rithwikh Varma\n", "- show_tags: true\n", "- comments: true" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "A Boolean expression is a Java expression that returns a Boolean value: true or false\n", "\n", "Rather than implementing mathematical operators such as \"+\" or \"-\", comparative or boolean operators such as \"==\" or \"!\" can be used.\n", "\n", "In Java two if statements could be used if both if statement conditions could be true at the same time.\n", "\n", "Java supplies a primitive data type called Boolean, instances of which can take the value true or false only" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import java.lang.Math;\n", "import java.util.Scanner;\n", "\n", "public class NumberGuess {\n", " private int answer;\n", " private int guess;\n", " Scanner in = new Scanner(System.in);\n", " public NumberGuess() {\n", " this.answer = (int) Math.floor(Math.random()*10 + 1);\n", " System.out.println(answer);\n", " }\n", " public void play() {\n", " while (answer != guess) {\n", " this.getGuess();\n", " }\n", " in.close();\n", " }\n", " public void getGuess() {\n", " System.out.print(\"Guess a number 1-10: \");\n", " this.guess = in.nextInt();\n", " System.out.println(this.guess);\n", " if (this.guess > this.answer) {\n", " System.out.println(\"too high\");\n", " } else if (this.guess < this.answer) {\n", " System.out.println(\"too low\");\n", " } else {\n", " System.out.println(\"congrats\");\n", " }\n", " }\n", " public static void main(String[] args) {\n", " NumberGuess num = new NumberGuess();\n", " num.play();\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Second Homework Assignment\n", "\n", "![Second Assignment](images/Quiz.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For Loop: A loop that iterates through its index" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "3\n", "6\n", "9\n", "12\n", "15\n", "18\n", "21\n", "24\n", "27\n", "30\n", "33\n", "36\n", "39\n", "42\n", "45\n", "48\n", "51\n", "54\n", "57\n", "60\n", "63\n", "66\n", "69\n", "72\n", "75\n", "78\n", "81\n", "84\n", "87\n", "90\n", "93\n", "96\n", "99\n" ] } ], "source": [ "for (int i = 0; i<100; i+=3) {\n", " System.out.println(i);\n", " }\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enhanced For Loop: Used to iterate through arrays." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "93\n", "24\n", "4\n", "3\n", "87\n", "545\n", "76\n", "12\n", "64\n", "30\n" ] } ], "source": [ "int[] arr = {93, 24, 4, 3, 87,545,76,12,64,30};\n", "\n", "for (int i : arr) {\n", " System.out.println(i);\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While Loop: Runs as long as a boolean condition is true and has the boolean condition checked before each iteration is ran\n", "Do While Loop: Similar to a While loop, however, the conditon is only checked after each iterative portion of the code is ran --> the code will execute at least one action." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "8\n", "16\n", "24\n", "32\n", "40\n", "48\n", "56\n", "64\n", "72\n", "80\n", "88\n", "96\n", "104\n", "112\n", "120\n", "128\n", "136\n", "144\n", "152\n", "160\n", "168\n", "176\n", "184\n", "192\n", "200\n", "208\n", "216\n", "224\n", "232\n", "240\n", "248\n", "256\n", "264\n", "272\n", "280\n", "288\n", "296\n", "304\n", "312\n", "320\n", "328\n", "336\n", "344\n", "352\n", "360\n", "368\n", "376\n", "384\n", "392\n", "400\n", "408\n", "416\n", "424\n", "432\n", "440\n", "448\n", "456\n", "464\n", "472\n", "480\n", "488\n", "496\n", "504\n", "512\n", "520\n", "528\n", "536\n", "544\n", "552\n", "560\n", "568\n", "576\n", "584\n", "592\n", "600\n", "608\n", "616\n", "624\n", "632\n", "640\n", "648\n", "656\n", "664\n", "672\n", "680\n", "688\n", "696\n", "704\n", "712\n", "720\n", "728\n", "736\n", "744\n", "752\n", "760\n", "768\n", "776\n", "784\n", "792\n", "800\n", "808\n", "816\n", "824\n", "832\n", "840\n", "848\n", "856\n", "864\n", "872\n", "880\n", "888\n", "896\n", "904\n", "912\n", "920\n", "928\n", "936\n", "944\n", "952\n", "960\n", "968\n", "976\n", "984\n", "992\n", "1000\n", "1008\n", "1016\n", "1024\n", "1032\n", "1040\n", "1048\n", "1056\n", "1064\n", "1072\n", "1080\n", "1088\n", "1096\n" ] } ], "source": [ "int i = 0;\n", "\n", "// printing even numbers with while loop\n", "while (i < 1100) {\n", " System.out.println(i);\n", " i += 8;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nested Loops: Loops placed within each other, creating better iteration, most optimal for 2D arrays" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 9 33 \n", "27 43 94 \n", "4 35 2 \n" ] } ], "source": [ "int[][] arr = {\n", " {6, 9, 33},\n", " {27, 43, 94},\n", " {4, 35, 2}\n", " };\n", " \n", "\n", " for (int i = 0; i