Lab 6 rOperators l Relational Operators l Equality Operators l Logical Operators rIf statement l One Alternative l With Compound Statement l Nested If.

Slides:



Advertisements
Similar presentations
ICS103: Programming in C 4: Selection Structures Muhamed F. Mudawar.
Advertisements

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.
Chapter 4 Selection Structures: if and switch Statements Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 8.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CS 201 Selection Structures (2) and Repetition
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises).
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
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;
Chapter 4 Making Decisions
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
A. Abhari CPS1251 Topic 4: Control structures Control Structures Overview Conditions if Statement Nested if Statements switch Statement Common Programming.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Homework Assignment #3 J. H. Wang Oct. 29, 2007.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Homework Assignment #3 J. H. Wang Apr. 19, 2007.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 5.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CPS120: Introduction to Computer Science Operations Lecture 9.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Copyright 2003 Scott/Jones Publishing Making Decisions.
1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
chap4 Chapter 4 Selection Structures: if and switch Statements.
1 CS161 Introduction to Computer Science Topic #6.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Chapter 4: Making Decisions.
CMPT 201 if-else statement
The Selection Structure
Chapter 4: Making Decisions.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 3: Selection Structures: Making Decisions
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

Lab 6 rOperators l Relational Operators l Equality Operators l Logical Operators rIf statement l One Alternative l With Compound Statement l Nested If NOTE: Be Careful Read Chapter 4(All Self-Check exercises and all remaining exercises). C KEYWORDS and format must be learned by heart

Review of Rules of Evaluating Expressions Exercise 1 : Include parentheses to show the order of performing the expressions. ra+b*c ra*b+c%d r-c%d r-a +c%d r-a /-b +c r-a/-(b+c)

Simple Conditions Exercise 2: Provide the English Meaning and Values. Given: x=-5, power=1024,y=7, MAX_POWER=1024,mor_or_dad=‘M’ SENTINEL=999 rx<=0 rx>=MAX_POWER rmor_or_dad == ‘M’ rx != SENTINEL

Operator Precedence Function calls highest ! + - & (UP) * / % + - >= == != % || = Lowest

Exercise3 (Step by Step Evaluation) rProvide the English Meaning and Values. Given :x=3.0, y=4.0, z =2.0, and flag =5 r!flag rx + y / z <= 3.5 r !flag || (y+z >= x-z) r!(flag || (y+z >= x-z)

rShort-circuit evaluation : stopping evaluation of a logical expression as soon as its value can be determined. l Exp : a ||b, a&&b rLogical Assignments : int age, char gender, int senior_citizen, in_range and is_letter, n, even l scanf (" %d ",&age); l senior_citizen=(age>=65); l ! senior_citizen; l senior_citizen && gender ==‘M’; l is_range = (n>-10 && n<10); is_letter = ( (‘A’ <= ch && ch<=‘Z’) || (‘A’ <= ch && ch<=‘Z’) ); even = (n%2 == 0) ; Short-circuit evaluation and Logical Assignments

Complementing a Condition and Morgan’s Theorem r!(item ==SENT) r!(status==‘s’ && age > 25) rMorgan’s Theorem l The complement of expr1 && expr2 as comp1 || comp2 l The complement of expr1 || expr2 is comp1 && comp2 rExercise4 :Given x=15.0 and y = 25.0, evaluate the following expressions : l x!=y l x<x l x >= y l x == y +x –y

rExercise5 : Given a =5, b=10, c=15 and flag =1. Use the short circuit evaluation : l c == a+b || !flag l a!= 7 && flag || c>=6 l !(b<= 12) && a%2 ==0 l !(a > 5 || c < a+b) l Complement each expression. l What is the value assigned to ans of type int if p=100 and q=50 ans = (p>95) +(q<95);

Programming rWrite an expression to test the following : l age is from 18 to 21 inclusive. l water is less than 1.5 and also greater than 0.1 l year is divisible by 4. l speed is not greater than 55. l y is greater than x and less than z. l w is either equal to 6 or not greater than 3. l Assign 1 to between if n is the range –k through +k l Assign 1 to uppercase if ch is uppercase letter; otherwise assign a value of 0. l Assign a value of 1 to divisor if m is divisor of n; otherwise assign it the value of 0.

If Statement rWhat do the statements display ? l If(12 <12) printf (" less" ); else printf (" not less" ); l var1= 25.12, var2= if( var1 <= var2) printf (" less or equal" ); else printf (" greater than" ); rWhat is the value assigned to x when y = 15.0 ? l x =25.0; If (y!= (x-10.0)) x=x-10.0; else x=x/2.0;

rWrite C statements : l If item is nonzero, then multiply product by item and save the result in product, otherwise skip the multiplication. In either case, print the value of product. l If x is 0, add to zero_count. If x is negative, add x to the minus_sum. If x is greater than 0, add x to plus_sum. rCompute the population growth from yesterday to today as a percentage of yesterday’s population. rTrace the following program given x =12.5, y=5.0, temp=?: if (x>y) { temp=x; x=y; y=temp;}

Exercise6 rWrite an if statement that might be used to compute and display the average of a set of n numbers whose sum is stored in variable total. This average should be only if n>0, otherwise an error message should be displayed.

Multiple-Alternative Decision rWrite a C program that shows the relationship between noise and human perceptions of noises. Loudness in Decibels (db) Perception or lower quiet intrusive annoying very annoying above 110 uncomfortable

Exercice7 rA store clerk gains a raise of 10% of his salary if all the sales he has performed exceed 30,000 DH worth of the merchandise each month, an only 5% raise if the sales are below 30,000 DH. Write a C program that shows the clerk man how to figure out what is his raise. rExplain the difference between : If( x >= 0) x = x+1 else if (x>=1) x= x+2 And If (x >= 0) x = x+1 if (x>=1) x= x+2

Exercise 8 Write a 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)