LAB 7-OPS

docx

School

Seneca College *

*We aren’t endorsed by this school

Course

102

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

10

Uploaded by ColonelDiscovery13270

Report
LAB 7 Working with Control Flow Structures We will now take some time to understand common logic control structures including the if, if/else , if/elseif/else and switch statements. You will also focus on various methods of testing conditions and “ logic traps ” to avoid that will negatively affect a test condition result. Perform the following Steps: 1. Launch the PowerShell_ISE and confirm that it is running as Administrator by observing the ISE title bar. 2. Open a new script pane, and save it as the filename: logic_ex_1.ps1 3. Let’s start with an if statement. Enter following code block: $CourseCode = “OPS102” if ( $CourseCode -eq “OPS102” ) { write-host “That is the correct course code” } 4. Save your editing changes and then run your script. What was the result? Output: That is the correct course code 5. Modify your script to use the read-host cmdlet to prompt the user for this course’s course code and store the results in your variable $UserGuess . $UserGuess = Read-Host "Please enter the course code:" if ($UserGuess -eq "OPS102") { Write-Host "That is the correct course code" } 6. Modify the test condition to see if $UserGuess equals $CourseCode . $CourseCode = "OPS102" $UserGuess = Read-Host "Please enter the course code:" if ($UserGuess -eq $CourseCode) { Write-Host "That is the correct course code" }
7. Save your editing changes and run the revised script and enter a course code other than OPS102. What happened this time? What does this indicate when using if statements? When we enter a course code other than "OPS102", for example in the following picture IPC144, the script won't print anything because the “if” condition will evaluate to false, and the “Write-Host” command will not be executed. This indicates that when using “if” statements, the script executes different blocks of code based on whether the condition is true or false. If the condition is true, the code inside the “if” block is executed; otherwise, it is skipped. Let’s get some practice testing conditions using Comparison Operators . Refer to the chart on the right-side for reference. You should use and write-down this chart for reference whenever you are using control structures while creating PowerShell scripts. 8. Edit your script to change the test condition to use a logic operator (!): (! $CourseName -eq $UserGuess )
9. Save your editing changed, then run your script and when prompted, again enter a different course code than OSM720. What happened? Why did this happen? When we enter a course code different from "OPS102", the condition evaluates to “false” because the entered course code (IPC144) does not match the expected course code “OPS102". Then, the logical NOT operator “!” negates this “false” value, making the overall condition “true”, which means that the code inside the “if” block will be executed. As a result, the output is: "That is not the correct course code" . 10. Close your current script pane (without making changes) and open a new script pane and save this new file as: logic_ex_2.ps1 Let’s start creating test conditions that compare numbers (as opposed to text), and let’s get practice using the if/else statement: 11. In your new script, enter the code block below: [int] $UserAge = read-host “Enter your age (in years)” if ( $UserAge -ge 65 ) { write-host “You can retire” } else { write-host “Back to work!” }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Notice that the brace for the if code block is on the same line as the test condition (as opposed to a separate line in your previous script. You can use either coding style, but it is recommended to stick with one “style” to be consistent. 12. Save your editing changes, and then run your script and when prompted, enter: 5 What happened this time? Output: Back to work! This is because the age we enter is less than 65 and the "if" condition is not met. The "else" block is executed. 13. Run your script again, and when prompted, enter the string (with double quotes): “80” What is the result? I don’t know, but I think 80 years is a good age to retire… Obviously, something is wrong… What caused the problem is that your shell script allows the user to enter a string . You learned in this lesson to assign strongly-typed variables to specify the data-type that the variable is allowed to store. Let’s fix your shell script; When we run the script and enter the string “80”, PowerShell will attempt to convert the input to an integer because of the type of declaration [int] applied to $UserAge. Since the input is enclosed in double quotes, it is treated as a string, and PowerShell will not implicitly convert it to an integer. Consequently, the script will encounter an error during the conversion process because it can not convert the string “80” to an integer. In addition a wrong " result is displayed "Back to work!, because the statement inside "else" is executed. Using strongly-typed variables helps enforce data type restrictions, ensuring that variables only store values of the specified type.
14. Change the top line of your shell script to read: [int] $UserAge = read-host “Enter your age (in years)” 15. Run your script again, and enter the string (with double quotes): “80” What Happened? You should notice that your script generates an error message – that is better than accepting the wrong data. *You can add “error-checking” to your script using loops that use test conditions to force the user to re-enter an integer instead of a script. Loops have not yet been covered in this course. The script generates an error message. Same as above. 16. Change your test condition in the script to the following: if ( ! $UserAge -ge 65 ) { ***Note: if the syntax above does not work for you, try the following instead: if (!( $UserAge -ge 65 )) { 17. Save and run your script entering the value 80 when prompted. What happened? What is the purpose of using the logical operator: “!”? Output: “Back to work!”. This is because of the logical operator “!’. The purpose of using the logical operator “!” (not) in a condition is to negate the result of the condition. In the original script provided, the condition “!$UserAge -ge 65” is used, which is logically equivalent to “$UserAge -lt 65 (less than 65). So, if the age entered by the user is less than 65, the script will output "You can
retire". However, if the age entered is 65 or greater, it will output "Back to work!". In this case the age is “80” so the script output is “Back to work”. Let’s now look at a “classic example” of using an if/elseif/else control structure: 18. Close your script and open a new script pane, and save it as: logic_ex_3.ps1 19. Enter the following code block: [ int ] $studentGrade = Read-host "Enter a percentage for your course (eg. 75)" if ( $studentGrade -ge 80 ){ $letterGrade = "A" } elseif ( $studentGrade -ge 70 ){ $letterGrade = "B" } elseif ( $studentGrade -ge 60 ) { $letterGrade = "C" } elseif ( $studentGrade -ge 50 ) { $letterGrade = "D" } else { $letterGrade = "F" } Write-host "Your letter grade is: $letterGrade
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
20. Save and run your several times using percentages each time to test like: 85 , 72 , 60 , 50 , and 35 . Did your script work properly for those values? Yes the script works properly. Input: 82 -> Output: Your letter grade is “A” Input: 72 -> Output: Your letter grade is “B” Input: 60 -> Output: Your letter grade is “C” Input: 50 -> Output: Your letter grade is “D” Input: 35 -> Output: Your letter grade is “F” Let’s use a switch logic statement to do the same thing. The example below contains a slight error (on purpose) that you will notice (but we will correct it later) 21. Close your script and open a new script pane, and save it as: YourSenecaID_Lab7_GradeCalculate.ps1 22. Enter the following code block: [ int ] $studentGrade = Read-host "Enter a percentage for your course (eg. 75)" switch ( $studentGrade ){ { $studentGrade -ge 80 } { $letterGrade = "A" } { $studentGrade -ge 70 } { $letterGrade = "B" } { $studentGrade -ge 60 } { $letterGrade = "C" }
{ $studentGrade -ge 50 } { $letterGrade = "D" } default { $letterGrade = "F" } } Write-host "Your letter grade is: $letterGrade 23. Save and run your script for the percentage: 85 Did it work? What is wrong? The script doesn’t work. The problem is the missing of “break” keyword to the end of each case. If one of the "switch" conditions is fulfilled, the execution is not stopped to display the corresponding grade, but the execution of the cases continues until the end of the program. The problem is that the “ break ” keyword has NOT been added to the end of the action taken for each case, and therefore, it travelled down assigning next a letter grade of B , and then the letter grade C, until the last case the letter grade: D . To prevent this from occurring, you include the break keyword as the last instruction for each case. You are not required to include the break keyword for the default case. For single line commands, you can use a semi-colon to separate commands. 24. Add in a break statement for each of the cases displayed below (including semicolon). If done properly, your code should look identical to what is displayed below: { $studentGrade -ge 80 } { $letterGrade = "A" ; break } { $studentGrade -ge 70 } { $letterGrade = "B" ; break } { $studentGrade -ge 60 } { $letterGrade = "C" ; break } { $studentGrade -ge 50 } { $letterGrade = "D" ; break }
25. Save and run your several times using percentages each time to test like: 85 , 72 , 60 , 50 , and 35 . Did the program work correctly? Which statement is more efficient (less typing) for this script: if/elseif/else or switch ? Input: 85 -> Output: Your letter grade is “A” Input: 72 -> Output: Your letter grade is “B” Input: 60 -> Output: Your letter grade is “C” Input: 50 -> Output: Your letter grade is “D” Input: 35 -> Output: Your letter grade is “F” The program works correctly, assigning the appropriate letter grades for each percentage input. In this case there are multiple conditions, based on the same variable “studentGrade”. In terms of typing efficiency, the “switch” statement is generally more concise and requires less typing compared to a series of “if/elseif/else” statements. With “switch”, we list all the conditions and their corresponding actions in a single block. This reduces the need to repeatedly type the variable being evaluated for each condition, making the code more compact. The “switch” statement is easier to read and maintain. Furthermore “switch” inherently supports a default case, which can handle cases not explicitly covered by other conditions. With “if/elseif/else”, we would need to add an additional “else” block for default handling. 26. After your saving changes and running your script, take a screenshot of PowerShell ISE showing BOTH your script and the output of the script and PASTE YOUR SCREENSHOT HERE .
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help