Ch14-Decision Control Structure Explained A Simple Example of Decision Control ■ Problem: – Compare two numbers and if they are equal, display YES and if they are not equal, display NO. ■ Pseudocode:...

1 answer below »
This online test will be about Applied programming Logic, It is related with flowchart, psudocode, shell scripting etc.


Ch14-Decision Control Structure Explained A Simple Example of Decision Control ■ Problem: – Compare two numbers and if they are equal, display YES and if they are not equal, display NO. ■ Pseudocode: The following code will output “Yes” if the two numbers are equal and “No” if they are not. Take the first number from the user Take the second number from the user if [ $number 1==$number 2] then print response “True” else print response “FALSE” fi A Simple Example of Decision Control ■ Flowchart: ■ Shell Script: #!/bin/bash echo "Please enter the first number" read first echo "Please enter the second number" read second if [ $first == $second ] then echo "YES" else echo "NO" fi Problem A ■ Allow the user to input a script name and determine the permissions of the given file. ■ We will use the if-then-fi decision making structure in this program. ■ For determining the permission of a file, we will use the file-test operators. ■ The syntax of the if-then-fi structure is: if [expression]; then code if 'expression' is true. fi ■ Note that the ‘expression’ will get executed only if the if condition evaluates to true. ■ (…) parentheses indicate a subshell. They contain a list of commands and not an expression like in many other languages. ■ ((…)) double parentheses contain arithmetic instructions. ■ [ … ] single brackets are used for conditional expressions. ■ [[ … ]] double brackets form an for conditional expressions, with a few additional features Problem A The following code will read the file name and display the permissions it has: Take the file name from the user if [ -e $filename ] then print response “file exists” if [ -r $filename ] then attribute the status=$status"readable " fi if [ -w $filename ] then attribute the status=$status"writable " fi if [ -x $filename ] then attribute the status=$status"executable" fi print response "file permission: "$status else print response "The file does not exist" fi Problem A #!/bin/bash cd ls -l read -p "Enter a file name: " file_name if [ -e $file_name ] then echo "file exists!" if [ -r $file_name ] then status="readable " fi if [ -w $file_name ] then status=$status"writable " fi if [ -x $file_name ] then status=$status"executable" fi echo "file permission: "$status else echo "file does not exist" fi Problem B ■ Input the marks of a student and check if marks are greater or equal to 80 then print “Very Good”. If marks are less than 80 and greater or equal to 50 then print “good” and so on. ■ In this example, we need to make a correct decision from multiple conditions. ■ We will use the if-elif-fi statement, which is the advanced control statement in Shell. ■ We can use if-elif-fi when we want to execute one out of many blocks of code. ■ We will use && to test more than one conditional expression at one time. ■ It will check expression 1 and execute statement 1 if it is true. ■ If expression 1 is false, it will check expression 2 and execute its statement, and so on. Problem B The following program will input the marks of a student and check if marks are greater or equal to 80 then print “Very Good”. If marks are less than 80 and greater or equal to 50 then print “good” and so on. Take the marks from the user if [ $marks -ge 80 ] then "Very Good" to be executed if expression 1 is true elif [ $marks -lt 80 ] && [ $marks -ge 50 ] then "Good" to be executed if expression 2 is true elif [ $marks -lt 50 ] && [ $marks -ge 33 ] then "Need to work hard" to be executed if expression 3 is true else "You are fail!" to be executed if no expression is true fi Problem B #!/bin/sh echo "Please enter the marks out of 100" read marks if [ $marks -ge 80 ] then echo "Very Good" elif [ $marks -lt 80 ] && [ $marks -ge 50 ] then echo "Good" elif [ $marks -lt 60 ] && [ $marks -ge 33 ] then echo "Need to work hard" else echo "You are failing!" fi Problem C ■ Allow the user to input a month name, then display the international event in the chosen month. If there is no defined pattern in the script, display “No match found.” ■ The case statement will start with the ‘case’ keyword, followed by the case expression (the month name), and the in keyword. The statement will end with the esac keyword. ■ Multiple patterns can be separated by the | operator. ■ The ) operator will terminate a pattern list. ■ A pattern including its associated commands are called a clause. ■ Each clause terminates with ;;. ■ The commands which correspond to the first pattern which will match the expression, get executed. ■ The wildcard asterisk symbol (*) acts as the final pattern that defines the default case. The final pattern will always match. Problem C The following program will read the input month from the user, and display important events in the chosen month (till July). Input the month from the user case month in month_1) print response “24th January is the international Day of Education” ;; month_2) print response " 20 February is the World Day of Social Justice ." ;; month_3) print response "8th March is the International women’s day." ;; month_4) print response “7th April is The World Health Day” ;; month_5) print response " The 15 May is the International Day of Families " ;; month_6) print response "20th June is the World Refugee Day " ;; month_7) print response "11th July is the World Population Day " ;; *) print response "No matching information found" ;; esac Problem C shopt -s nocasematch echo "Enter name of the month" read mnth case $mnth in January) echo " 24th January is the international Day of Education." ;; February) echo " 20 February is the World Day of Social Justice ." ;; March) echo "8th March is the International women’s day." ;; April) echo "7th April is The World Health Day" ;; May) echo "The 15 May is the International Day of Families" ;; June) echo "20th June is the World Refugee Day" ;; July) echo "11th July is the World Population Day" ;; *) echo "No matching information found" ;; esac Problem D ■ Allow the user to choose from the three car brands namely, Mercedes, Audi and BMW, and display the headquarters of the selected car company. ■ The switch case statement in Bash allows the script to compare different values against a variable, easier than the if conditional statement. ■ It has a similar concept like the C or JavaScript switch statement. The Bash case statement is different than the other two as it stops searching for the pattern match after it has executed statements associated with a matched pattern. ■ In this example, we will first display a menu using the printf command, and ask the user to key-in the name of the brand. ■ After that, the case conditional will follow, similar to the previous example. ■ Note that in this example, we do not use the wildcard asterisk symbol (*) as the final pattern. Problem D The following program will allow the user to choose from the three car brands namely, Mercedes, Audi and BMW, and display the headquarters of the selected car company. display the menu [a] mercedes [b]audi [c]bmw input the choice of the user case $input in mercedes) print response "Headquarters - Affalterbach, Germany" ;; audi) print response "Headquarters - Ingolstadt, Germany" ;; bmw) print response "Headquarters - Chennai, Tamil Nadu, India" ;; esac Problem D #!/bin/bash clear printf "Menu: \n[a] mercedes [b]audi [c]bmw " Read -p “Name of the Brand you chose:” CARS case "$CARS" in mercedes) echo "Headquarters - Affalterbach, Germany" ;; audi) echo "Headquarters - Ingolstadt, Germany" ;; bmw) echo "Headquarters - Chennai, Tamil Nadu, India" ;; esac This online test will be about Applied programming Logic, It is related with flowchart, psudocode, shell scripting etc. t will be an online test, 2 separate test, each will contain 50 multiple choice questions about flowchart solving, pseudocode bash, shell scripting I want to start it tomorrow 11.30 am eastern time But i want the expart connect my computer through team viewer.......... so this is how both expart & me can see the exam screen and also help expart in navigating exam window because question will appear one after one, so using screen shot will kill lot of time i will connect to expart here through chat here and then provide expart team viewer ID & Password that time and take expart to exam window So tomorrow 24 April; 11.30 am eastern time
Answered 1 days AfterApr 23, 2021

Answer To: Ch14-Decision Control Structure Explained A Simple Example of Decision Control ■ Problem: – Compare...

Gaurav answered on Apr 25 2021
153 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here