# COMP 208 Computers in Engineering

Size: px
Start display at page:

## Transcription

1 COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP Lecture 14 1

2 Review: basics of C C is case sensitive 2 types of comments: /* */, // A C program is a collection of functions The main function is the starting point If a function doesn t return anything, the return type should be void Blocks of code are enclosed in { Operators: + - * / % I/O operations (scanf/printf) provided by standard library. Header file stdio.h must be included COMP Lecture 14 2

3 Another Shorthand Expressions such as i = i+3; x = x*(y+2); Can be rewritten i += 3; x *= (y+2); COMP Lecture 14 3

4 Compound assignment In general, expressions of the form: var = var op exp; Can be rewritten in a form that is equivalent (but more efficient to execute): var op = exp; This is called compound assignment. COMP Lecture 14 4

5 Operator Precedence ( ) [ ] ->.! - * & sizeof cast (right->left) / % + - < <= >= > ==!= & /\ &&?: (right->left) = += -= (right->left), (comma) COMP Lecture 14 5

6 If Statements in C Syntax: Other forms: if (expression) statement1 else statement2 if (expression) statement Each of the statement can be a simple statement or a block statement enclosed in { if (expression1) statement else if (expression2) statement... else statement COMP Lecture 14 6

7 A Note On Syntax There is no marker to end the if statement Only a single statement can appear in each clause Grouping statements inside braces { makes them into a single compound statement A compound statement (a group of statements in { ) can appear wherever a single statement can appear COMP Lecture 14 7

8 examples if (a > 0) b++; = if (a > 0) { b++; recommended! if (a > 0) b++; c++; = if (a > 0) { b++; c++; if (a > 0) { b++; c++; COMP Lecture 14 8

9 Nested if Statements The statement executed as a result of an if statement or else clause could be another if statement These are called nested if statements COMP Lecture 14 9

10 Min of three int num1, num2; int num3, min = 0;... if (num1 < num2) if (num1 < num3) min = num1; else min = num3; else if (num2 < num3) min = num2; else min = num3; if (num1 < num2) { if (num1 < num3) min = num1; else min = num3; else { if (num2 < num3) min = num2; else min = num3; It is recommended to use { to make the structure clearer. COMP Lecture 14 10

11 Wait a Second! There is no logical type in C This isn t like Fortran What is the expression supposed to evaluate to? What is the meaning of an if statement? COMP Lecture 14 11

12 Semantics of if Statements 1. Evaluate the expression a) If it evaluates to any non-zero value, execute the first statement b) If it evaluates to 0, execute the else clause, if any. 2. Go on to the statement following the if if (3) printf( true ); else printf( false ); if (3.5) printf( true ); else printf( false ); COMP Lecture 14 12

13 From Fortran to C Part 2 Nathan Friedman

14 Roots of a Quadratic in C #include <stdio.h> #include <math.h> void main() { float a, b, c; float d; float root1, root2; scanf ("%f%f%f", &a, &b, &c); /* continued on next slide */ COMP Lecture 14 14

15 if (a == 0.0) if (b == 0.0) if (c == 0.0) printf ("All numbers are roots \n"); else printf ("Unsolvable equation"); else printf ("This is a linear form, root = %f\n", -c/b); else { d = b*b - 4.0*a*c ; if (d > 0.0) { d = sqrt (d); root1 = (-b + d)/(2.0 * a) ; root2 = (-b - d)/(2.0 * a) ; printf ("Roots are %f and %f \n", root1, root2); else if (d == 0.0) else { printf ("The repeated root is %f \n", -b/(2.0 * a)); printf ("There are no real roots \n"); printf ("The discriminant is %f \n", d); COMP Lecture 14 15

16 Quadratic Roots Revisited Let s make one slight change to the program: COMP Lecture 14 16

17 if (a = 0.0) if (b == 0.0) if (c == 0.0) printf ("All numbers are roots \n"); else printf ("Unsolvable equation"); else printf ("This is a linear form, root = %f\n", -c/b); else { d = b*b - 4.0*a*c ; if (d > 0.0) { d = sqrt (d); root1 = (-b + d)/(2.0 * a) ; root2 = (-b - d)/(2.0 * a) ; printf ("Roots are %f and %f \n", root1, root2); else if (d == 0.0) else { printf ("The repeated root is %f \n", -b/(2.0 * a)); printf ("There are no real roots \n"); printf ("The discriminant is %f \n", d); COMP Lecture 14 17

18 Quadratic Roots Revisited Can you spot the change? What do you think the effect is? COMP Lecture 14 18

19 What Happens Here? The equivalent statement in Fortran would cause a syntax error The expression (a = 0.0) is not of type logical But C does not have a type logical What happens in C? COMP Lecture 14 19

20 The C Assignment Operator In Fortran, assignment is a statement. In C, assignment is an operator COMP Lecture 14 20

21 The C Assignment Operator In Fortran, assignment is a statement. In C, assignment is an operator It can appear as part of an expression if ((a = b+c) > d) f++; = a = b+c; if (a > d) f++; When evaluated it causes a value to be assigned (as in Fortran) It also returns a value that can be used in evaluating the expression If it appears independently, with a ; after it this value is discarded COMP Lecture 14 21

22 The C Assignment Operator Syntax: variable = expression Semantics 1. Evaluate the expression 2. Store the value in the expression in the variable 3. Return the value to the expression Assignment statement in C has a value (the value of the right-hand side expression). In Fortran, assignment statements have no value. COMP Lecture 14 22

23 Back to our example In C, (a=0.0) would assign 0 to a and then return 0 as the value of the expression The if condition, with value 0, would be taken as equivalent to false The else clause would be evaluated and when the root was calculated, there would be an attempt to divide by 0 COMP Lecture 14 23

24 ? : Conditional operator C has many operators and functions that are not part of Fortran They come in useful in many applications One of these is the?: operator COMP Lecture 14 24

25 The? Operator The? (ternary condition) operator is a more efficient form for expressing simple if selection It has the following syntax: e1? e2 : e3 Semantics: e1 is evaluated, if it is true (non-0), the value of the whole expression is e2; otherwise it is e3. COMP Lecture 14 25

26 The? Operator To assign the maximum of a and b to z: z = (a>b)? a : b; which is equivalent to: if (a>b) z = a; else z = b; printf( Time = %d %s\n, n, (n==1)? hour : hours ); If n is 1, it prints Time = 1 hour If n is 3, it prints Time = 3 hours COMP Lecture 14 26

27 Loops in C The basic looping construct in Fortran is the DO loop In C, there is a more complex looping command called the for loop COMP Lecture 14 27

28 The C for Loop The C for statement has the following (deceptively simple) form: for (e1; e2; e3) statement The statement can be a block of statements inside braces { The Fortran equivalent is: e1 DO WHILE(e2) statement e3 END DO or COMP Lecture e1 DO IF (.NOT. e2) EXIT statement e3 END DO

29 Semantics of the for loop for (e1; e2; e3) statement 1. e1 is evaluated just once before the loop begins. It initializes the loop. 2. e2 is tested at the beginning of each iteration. It is the termination condition. If it evaluates to 0 (false) the loop terminates. Otherwise the loop continues. 3. e3 is evaluated at the end of each iteration. It is used to modify the loop control. It is often a simple increment, but can be more complex. COMP Lecture 14 29

30 A C for Loop Example main(){ int x; for (x=3; x>0; x--) printf("x=%d\n",x);...outputs: x=3 x=2 x=1 COMP Lecture 14 30

31 What do these do? for (i=1; i<10; i++) printf( %d %d %d\n, i, i*i, i*i*i); for (x=3; ((x>3) && (x<9)); x++) for (x=3,y=4; ((x>=3) && (y<9)); x++,y+=2) printf( yes ); for (x=0,y=4,z=4000; z; z/=10) Comma operator COMP Lecture 14 31

32 The While Statement The while has the syntax: while (expression) statement Semantics: The expression is evaluated. If it evaluates to 0 (false) the loop terminates, otherwise the statement is executed and we repeat the evaluation. COMP Lecture 14 32

33 Example main() { int x=3; while (x>0) { printf("x=%d\n",x); x--;...outputs: x=3 x=2 x=1 COMP Lecture 14 33

34 While Statements The equivalence of a for loop and a while loop for(e1; e2; e3) statement The while statement while (e) s is semantically equivalent to for (;e;) s e1; while(e2) { statement e3 COMP Lecture 14 34

35 for vs. while Using Fortran parlance, for a definite iterator, we normally use the for loop For an indefinite iterator, we normally use the while loop. COMP Lecture 14 35

36 What do these do? As with many constructs in C, the while statement can interact with other features to become much less clear: while (x--) ; while (x=x+1) ; while (x+=5) ; while (i++ < 10) ; while (x--!= 0) while ((x=x+1)!= 0) while ( (ch = getchar())!= q ) putchar(ch); COMP Lecture 14 36

37 Do-while Both while and do-while execute a statement repeatedly and terminate when a condition becomes false In the do-while, the testing is done at the end of the loop do statement while (expression) Therefore, the loop body is executed at least once. COMP Lecture 14 37

38 Break and Continue The break statement is similar to the Fortran EXIT and allows us to exit from a loop break; --exit from loop The continue statement forces control back to the top of the loop continue; --skip 1 iteration of loop COMP Lecture 14 38

39 Using break and continue while (scanf( %d, &value ) == 1 && value!= 0) { if (value < 0) { printf( Illegal value\n ); break; /* Abandon the loop */ if (value > 100) { printf( Invalid value\n ); continue; /* Skip to start loop again */ /* Process the value (guaranteed between 1 and 100) */...; /* end while when value read is 0 */ COMP Lecture 14 39

40 Inputting Values in C In the program for computing the roots of a quadratic, we began by reading the values for the coefficients a, b and c COMP Lecture 14 40

41 Roots of a Quadratic in C #include <stdio.h> #include <math.h> void main() { float a, b, c; float d; float root1, root2; scanf ("%f%f%f", &a, &b, &c); /* continued on next slide */ COMP Lecture 14 41

42 The Input Statement Syntax: scanf( format string, list of variable references) ; Semantics: Each variable reference is has the form &name For now, ignore the & that appears in front of variable names It has to be there but we ll explain why later The & is the address of operator. So, &a is the address of the variable a. COMP Lecture 14 42

43 parameter passing in C In C, function parameters are passed in a way known as pass by value. The default way in Fortran is pass by reference. void fun(int x) { x = x+1; int main() { int i = 1; fun(i); printf( %d\n, i); If this were Fortran, i would be changed to 2. NOT in C though. void fun(int *x) { *x = *x+1; int main() { int i = 1; fun(&i); printf( %d\n, i); In C, we have to pass the address of the variable COMP Lecture 14 43

### Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

Computers in Engineering COMP 208 Moving From Fortran to C Part 2 Michael A. Hawker Roots of a Quadratic in C #include #include void main() { float a, b, c; float d; float root1, root2;

### Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

Computers in Engineering COMP 208 Moving From Fortran to C Part 2 Michael A. Hawker Roots of a Quadratic in C #include #include void main() { float a, b, c; floatd; float root1, root2;

### Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Computers in Engineering COMP 208 Moving From Fortran to C Michael A. Hawker Remember our first Fortran program? PROGRAM hello IMPLICIT NONE!This is my first program WRITE (*,*) "Hello, World!" END PROGRAM

### There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

### Problem Solving and 'C' Programming

Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 05: Selection and Control Structures 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained herein

### Flow Control. CSC215 Lecture

Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

### Operators and Expressions:

Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence

### ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control Outline of this Lecture Examples of Statements in C Conditional Statements The if-else Conditional Statement

### Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

CP Lect 5 slide 1 Monday 2 October 2017 Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont) Cristina Alexandru Monday 2 October 2017 Last Lecture Arithmetic Quadratic equation

### C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

C Program Output #include main () { printf ( Hi everyone\n ); Hi everyone #include main () { printf ( Hi everyone\n ); #include and main are Keywords (or Reserved Words) Reserved Words

### Control Statements. If Statement if statement tests a particular condition

Control Statements Control Statements Define the way of flow in which the program statements should take place. Implement decisions and repetitions. There are four types of controls in C: Bi-directional

### UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

( Word to PDF Converter - Unregistered ) http://www.word-to-pdf-converter.net FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING INTRODUCTION TO C UNIT IV Overview of C Constants, Variables and Data Types

### MODULE 2: Branching and Looping

MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written

### Structured Programming. Dr. Mohamed Khedr Lecture 4

Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1 Scientific Notation for floats 2.7E4 means 2.7 x 10 4 = 2.7000 = 27000.0 2.7E-4 means 2.7 x 10-4 = 0002.7 = 0.00027 2 Output Formatting

### Lecture 6. Statements

Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions

### INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue

### Computers Programming Course 7. Iulian Năstac

Computers Programming Course 7 Iulian Năstac Recap from previous course Operators in C Programming languages typically support a set of operators, which differ in the calling of syntax and/or the argument

### UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

### n Group of statements that are executed repeatedly while some condition remains true

Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,

### Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

### Variables and literals

Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

### PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

PDS Lab Section 16 Autumn-2017 Tutorial 3 C Programming Constructs This flowchart shows how to find the roots of a Quadratic equation Ax 2 +Bx+C = 0 Start Input A,B,C x B 2 4AC False x If 0 True B x 2A

Prepared by: Shraddha Modi Introduction Operator: An operator is a symbol that tells the Computer to perform certain mathematical or logical manipulations. Expression: An expression is a sequence of operands

### Chapter 4. Flow of Control

Chapter 4. Flow of Control Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Sequential flow of control

### C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

### Programming for Electrical and Computer Engineers. Loops

Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly

### Introduction to C Programming

1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

### Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations 4th October, 2010 Integer arithmetic in C. Converting pre-decimal money to decimal. The

### Decision Making and Loops

Decision Making and Loops Goals of this section Continue looking at decision structures - switch control structures -if-else-if control structures Introduce looping -while loop -do-while loop -simple for

### Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

### Programming and Data Structure

Programming and Data Structure Sujoy Ghose Sudeshna Sarkar Jayanta Mukhopadhyay Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Spring Semester 2012 Programming and Data

### Computer Programming. Decision Making (2) Loops

Computer Programming Decision Making (2) Loops Topics The Conditional Execution of C Statements (review) Making a Decision (review) If Statement (review) Switch-case Repeating Statements while loop Examples

### These are reserved words of the C language. For example int, float, if, else, for, while etc.

Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

### Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Programming and Data Structure Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Shortcuts in Assignment Statements A+=C A=A+C A-=B A=A-B A*=D A=A*D A/=E

### Course Outline Introduction to C-Programming

ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,

### Chapter 4 C Program Control

1 Chapter 4 C Program Control Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 4 C Program Control Outline 4.1 Introduction 4.2 The Essentials of Repetition

### Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

### B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

### Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps 1 True and False in C False is represented by any zero value The int expression having the

### CS1100 Introduction to Programming

Decisions with Variables CS1100 Introduction to Programming Selection Statements Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Course Material SD, SB,

### Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop

### Chapter 3 Structured Program Development

1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

CS6202 - PROGRAMMING & DATA STRUCTURES UNIT I Part - A 1. W hat are Keywords? Keywords are certain reserved words that have standard and pre-defined meaning in C. These keywords can be used only for their

### CSCI 171 Chapter Outlines

Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

### Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

CS 156 : COMPUTER SYSTEM CONCEPTS TEST 1 (C PROGRAMMING PART) FEBRUARY 6, 2001 Student s Name: MAXIMUM MARK: 100 Time allowed: 45 minutes Note: unless otherwise stated, the questions are with reference

### Chapter 13 Control Structures

Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending

### C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the

### C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 1 By the end of this lecture, you will be able to identify

### Introduction. C provides two styles of flow control:

Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

### C Programming Multiple. Choice

C Programming Multiple Choice Questions 1.) Developer of C language is. a.) Dennis Richie c.) Bill Gates b.) Ken Thompson d.) Peter Norton 2.) C language developed in. a.) 1970 c.) 1976 b.) 1972 d.) 1980

### Repetition and Loop Statements Chapter 5

Repetition and Loop Statements Chapter 5 1 Chapter Objectives To understand why repetition is an important control structure in programming To learn about loop control variables and the three steps needed

### Conditional Expressions

Conditional Expressions Boolean Expressions: An expression that evaluates to either TRUE or FALSE. The most common types of boolean expressions are those that use relational operators. The general syntax

### Lecture 7 Tao Wang 1

Lecture 7 Tao Wang 1 Objectives In this chapter, you will learn about: Interactive loop break and continue do-while for loop Common programming errors Scientists, Third Edition 2 while Loops while statement

### ECET 264 C Programming Language with Applications. C Program Control

ECET 264 C Programming Language with Applications Lecture 7 C Program Control Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 7 - Paul I. Lin

### Conditionals and Loops

Conditionals and Loops Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing steps in a loop Chapter 5 focuses on: boolean expressions conditional

CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and

### Conditional Statement

Conditional Statement 1 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition Also called Branching How do we specify conditions?

### Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

### from Appendix B: Some C Essentials

from Appendix B: Some C Essentials tw rev. 22.9.16 If you use or reference these slides or the associated textbook, please cite the original authors work as follows: Toulson, R. & Wilmshurst, T. (2016).

### Pointers and scanf() Steven R. Bagley

Pointers and scanf() Steven R. Bagley Recap Programs are a series of statements Defined in functions Can call functions to alter program flow if statement can determine whether code gets run Loops can

### Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

### IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Subject code - CCP01 Chapt Chapter 1 INTRODUCTION TO C 1. A group of software developed for certain purpose are referred as ---- a. Program b. Variable c. Software d. Data 2. Software is classified into

### C Programming Class I

C Programming Class I Generation of C Language Introduction to C 1. In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) 2. In 1970, Ken Thompson created a language

### Chapter 5: Control Structures

Chapter 5: Control Structures In this chapter you will learn about: Sequential structure Selection structure if if else switch Repetition Structure while do while for Continue and break statements S1 2017/18

### 5. Selection: If and Switch Controls

Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation

### Expression and Operator

Expression and Operator Examples: Two types: Expressions and Operators 3 + 5; x; x=0; x=x+1; printf("%d",x); Function calls The expressions formed by data and operators An expression in C usually has a

### Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

### Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #43 Multidimensional Arrays In this video will look at multi-dimensional arrays. (Refer Slide Time: 00:03) In

### Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

### 2. Numbers In, Numbers Out

COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break

### DECISION CONTROL AND LOOPING STATEMENTS

DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of

### 3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

3. EXPRESSIONS It is a sequence of operands and operators that reduce to a single value. Operator : It is a symbolic token that represents an action to be taken. Ex: * is an multiplication operator. Operand:

### Department of Computer Science

Department of Computer Science Definition An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and

### Programming for Engineers Iteration

Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

### Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Lecture 02 Summary C/Java Syntax Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 1 2 By the end of this lecture, you will be able to identify the

### sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

INPUT AND OUTPUT IN C Function: printf() library: sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ; format_string it is

### COP 2000 Introduction to Computer Programming Mid-Term Exam Review

he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can

### Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Code: DC-05 Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 NOTE: There are 11 Questions in all. Question 1 is compulsory and carries 16 marks. Answer to Q. 1. must be written in the space

### UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . \$, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

Flow Control Comments Comments: /* This is a comment */ Use them! Comments should explain: special cases the use of functions (parameters, return values, purpose) special tricks or things that are not

### UNIT 3 OPERATORS. [Marks- 12]

1 UNIT 3 OPERATORS [Marks- 12] SYLLABUS 2 INTRODUCTION C supports a rich set of operators such as +, -, *,,

### CSE 5A Final Fall 2006

Student ID cs5f Name Signature CSE 5A Final Fall 2006 Page 1 (18 points) Page 2 (26 points) Page 3 (28 points) Page 4 (16 points) Page 5 (40 points) Page 6 (44 points) Total (172 points = 164 points +

### Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Lesson #4 Logical Operators and Selection Statements Control Structures Control structures combine individual instructions into a single logical unit with one entry point at the top and one exit point

### Structured Program Development

Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

### Unit 3. Operators. School of Science and Technology INTRODUCTION

INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

### Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

### Fundamentals of Computer Programming Using C

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar Faculty Name: Ami D. Trivedi Class: FYBCA Subject: US01CBCA01 (Fundamentals of Computer Programming Using C) *UNIT 3 (Structured Programming, Library Functions

### C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

C Program Control Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline The for repetition statement switch multiple selection statement break

### Chapter 3. Selections

Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

### Dept. of CSE, IIT KGP

Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

### Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Outline Computer programming Debugging Hints Gathering evidence Common C errors "Education is a progressive discovery of our own ignorance." Will Durant T.U. Cluj-Napoca - Computer Programming - lecture

### Data Type Fall 2014 Jinkyu Jeong

Data Type Fall 2014 Jinkyu Jeong (jinkyu@skku.edu) 1 Syntax Rules Recap. keywords break double if sizeof void case else int static... Identifiers not#me scanf 123th printf _id so_am_i gedd007 Constants

### Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Day05 A Young W. Lim 2017-10-07 Sat Young W. Lim Day05 A 2017-10-07 Sat 1 / 14 Outline 1 Based on 2 Structured Programming (2) Conditions and Loops Conditional Statements Loop Statements Type Cast Young

### CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

### This exam is to be taken by yourself with closed books, closed notes, no calculators.

Student ID CSE 5A Name Final Signature Fall 2004 Page 1 (12) cs5a This exam is to be taken by yourself with closed books, closed notes, no calculators. Page 2 (33) Page 3 (32) Page 4 (27) Page 5 (40) Page

### Control structures in C. Going beyond sequential

Control structures in C Going beyond sequential Flow of control in a program Start (main) Program starts with the first statement in main Instructions proceed sequentially: One at a time Top to bottom

### C-LANGUAGE CURRICULAM

C-LANGUAGE CURRICULAM Duration: 2 Months. 1. Introducing C 1.1 History of C Origin Standardization C-Based Languages 1.2 Strengths and Weaknesses Of C Strengths Weaknesses Effective Use of C 2. C Fundamentals

### Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)