LAB 4 SELECTION STRUCTURES

Similar documents
9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Sources: On the Web: Slides will be available on:

CS 241 Data Organization Coding Standards

2 SYSTEM DESCRIPTION TECHNIQUES

9.4. The Scalar Product. Introduction. Prerequisites. Learning Style. Learning Outcomes

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Chapter 5. Selection 5-1

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

ChE-1800 H-2: Flowchart Diagrams (last updated January 13, 2013)

FEEG Applied Programming 5 - Tutorial Session

Beginner s Matlab Tutorial

Selection Statements

Week 13 Trigonometric Form of Complex Numbers

Unit 6 Trigonometric Identities, Equations, and Applications

CHM 579 Lab 1: Basic Monte Carlo Algorithm

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Tutorial on C Language Programming

1 Basic commands. 2 Terminology. CS61B, Fall 2009 Simple UNIX Commands P. N. Hilfinger

VHDL Test Bench Tutorial

CHAPTER 11: Flip Flops

Lecture 2 Notes: Flow of Control

MATLAB Programming. Problem 1: Sequential

BPMN Business Process Modeling Notation

10 Listing data and basic command syntax

Two-way selection. Branching and Looping

Lab E1: Introduction to Circuits

Lesson/Unit Plan Name: Patterns: Foundations of Functions

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

3. Locate the different selections of Styles from the Home Tab, Styles Group

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

MODERN APPLICATIONS OF PYTHAGORAS S THEOREM

FSMQ Additional Mathematics. OCR Report to Centres June Unit 6993: Paper 1. Free Standing Mathematics Qualification

Visual Tutorial Basic Edition 1. Visual. Basic Edition Tutorial.

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

LECTURE 11: PROCESS MODELING

Using the SQL TAS v4

Bash shell programming Part II Control statements

How to Concatenate Cells in Microsoft Access

A UML Introduction Tutorial

Chapter 1 Java Program Design and Development

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Problem Solving Basics and Computer Programming

Cubes and Cube Roots

LED Bulb Manual. Aeotec by Aeon Labs LED Bulb.

CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

Chapter 9. Systems of Linear Equations

Order of Operations More Essential Practice

Creating a Simple Macro

Using Microsoft Project 2000

PowerWorld Simulator

Using Microsoft Word. Working With Objects

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Solutions to Bulb questions

Using Parametric Equations in SolidWorks, Example 1

Lecture 2 Mathcad Basics

Introduction to Windows Server 2016 Nested Virtualization

Student Outcomes. Lesson Notes. Classwork. Exercises 1 3 (4 minutes)

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Compiler I: Syntax Analysis Human Thought

Data Flow Diagrams. Outline. Some Rules for External Entities 1/25/2010. Mechanics

arrays C Programming Language - Arrays

To draw a line. To create a construction line by specifying two points

Process / Operation Symbols

Final Exam Review: VBA

The programming language C. sws1 1

Lab Creating a Logical Network Diagram

Creating a table of contents quickly in Word

Database Programming with PL/SQL: Learning Objectives

Introduction. UML = Unified Modeling Language It is a standardized visual modeling language.

6.4 Logarithmic Equations and Inequalities

Joins Joins dictate how two tables or queries relate to each other. Click on the join line with the right mouse button to access the Join Properties.

Chapter One Introduction to Programming

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

TRIGONOMETRY Compound & Double angle formulae

First Steps with CoDeSys. Last update:

Contents. 2 Lines and Circles Cartesian Coordinates Distance and Midpoint Formulas Lines Circles...

THE SET ANALYSIS. Summary

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Lecture 22: C Programming 4 Embedded Systems

JavaScript: Control Statements I

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports

Beyond the Mouse A Short Course on Programming

ESPResSo Summer School 2012

C++ INTERVIEW QUESTIONS

Part 1 Foundations of object orientation

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Triangle Trigonometry and Circles

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Transcription:

LAB 4 SELECTION STRUCTURES School of Computer and Communication Engineering Universiti Malaysia Perlis 1

1. OBJECTIVES: 1.1 Introduction to if, if., nested if and switch statements. 1.2 Able to use selection control structures in programs. 1.3 Able to modify program according to user requirements. 2 INTRODUCTION: In C language, there are several selection methods which can be used: (a) One way selection (b) Two way selection (c) Multi selection (d) Nested if (e) Switch command 2.1 One way selection statement The syntax for one way selection is as below: Compound statement: a group of statements bracketed by { and } that are executed sequentially. { (open curly bracket) and } (close curly bracket) must be used to show the start and end of the commands under the if statement. { Flowchart: ; statement2; start } Yes No 2.2 Two way selection statement The syntax for two way selection is as below: or { ; statement2; } { ; statement2; } 2

Flowchart: start statement2 No Yes 2.3 Multi selection statement The syntax for multi selection statement is as below: Flowchart: start F T T F end 2.4 Nested if statement Nested if statement is when one if statement is placed inside another if statement. The nested if statement is used to code decisions with multiple alternatives. The nested if selection can be modified with the use of logic operator (AND) to combine two or more al (relation) statements to be as one statement, thus omits the nested if statement. Nested if Use of logic operator if (() && ()) 3

Flowchart: start F T statement2 end 2.5 Switch statement The switch statement is another way to do multi selections statement. The syntax is as follow: switch (variable) { case value1: printf( Message 1 ); break; case value2: printf( Message 2 ); break; case value3: printf( Message 3 ); break; case value4: printf( Message 4 ); break; case value5: printf( Message 5 ); break; default: printf( Default Message ); } Note: The break statement is used to end the switch after the category or selection is selected. Omitting the break statement will not generate a syntax error in your program but it will produce semantic error. Question: Find out what are syntax and semantic errors. 4

3 TASKS: 3.1 (a) The following code segment is syntactically correct, but difficult to read. Rewrite the segment using indentation that improves its readability. if (croadstat == 's') if (ftemp > 0) printf("roads wet.\n"); printf("roads icy.\n"); printf("roads dry.\n"); (b) Rewrite the following if statement as an equivalent switch statement. The variable digit is of type int. if (idigit == 0) ivalue = 3; if (idigit == 1) ivalue = 3; if (idigit == 2) ivalue = 6; if (idigit == 3) ivalue = 9; 5

(c) The decision table below shows fines imposed for speeding violations. Write a code segment that assigns the correct fine to type double variable fine based on the value of type int variable speed. Speed (mph) Fine ($) 65 or less 0 66-70 15.00 71-75 30.00 76-80 75.00 over 80 100.00 (d) Evaluate the expression below assuming a is 5, flag is 1, and c is 15. What part of the expression is not computed at all because of short-circuit evaluation? a!= 3 && flag c >= 10 (e) Write the output for the following code segment. v1 = 15.0; v2 = 0.5; if (v1 > 10.0) printf("ten "); if (v1 > 14.0) printf("fourteen "); if (v2 * v1 > 7.0) printf("seven "); if (v1 - v2 > 9.0) printf("nine "); printf("\n"); 6

3.2 Write an interactive program that contains an if statement that may be used to compute the area of square (area = side 2 ) or a triangle (area = ½ x base x height ) after prompting the user to type the first character of the figure names (S or T). a. Write down the flowchart for the program. b. Write your program based on flowchart in (a). 7

3.3 Write a program for the National Earthquake Information centre implementing the following decision table to characterize an earthquake based on its Richter scale number. Richter Scale Number (N) Characterization N < 5.0 Little or no damage 5.0 N 5.5 Some damage 5.5 N 6.5 Serious damage: wall may crack or fall 6.5 N 7.5 Disaster: house and buildings may collapse higher Catastrophe: most buildings destroyed a. Write down the flowchart for the program. b. Can you handle this problem with a switch statement? If so, use a switch if not, explain why; c. Write a program based on flowchart in (a). 8

3.4 Write a nested if statement for the decision diagrammed in the accompanying flowchart. Use a multiple-alternative if for intermediate decisions where possible. false ph > 7 true ph == 7 true ph < 12 true false Neutral false ph > 2 true Alkaline false Very Alkaline Very Acidic Acidic 9

3.5 Write a program that takes the x-y coordinates of a point in the Cartesian plane and displays a message telling either an axis on which the point lies or the quadrant in which it is found. y QII QI x QIII QIV Sample lines of output: (-1.0, -2.5) is in quadrant III (0.0, 4.8) is on the y axis a. Write down the flowchart for the program. b. Write a program based on flowchart in (a). 10

3.6 Write a program that assigns to the variable lumens the expected brightness of a standard light bulb whose power has been stored in watts. Use the following table: Power (Watts) Brightness (Lumens) 15 125 25 215 40 500 60 880 75 1000 100 1675 Assign -1 to lumens if the value of watts is not in the table. a. Write down the flowchart for the program. b. Write a switch statement based on flowchart in (a). c. Write a nested if statement equivalent to the switch statement (b) based on flowchart in (a). 11