Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei

Slides:



Advertisements
Similar presentations
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Advertisements

Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
If Statements & Relational Operators Programming.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
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,
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
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)
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.
Rational Expressions relational operators logical operators order of precedence.
 Type Called bool  Bool has only two possible values: True and False.
CompSci 230 S Programming Techniques
Decision making If.. else statement.
The Ohio State University
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Chapter 3 Control Statements
Operators and Expressions
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
A mechanism for deciding whether an action should be taken
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Programming Fundamentals
Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei
Relational & Logical Operators
Relational and Logical Operators
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Relational Operators Operator Meaning < Less than > Greater than
C Operators, Operands, Expressions & Statements
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
Chapter 5: Control Structure
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Relational and Logical Operators
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Life is Full of Alternatives
Relational and Logical Operators
Relational and Logical Operators
Using C++ Arithmetic Operators and Control Structures
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Relational and Logical Operators
Selection Control Structure
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei Email: qinpeizhao@tongji.edu.cn http://sse.tongji.edu.cn/zhaoqinpei/Courses/

Making decisions How to make decisions based on arithmetic comparisons What logical operators are and how you can use them Reading data from the keyboard A calculator Beiginning C / Qinpei Zhao 2019/4/10

Decisions Play a game? Beiginning C / Qinpei Zhao 2019/4/10

If statement If it is a political lesson, I’ll stay in bed. If it is a programming class, I’ll go to class. If today is a public holiday, there is no class. Else, everything continues. Multiple conditions: If it is Monday, I’ll have class A; If it is Tuesday, I’ll have class B; … If it is Sunday, I’ll stay at home. Beginning C / Qinpei Zhao 2019/4/10

Arithmetic comparisons and expression < is less than; < = is less than or equal to = = is equal to; ! = is not equal to > is greater than; > = is greater than or equal to expressions involving relational operators: logical expression / Boolean expressions _Bool result = 1 < 5; (true / nonzero) _Bool result = 2 = = 3; (false / 0) bool result = 5 >= (1+5); (false / 0) (#include <stdbool.h>) ‘Z’ >= ‘A’ ? 逻辑表达式、布尔运算 Beginning C / Qinpei Zhao 2019/4/10

The basic if statement if(expression) statement1; next_statement; if(expression) statement1; Beginning C / Qinpei Zhao 2019/4/10

The basic if statement (cont.) Guessing numbers Check if age is a non-zero value Beginning C / Qinpei Zhao 2019/4/10

Extending the if statement: if-else if(expression) statement1; else statement2; next_statement; Beginning C / Qinpei Zhao 2019/4/10

Using blocks of code in if statements Enclose a block of statements between braces { } If the weather is sunny, I will walk to the park, eat a picnic, and walk home. Else I will stay in, watch football, and drink beer. Beginning C / Qinpei Zhao 2019/4/10

Nested if statements Nested ifs: have ifs within ifs. If the weather is good, I will go out in the yard. And if it’s cool enough, I will sit in the sun. Else I will sit in the shade. I will stay indoors. I will then drink some lemon tea. Beginning C / Qinpei Zhao 2019/4/10

Logical operators (逻辑运算符) - AND && The AND operator && - combines two logical expressions Binary operator 二目/元运算符 Beginning C / Qinpei Zhao 2019/4/10

Logical operators (逻辑运算符) - OR || The OR operator || - check for any of two or more conditions being true Beginning C / Qinpei Zhao 2019/4/10

Logical operators (逻辑运算符) - NOT ! Unary operator 一目/元运算符 The NOT operator ! – reverse the value of a logical expression: true becomes false, and false becomes true Beginning C / Qinpei Zhao 2019/4/10

Verify uppercase letters Beginning C / Qinpei Zhao 2019/4/10

The conditional operator Ternary operator 三目/元运算符 The conditional operator – evaluates one of two expressions depending on whether a logical expression evaluates true or false Beginning C / Qinpei Zhao 2019/4/10

Operator precedence (运算符优先级) Suppose you are to process job applications and you want to only accept applicants who are 25 or older and have graduated from Tongji or Fudan. Age >= 25 && Tongji || Fudan (Age >= 25 && Tongji ) || Fudan Age >= 25 && (Tongji || Fudan) Beginning C / Qinpei Zhao 2019/4/10

Operator precedence (运算符优先级) All the comparison operators are below the binary arithmetic operators. The binary logical operators are below the comparison operators. Assignments come last in the list. The conditional operator squeezes in just above the assignment operators. The ! Operator is highest within the set of logical operators. arithmetic > comparisons > logical combinations > conditional operator > assignments See Table 3-2 Beginning C / Qinpei Zhao 2019/4/10

Using logical operators without confusion Suppose you are to process job applications and you want to only accept applicants who meet certain educational specifications. An applicant who meets any of the following criteria should be accepted for an interview: Graduates over 25 who studied computer science and who didn’t graduate from Fudan Graduates from Fudan who studied computer science Graduates from Tongji who studied economics and aren’t older than 28 Graduates from Fudan who are over 25 and who didn’t study computer science Beginning C / Qinpei Zhao 2019/4/10

Multiple choices (else-if) Beginning C / Qinpei Zhao 2019/4/10

Multiple choices (switch-case) Beginning C / Qinpei Zhao 2019/4/10

The goto statement Confusing: difficult to follow Avoid the goto statement as far as possible Beginning C / Qinpei Zhao 2019/4/10

Bitwise operators Operate on the bits in integer values http://visualgo.net/bitmask.html Bitwise operators Operate on the bits in integer values ~ unary operator (applies to one operand) Others are binary operator Beginning C / Qinpei Zhao 2019/4/10

Bitwise operator – AND & both bits are 1, the resulting bit is 1; otherwise, the resulting bit is 0. int x = 13; int y = 6; int z = x & y; the bitwise operator v.s. the logical operator x & y v.s. x && y Beginning C / Qinpei Zhao 2019/4/10

Other bitwise operators results in 1 if either or both of the corresponding bits are 1; otherwise, the resulting bit is 0. Exclusive OR ^ Produces a 1 if both bits are different 0 if they’re the same Unary operator ~ Flips the bits of its operand, so 1 becomes 0, and 0 becomes 1. Beginning C / Qinpei Zhao 2019/4/10

Designing a program Calculator The problem is to write a simple calculator that can add, substract, multiply, divide, and find the remainder when one number is divided by another. The program must allow the calculation that is to be performed to be keyed in a natural way, such as 5.6*27 or 3 + 6. Calculator Get the user’s input for the calculation that the user wants the computer to perform Check that input to make sure that it’s understandable Perform the calculation Display the result Beginning C / Qinpei Zhao 2019/4/10