Lecture 4 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
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.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
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,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Branching statements.
Chapter 3 Control Statements
Selections Java.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
JavaScript: Control Statements I
Looping.
Lecture 2: Logical Problems with Choices
Introduction to C Programming
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
2.6 The if/else Selection Structure
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Control Statements Paritosh Srivastava.
CSC215 Lecture Control Flow.
Presentation transcript:

Lecture 4 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea

Control Structures in C

copyright © 2009 Arne Kutzner. All right reserved.L4.3 Control Structures (cont.) C provides the following control structures –compound structures (sequential Execution) –selection structures: if if/else switch –repetition structures: while do/while for Control structures are not thought to be statements in C. (Important regarding the placement of semicolons)

Sequential Execution of Statements in C

copyright © 2009 Arne Kutzner. All right reserved.L4.5 Sequential Execution (called Compound Statement) Statements are executed one by one in the order they are written in the curly braces: { statement_1; statement_2; … statement_n; } begin in Pseudocode end in Pseudocode every statement in C ends with a semicolon !

Selection Structures: if,if/else structures

copyright © 2009 Arne Kutzner. All right reserved.L4.7 if statement in C if condition then statement if (condition) statement; Pseudocode C Code Parentheses necessary !

copyright © 2009 Arne Kutzner. All right reserved.L4.8 if-else statement in C if condition then statement_1 else statement_2 if (condition) statement_1; else statement_2; Pseudocode C Code Parentheses necessary !

copyright © 2009 Arne Kutzner. All right reserved.L4.9 if statement with one compound alternative if condition then begin statement_1 statement_2 … end if (condition) { statement_1; statement_2; … } Pseudocode C Code

copyright © 2009 Arne Kutzner. All right reserved.L4.10 if Statement with Two Alternatives that are Compounds if condition then begin statement_11 statement_12 … statement_1n end else begin statement_21 statement_22 … statement_2m end if (condition) { statement_11; statement_12; … statement_1n; } else { statement_21; statement_22; … statement_2m; } PseudocodeC Code

copyright © 2009 Arne Kutzner. All right reserved.L4.11 Sequential if/else Structures Test for multiple cases by placing if/else selection structures inside if/else structures Once condition is met, rest of statements is skipped statement_1; if (condition_1) { statement_2; statement_3; } else if (condition_2) { statement_4; statement_5; } … else statement_n; statement_m;

copyright © 2009 Arne Kutzner. All right reserved.L4.12 Example Program in Pseudocode begin write "Enter midterm grade:" read midterm write "Enter final grade:" read final grade ← (0.6 * final) + (0.4 * midterm) write "Grade is " write grade if grade > 60 then write " and passed" else write " and failed" end

copyright © 2009 Arne Kutzner. All right reserved.L4.13 Pseudocode in C #include int main() { double midterm, final, grade; printf("Enter midterm grade:"); scanf ("%lf", &midterm); printf("\n Enter final grade:"); scanf ("%lf", &final); grade = 0.6 * final * midterm; printf("Grade is %lf and", grade); if (grade > 60) printf(" passed!!\n"); else printf(" failed\n"); } Variable Declaration necessary !

copyright © 2009 Arne Kutzner. All right reserved.L4.14 An ambiguous cases the last else-alternative belongs to the most recent if-structure in the same block. Example: int i = 1; int j = 2; int k = 3; if (i > j) if (i > k) printf("A"); else printf("B"); is equivalent to int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) printf("A"); else printf("B"); } Nested if/else

copyright © 2009 Arne Kutzner. All right reserved.L4.15 To force the else clause to match the first if clause, you must add a pair of braces: int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) printf("A"); } else printf("B"); This code prints B. Nested if/else (cont.)

copyright © 2009 Arne Kutzner. All right reserved.L4.16 Caution (weird C) Adding a semicolon at the end of the condition of a if structure can be a serious mistake. if (grade > 60); printf("Passed\n"); This mistake is hard to find, because the above code is correct. It results in a logic error !! misplaced semicolon if grade > 60 then begin end write "Passed" Problem shown Pseudocode:

Repetition Structures while - loops

copyright © 2009 Arne Kutzner. All right reserved.L4.18 while statement in C while condition do statement while (condition) statement; Pseudocode C Code Parentheses necessary !

copyright © 2009 Arne Kutzner. All right reserved.L4.19 while statement combined with compound structure while condition do begin statement_1 statement_2 … end while (condition) { statement_1; statement_2; … } Pseudocode C Code

copyright © 2009 Arne Kutzner. All right reserved.L4.20 Factorial Computation in Pseudo Code begin write "Enter n:" read n product ← 1 i ← 1 while i <= n do begin product ← product * i i ← i + 1 end write product end

copyright © 2009 Arne Kutzner. All right reserved.L4.21 Factorial Computation in C #include void main() { int n, i, product; printf("Enter n:"); scanf("%d", &n); product = 1; i = 1; while (i <= n) { product = product * i; i = i + 1; } printf("%d", product); } while loop in C

copyright © 2009 Arne Kutzner. All right reserved.L4.22 Caution (weird C) Adding a semicolon at the end of the condition of a while structure can be a serious mistake. i = 10; while (i > 0); i = i – 1; The above statements results in an infinite loop Misplaced semicolon i ← 10 while i > 0 do begin end i ← i - 1 Problem shown Pseudocode:

copyright © 2009 Arne Kutzner. All right reserved.L4.23 Nested Loops Loops (like all other control structures as e.g. the if-structure) may be nested A nested loop consists of an outer loop with one or more inner loops.

copyright © 2009 Arne Kutzner. All right reserved.L4.24 Example of nested while loop compute int sum, i, j; sum = 0; i=0; while (i <= N) { j = 0; while (j <= M) { sum = sum + (i * j); j = j + 1; } i = i + 1; } printf("The total sum is %d", sum); outer loop inner loop

copyright © 2009 Arne Kutzner. All right reserved.L4.25 Exercise Develop some program in Pseudocode that contains if/else, while structures and translate this program into C

Boolean Values and Conditional Expressions in C

copyright © 2009 Arne Kutzner. All right reserved.L4.27 Representation of Boolean Values in C VERY IMPORTANT : In the C the boolean values true and false are represented by integer values. The rules for interpreting integer values: –false : the value 0 –true : any value other then 0 ( typically 1 ) Remark: C does not have a separated boolean datatype as e.g. Java (serious design lack of C)

copyright © 2009 Arne Kutzner. All right reserved.L4.28 Conditional Expression in C - Introduction if (condition) statement; we call the code inside the parentheses conditional expression while (condition) statement; A conditional expression in C is an expression whose evaluation delivers a numerical value that is interpreted as a boolean value (true / false)

copyright © 2009 Arne Kutzner. All right reserved.L4.29 Constants as Conditional Expressions The two constants 0 and 1 are the simplest form of conditional expressions –The following statements prints asterisks “forever”: while (1) printf("*");

copyright © 2009 Arne Kutzner. All right reserved.L4.30 Single Variables as Conditional Expressions A single variable of type int represents a valid conditional expressions –The following statements prints asterisks “forever”: int i; i = 1; while (i) printf("*");

copyright © 2009 Arne Kutzner. All right reserved.L4.31 Compound Conditional Expressions Compound conditional expressions can be created by the use of conditional operators. Simple forms of compound expressions: –variable conditional_operator variable Example: x > y –variable conditional_operator constant Example: x == 4

copyright © 2009 Arne Kutzner. All right reserved.L4.32 Types of conditional operators 1.Relational and equality operators for comparisons 2.Operators for the representation of the boolean operations and/or/not (called logical operators)

copyright © 2009 Arne Kutzner. All right reserved.L4.33 Relational and Equality Operators OperatorMeaning <less than >greater than <=less than or equal to >=greater than or equal to ==equal to !=not equal to

copyright © 2009 Arne Kutzner. All right reserved.L4.34 Examples xpoweryitemMINDAYnumMAXSens 'M' OperatorConditionMeaningValue <= x <= 0x less than or equal to 0 1(true) < power < MAXpower less than MAX 0(false) >= x >= yx greater than or equal to y 0(false) > item > MINitem greater than MIN 1(true) == DAY == 'M'DAY equal to 'M' 1(true) != num != Sensnum not equal to Sens 0(false)

copyright © 2009 Arne Kutzner. All right reserved.L4.35 Logical Operators in Conditional Expressions Logical Operators in C: OperatorMeaning && AND || OR ! NOT

copyright © 2009 Arne Kutzner. All right reserved.L4.36 Logical Operator: AND Operator Example: Suppose x=0.5, y=0.4, z=4 (y>z)&&(x y)&& z delivers value 1 xyx && y nonzero (true) 1(true) nonzero (true)0 (false) nonzero (true)0 (false)

copyright © 2009 Arne Kutzner. All right reserved.L4.37 Logical Operator: OR Operator Example: Suppose x=0.5, y=0.4, z=4 (y>z)||(x<z) delivers value 1 (x<y)||(z!=4) delivers value 0 xyx || y nonzero (true) 1(true) nonzero (true)0 (false)1(true) 0 (false)nonzero (true)1(true) 0 (false)

copyright © 2009 Arne Kutzner. All right reserved.L4.38 Logical Operator: NOT Operator Example: Suppose x=0.5, y=0.4, z=4 !(y > z) delivers value 1 !z delivers value 0 x!x nonzero (true)0 (false) 1 (true)

copyright © 2009 Arne Kutzner. All right reserved.L4.39 Binding Rules (Operator Precedence) How to evaluate a == b && c ? Two alternatives: 1.First equality check and then AND-operation: (a == b) && c 2.First AND-operation and then equality check: a == (b && c) This ambiguity is resolved by operator precedence rules

copyright © 2009 Arne Kutzner. All right reserved.L4.40 Operator Precedence OperatorPrecedence highest (strong binding) ! * / % + - = > == != && || = lowest (weak binding) arithmetic operators assignment operator

copyright © 2009 Arne Kutzner. All right reserved.L4.41 Exercise Compute the values for all following expressions: zyxa ExpressionValue a != 10 || a == 00(false) a > x && x < y1(true) !x == 00(false) x && y || z1(true) !a0(false) !a + y + !x2(true)

copyright © 2009 Arne Kutzner. All right reserved.L4.42 Parentheses in Conditional Expressions Using parentheses you can always change the precedence of operators in a conditional expression. Example: !(a && b) The AND-operation is here more binding than the NOT-operation

copyright © 2009 Arne Kutzner. All right reserved.L4.43 Operator Associativity When two operators with the same precedence are evaluated, the associativity of the operators determines the order of evaluation. All binary operators except assignment operators are left- associative. a – b + c – d is equivalent to ((a – b) + c) – d Assignment operators are right-associative. Therefore, the expression a = b = c = 5 is equivalent to a = (b = (c = 5))

Logical Assignments in C

copyright © 2009 Arne Kutzner. All right reserved.L4.45 Logical Assignments Logical assignments are assignments where the right side represents a conditional expression. Examples: senior_citizen = (age >= 65); in_range = (i > -10) && (i < 10); is_even = ((n % 2) == 0); conditional expressions

copyright © 2009 Arne Kutzner. All right reserved.L4.46 Caution (weird C) Logic assignments can trigger big trouble in the context of an intended equality check i = 0; if (i = 1) printf("true"); else printf("false"); The above statements prints true ! please distinguish = and == i ← false i ← true if i==true then write "true" else write "false" identical logic in Pseudocode: