Quiz 1 96/07/23.

Slides:



Advertisements
Similar presentations
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4 Making Decisions
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Introduction to Programming Lecture 6: Making Decisions.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
IST 210: PHP Logic IST 210: Organization of Data IST2101.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Branching statements.
Java Programming Fifth Edition
CNG 140 C Programming (Lecture set 3)
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Making Decisions.
Selections Java.
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.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
JavaScript: Control Statements I
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
JavaScript: Control Statements.
JavaScript: Control Statements I
Control Structures – Selection
Introduction to Programming
Objectives After studying this chapter, you should be able to:
3 Control Statements:.
Chapter 7 Conditional Statements
Summary Two basic concepts: variables and assignments Basic types:
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Boolean Expressions to Make Comparisons
Control Structures Lecture 6.
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

Quiz 1 96/07/23

Quiz 1 عدد زیر را به مبنای 2 بنویسید. 100.25 -11 (به روش متمم 2، طول بیتی = 4) الگوریتم برنامه ای بنویسید که عدد n را دریافت کند و دنباله فیبوناچی را عدد کمتر از n چاپ کند. برای n = 10 خروجی زیر مورد نظر است: 1 , 1 , 2 , 3 , 5 , 8

Control Structures Lecture 6

Outline Control Structures selection structure if if … else Nested if … else switch

Control Structures All programs could be written in terms of only three control structures, sequence structure Normal operation, definition, etc. selection structure if … else, switch, etc. repetition structure for, while, do … while.

Control Structures Decisions are based on conditions Do statements based on conditions True  The statements will be done False  The statement wont be done

Conditions Conditions by comparisons; e.g., If a is greater then b If c equals to d Comparing numbers: Relational Operators

Relations Relations produce a bool value

 You should read the book Boolean operations Multiple conditions in decision making Logical relation between conditions if you are student and you have the programming course  You should read the book C Boolean operators and && or || not ! p q p && q p || q !p False True

Boolean operations Examples bool a = true, b=false, c; c = !a; //c=false c = a && b; //c=false c = a || b; //c=true c = !a || b; //c=false

Precede

Example

Casting In logical operations In mathematical & comparison operations 0  False, non-zero  True In mathematical & comparison operations False  0 , True  1

Examples  x  [10 , 20] Wrong Version Correct Version Let x = 30 10 <= 30 <=20  (10 <= 30) <= 20  true <= 20  1 <= 20  true!!! Correct Version  (10 <= x) && (x <= 20)  (10 <= 30) && (30 <= 20)  true && false  false

Examples  a,b > 0 Wrong version  Let a = -10, b = 20  -10 && 20 > 0  -10 && (20 > 0)  -10 && true  true && true  true !!! Correct version  (a > 0) && (b > 0)  Let a = -10, b = 20  (-10 > 0) && (20 > 0)  false && true  false

Lazy evaluation(Short-circuit) When final result is found, does not evaluate remaining int i bool a = true, b false, c true; d a || || c; = 1; bool bool d = b && (a || c); d = (i > 0) && (sqrt(i) > 5.6);

Selection Structures C provides three types of selection structures in the form of statements: The if selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false. The if…else selection statement performs an action if a condition is true and performs a different action if the condition is false. The switch selection statement performs one of many different actions depending on the value of an expression.

Selection Structures The if statement is called a single-selection statement because it selects or ignores a single action. The if…else statement is called a double-selection statement because it selects between two different actions. The switch statement is called a multiple-selection statement because it selects among many different actions.

if statement Decision making in C Expression if( <expression> ) <statements1> else <statements2> Expression A boolean statement: a <= b + A mathematical statement: a zero  false Non-zero  true b or a variable: a

Type of statements Expression statement Compound statement Single statements x = y + 10; Compound statement Starts with { and ends with }  All statements can be between { and }

Flowchart if(<expression>) <statement1> else

Program to check if number is even or odd

Statements in if-else Empty statement Block statements

More than two choices If statement: 2 choices If conditions are true  if statements If conditions are false  else statements How to make decisions when there are multiple choices?

Reminder if( <Condition> ) <command 1> Other command… true false

Reminder if( <Condition> ) <command 1> else <command 2> Other command… false true

Reminder if( <Condition> ) { <command 1> <command 2> } else <command 3> Other command… false true

More than two choices To avoid repeating conditions in if statements To avoid running unnecessary statements Nested if: check multiple conditions <Statements 1> becomes an if-else statement <Statements 2> becomes an if-else statement Repeat it as many as needed

Nested if

Reminder if( <Condition 1> ) { <command 1> <command 2> } else if( <Condition 2>) <command 3> else <command 4> Other command… false false true true

Map numeric grade to alphabetic

Example 1: Map numeric grade to alphabetic

Map numeric grade to alphabetic

Map numeric grade to alphabetic

Nested if: Example 2 Determine a char is alphabetic, Uppercase or not, numeric, less or greater than 5 or none of them

Equivalent if-else

Nested if: Incomplete branch 1) else part is optional 2) else always associates with the nearest if Example: To avoid error you should close off you code or Use Empty statements

Nested if: close off & empty statement

switch-case: Multiple choices Multiple conditions If-else if-else if-…. Select from alternative values of a variable switch-case Values should be constant not expression: i, i+j, Values & Variables should be int or char switch(variable) { case value1: <statements 1> case value2: <statements 2> }

How does switch-case work? Each switch-case can be rewritten by If-else if-else version of switch-case in the previous slide if(variable == value1)} <statements 1> <statements 2> } else if(variable == value2){

Example Write a program that get a char, if a or b or c print uppercase of char otherwise print error.

Example using switch

switch-case All values used in case should be different switch(i){ //Error case 1: … case 2:

switch-case All values must be value, not expression of variables switch(i){ //Error case j: … case 2: case k+10:

switch-case: multiple matches

switch-case vs. if-else if-else is more powerful than switch-case switch-case is only for checking the values of a variable and the values must be constant Some if-else cannot be rewritten by switch-case double var1, var2; if(var1 <= 1.1) <statements 1> if(var1 == var2) <statements 2>

Nested switch-case

Conditional Expression Assign value according to conditions A ternary operator Example:

Example Map Alphabetic Grade to Numeric