Introduction to C. Control Flow. Instructor: Yin Lou 01/26/2011. Introduction to C CS 2022, Spring 2011, Lecture 2

Similar documents
Senem Kumova Metin & Ilker Korkmaz 1

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Two-way selection. Branching and Looping

Computer Programming Tutorial

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

Lecture 2 Notes: Flow of Control

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Object Oriented Software Design

Statements and Control Flow

VB.NET Programming Fundamentals

CS 241 Data Organization Coding Standards

Object Oriented Software Design

Selection Statements

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

Sources: On the Web: Slides will be available on:

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

J a v a Quiz (Unit 3, Test 0 Practice)

Moving from C++ to VBA

arrays C Programming Language - Arrays

Example of a Java program

FEEG Applied Programming 5 - Tutorial Session

The C Programming Language course syllabus associate level

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 8 Selection 8-1

Decision Logic: if, if else, switch, Boolean conditions and variables

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Introduction to Java

6. Control Structures

Tutorial on C Language Programming

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Test case design techniques I: Whitebox testing CISS

Education: P.h.D. Candidate (Santa Clara University, California) M.S. in Computer Engineering (Santa Clara University, California)

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

INDIVIDUAL MASTERY for: St#: Test: CH 9 Acceleration Test on 29/07/2015 Grade: B Score: % (35.00 of 41.00)

PIC 10A. Lecture 7: Graphics II and intro to the if statement

INDIVIDUAL MASTERY for: St#: Test: CH 9 Acceleration Test on 09/06/2015 Grade: A Score: % (38.00 of 41.00)

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

JavaScript: Control Statements I

Java Basics: Data Types, Variables, and Loops

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Chapter 2 Introduction to Java programming

Conditionals: (Coding with Cards)

Appendix: Tutorial Introduction to MATLAB

#820 Computer Programming 1A

Computer Programming I & II*

Programming Project 1: Lexical Analyzer (Scanner)

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

C++FA 5.1 PRACTICE MID-TERM EXAM

Code convention document Argus-viewer

Computer Programming I

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Rigorous Software Development CSCI-GA

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

CS 106 Introduction to Computer Science I

Computer Programming I

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

The programming language C. sws1 1

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration

Objective-C Tutorial

Introduction to Java Lecture Notes. Ryan Dougherty

Informatica e Sistemi in Tempo Reale

if and if-else: Part 1

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Compiler Construction

Moving from CS 61A Scheme to CS 61B Java

Lumousoft Visual Programming Language and its IDE

Chapter 5. Selection 5-1

4/1/2017. PS. Sequences and Series FROM 9.2 AND 9.3 IN THE BOOK AS WELL AS FROM OTHER SOURCES. TODAY IS NATIONAL MANATEE APPRECIATION DAY

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Conditions & Boolean Expressions

Translating C code to MIPS

13 Classes & Objects with Constructors/Destructors

After: bmotorreflected[port2]= 1; //Flip port2 s direction

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Keil C51 Cross Compiler

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Statements and Control Flow

IC4 Programmer s Manual

Explicit Flow Control: break label, goto case and explicit switch

PHP Tutorial From beginner to master

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

C Coding Style Guide. Technotes, HowTo Series. 1 About the C# Coding Style Guide. 2 File Organization. Version 0.3. Contents

Functional Programming

CSC 221: Computer Programming I. Fall 2011

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

Part I. Multiple Choice Questions (2 points each):

Conditional Statements Summer 2010 Margaret Reid-Miller

Section 8.2 Solving a System of Equations Using Matrices (Guassian Elimination)

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Transcription:

Introduction to C Control Flow Instructor: Yin Lou 01/26/2011

Statements

Statements <statement> := <expression>; x = 0; ++i; printf("%d", x);

Blocks <block> := <statements> x = 0; ++i; printf("%d", x);

Blocks <block> := <statements> x = 0; ++i; printf("%d", x); A block is syntactically equivalent to a single statement.

Blocks <block> := <statements> x = 0; ++i; printf("%d", x); A block is syntactically equivalent to a single statement. if, else, while, for Variables can be declared inside any block. There is no semicolon after the right brace that ends a block.

Example int x = 0; int x = 5; printf("inside: x = %d\n", x); printf("outside: x = %d\n", x);

Example int x = 0; int x = 5; printf("inside: x = %d\n", x); printf("outside: x = %d\n", x); Inside: x = 5 Outside: x = 0

if Statement if (<condition>) <statement> // single statment if (2 < 5) printf("2 is less than 5.\n"); // block if (2 < 5) printf("i ll always print this line.\n"); printf("because 2 is always less than 5!\n");

if-else Statement if (<condition>) <statement1> else <statement2> if (x < 0) printf("%d is negative.\n", x); else printf("%d is non-negative.\n", x);

else-if Statement if (a < 5) printf("a < 5\n"); else if (a < 8) printf("5 <= a < 8\n"); else printf("a >= 8\n"); if (a < 5) printf("a < 5\n"); else if (a < 8) printf("5 <= a < 8\n"); else printf("a >= 8\n");

if-else Statement Pitfalls if (a > 70) if (a > 80) printf("grade = B\n"); else printf("grade < B\n"); printf("fail.\n"); printf("done.\n"); if (a > 70) if (a > 80) printf("grade = B\n"); else printf("grade < B\n"); printf("fail.\n"); printf("done.\n");

Relational Operators C has the following relational operators a == b true iff a equals b a!= b true iff a does not equal b a < b true iff a is less than b a > b true iff a is greater than b a <= b true iff a is less than or equal to b a >= b true iff a is greater than or equal to b a && b true iff a is true and b is true a b true iff a is true or b is true!a true iff a is false

Booleans in C C DOES NOT have a boolean type. Instead, conditional operators evaluate to integers (int) 0 indicates false. Non-zero value is true. if (<condition>) checks whether the condition is non-zero.

Booleans in C C DOES NOT have a boolean type. Instead, conditional operators evaluate to integers (int) 0 indicates false. Non-zero value is true. if (<condition>) checks whether the condition is non-zero. Programmer must be very careful to this point!

Booleans in C C DOES NOT have a boolean type. Instead, conditional operators evaluate to integers (int) 0 indicates false. Non-zero value is true. if (<condition>) checks whether the condition is non-zero. Programmer must be very careful to this point! Examples if (3) printf("true.\n"); if (!3) // unreachable code if (a = 5) // always true, potential bug (a == 5) int a = (5 == 5); // a = 1

Conditional expressions <condition>? <expression1> : <expression2> grade = (score >= 70? S : U ); printf("you have %d item%s.\n", n, n == 1? "" : "s");

Conditional expressions <condition>? <expression1> : <expression2> grade = (score >= 70? S : U ); printf("you have %d item%s.\n", n, n == 1? "" : "s"); Conditional expression often leads to succinct code.

switch Statement A common form of if statement if (x == a) statement1; else if (x == b) statement2;... else statement0;

switch Statement A common form of if statement if (x == a) statement1; else if (x == b) statement2;... else statement0; switch statement switch (x) case a: statement1; break; case b: statement2; break;... default: statement0;

More on switch Statement Fall-through property int month = 2; switch (month) case 1: printf("jan.\n"); break; case 2: printf("feb.\n"); case 3: printf("mar.\n"); default: printf("another month.\n");

More on switch Statement Fall-through property int month = 2; switch (month) case 1: printf("jan.\n"); break; case 2: printf("feb.\n"); case 3: printf("mar.\n"); default: printf("another month.\n"); Feb. Mar. Another month.

More on switch Statement Fall-through property int month = 2; int days; switch (month) case 2: days = 28; break; case 9: case 4: case 6: case 11: days = 30; break; default: days = 31;

More on switch Statement Fall-through property int month = 2; int days; switch (month) case 2: days = 28; break; case 9: case 4: case 6: case 11: days = 30; break; default: days = 31; It s always recommended to have default, though it s optional.

while Loop while (<condition>) <statement>

while Loop while (<condition>) <statement> If the condition is initially false, the statement is never executed.

while Loop while (<condition>) <statement> If the condition is initially false, the statement is never executed. do <statement> while (<condition>);

while Loop while (<condition>) <statement> If the condition is initially false, the statement is never executed. do <statement> while (<condition>); The statement is executed at least one.

for Loop for (<exp1>; <exp2>; <exp3>) <statement> exp1; while (exp2) statement exp3; for (i = 0; i < n; ++i) // do something

Infinite Loop while (1) // do something for (;;) // do something

Infinite Loop while (1) // do something for (;;) // do something Both are okay, but for may lead to fewer machine code on some platform, which means it is slightly more efficient.

break and continue break int n = 10; while (1) if (!n) break; --n; continue int i; for (i = 0; i < 10; ++i) if (i == 0) continue; printf("%d\n", i);

Common Pitfalls int i; for (i = 0; i < 10; ++i); printf("%d\n", i); int n = 10; while (n = 1) printf("%d\n", n); --n;

How to Avoid Bugs? Always use int i; for (i = 0; i < 10; ++i) printf("%d\n", i); Put literals on the left int n = 10; while (1 == n) // 1 = n, compilation error printf("%d\n", n); --n;