Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises).

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Modular Programming With Functions
Computer Programming w/ Eng. Applications
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Lab 6 rOperators l Relational Operators l Equality Operators l Logical Operators rIf statement l One Alternative l With Compound Statement l Nested If.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
C++ for Engineers and Scientists Third Edition
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Control Structures I (Selection)
1 Chapter 9 Scope, Lifetime, and More on Functions.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Flow of Control Part 1: Selection
Homework Assignment #3 J. H. Wang Apr. 19, 2007.
Previously Repetition Structures While, Do-While, For.
Chapter 05 (Part III) Control Statements: Part II.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Homework Assignment #4 J. H. Wang Dec. 3, 2007.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
1 Programming Application Overview of Structure Programming.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
COMP Loop Statements Yi Hong May 21, 2015.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
C++ Programming Control Structures I (Selection).
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
A First Book of C++ Chapter 4 Selection.
Repetition statements
Chapter 4 – C Program Control
The if…else Selection Statement
Chapter 4 C Program Control Part I
Decisions Chapter 4.
Chapter 4 (Conditional Statements)
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Looping.
CS1100 Computational Engineering
CS1100 Computational Engineering
SELECTION STATEMENTS (2)
Chapter 4 Selection.
ICS103: Programming in C 4: Selection Structures
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises).

Multiple Alternative Decision Example1 Write a C program to compute the tax due based on the Tax Table. Precondition: the salary is defined. Post-condition: Returns the tax due for 0.0<=salary<=150,000.00, and returns -1.0 if salary is outside the table range. Salary Range ($) Base Tax($) % of Excess , , , , , , , , , , , , ,

Multiple Alternative Decision Example2 and 3 rThe Air Force has asked you to write a program to label supersonic aircraft as military or civilian. Your program is to be given the plane’s observed speed in km/h and its estimated length in meters. For planes traveling in excess of 1100km/h, you will label those longer than 52 meters « civilian » and shorter aircraft as « military ». For planes at slower speeds, you will issue an « aircraft type unknown » message. rThe ph exercise

Multiple Alternative Decision Example4 rNote1 : the else also refers to the last if to which an else was not attributed. rExample 4: Write a C program about invoices. It reads a simple price net of taxes and calculates the price including all taxes corresponding to a constant rate of TVA of 18.6%. it established a handing-over of which the rate depends on the value obtained: l 0% for an amount lower than 100Euro. l 1% for an amount superior or equal to 100Euro and inferior with 200Euro. l 3% for an amount superior or equal to 200Euro and inferior with 500Euro. l 5% for an amount superior or equal to 500Euro.

Switch Statement The switch statement is a multiway conditional statement generalizing the if- else statement. The following is a program extract that shows a typical example of the switch statement: Switch (Value) { case 1: printf(“Good morning\n”); break; case 2: case 3: printf(“Good Evening\n”); break; default: printf(“Good Night\n”); }

Switch Statement Example2 rNote2: the expression of the switch may be of type int or char, but not of type double. rWrite a C program to show the expected brightness of a standard light bulb given its wattage. Display Unknown bulb if the watts input is not in the table. Watts Brightness (in Lumens) Otherwise -1

Switch Statement Example3 rNote3: the statements following a case label may be one or more C statements, so we don’t need to make multiple statements into a single compound statement using braces. rRemove the break and see the output in case of the absence of break. What is happening ?

Common Programming Errors rIf(0<=x<=4) what does it mean ? And how can we check if x is in the range of 0 to 4. rIf(x=10) x=10 is an assignment statement. 10 is the value of the condition !!! rIf (x>0) sum= sum+x; printf( " Greater than zero\n "); else printf(" Less than zero\n ");

For Statement Definition /While Statement The for statement is like the while. It is used to execute code iteratively. Consider a construction of the form: for(expr1;expr2;expr3) Statement Next statement It is semantically equivalent to expr1; While (expr2) { Statement; expr3; }

Examples rWrite a c program that displays a table of angle measures along with sine and cosine values. Assume that the initial and final angle measures (in degrees) are available in init_degree and final_degree (type int variables) and that the change in angle measure between table entries is given by step_degree (also a type int variable). Remember that the math library’s sin and cos functions take arguments that are in radians. rExample2: Write a C program to compute the factorial as long as the user wants.

Examples rWrite a nests of loops that cause the following output to be displayed : rWrite a C program to display a triangle filled with stars as the following : * ** *** **** *****

How to Debug and Test Programs rFor(i=0;i<n;i++) { sum+=score; if(DEBUG) printf(“****** score is %d, sum is %d \n”, score, sum); printf(“ Enter next score (%d to quit )>”, SENTINET); scanf(“ %d”,&score); } rWhat if you want to execute a given statement n times ? for (count =0; count<=n; ++count); what if count is initialized to 1 ?

Notes rEach of the 3 expressions of the for statement is facultative. i=1; for (;i<=5;i++); r C permits to group a lot of actions in one expression: for (i=0, sum=0;i<=5;i++); for (i=0, sum=0;i<=5;printf(“ sum= %.2lf”, sum),i++); rBreak can be used to got out from the for loop if a given condition is true. rContinue can be used not to execute a given statement in a for loop if a given condition is true.