Lecture 8. Daily Puzzle

Size: px
Start display at page:

Download "Lecture 8. Daily Puzzle"

Transcription

1 Lecture 8 if and switch Statements Daily Puzzle If a bottle, partly filled with liquid, has a round, square or rectangular bottom which is flat, can you find its volume using only a ruler? You may not add or pour out liquid.

2 Daily Puzzle Solution: area of base (h 1 +h 2 ) Announcements Assignment sheet error- no two player mode for mugwump Assignment hint sheets posted on website Assignment grading will be the week AFTER the due date - TAs do the grading (not instructors) - will be able to start booking times after the break Next week's labs are a practise lab exam - Monday and Tuesday people can go to other lab times or to extra lab times (will be posted)

3 Announcements Prof. McCuaig has several raspberry pi kits still and any student who needs one may purchase one today between 2:30 and 4:30 in her office (Reynolds 311) Q&A Do you have a question? (I may have an answer)

4 if statements What happens here? a = -12; if (a > 0) printf( a is positive\n ); b = log(a); if statements If a is greater than 0, it prints a is positive. It calculates the log of a regardless of the outcome of the if statement.

5 if statements To fix this, use a compound statement! a = -12; if (a > 0) { printf( a is positive\n ); b = log(a); if statements NOTE: No semicolons after the curly braces!

6 if-else statements No Is the condition true? Yes if-else statements The simplest possible if-else statement takes the form:! if (expression)!!! statement1;!!! else!!! statement2;

7 if-else statements statement1 is executed if expression has a nonzero value. If expression has a value of zero, the statement2 is executed. if-else example #include <stdio.h> int main(void) { int a_number; printf( Enter a number: ); scanf( %d, &a_number); if (a_number % 2 == 0) printf( %d is even\n, a_number); else printf( %d is odd\n, a_number); return 0;

8 if-else statements There are *many* different combinations: if (expression) if (expression){ statement1; statement1a; else { statement1b; statement2a; statement2b; else statement2; if-else statements For example:!! if (x < y)!! min = x;!! else!! min = y; If x < y is true, then min is assigned the value of x; if it is false, min is assigned the value of y.

9 if-else statements For example: if (denominator == 0.00)!! printf( Divide by zero );! else!! f = numerator/denominator; if-else statements If the value of the variable denominator is less than zero, an error message is printed otherwise the value of f is computed.

10 if-else statements /* Find the minimum of two values */ #include <stdio.h> int main(void) { int x, y, min; printf( Input two integers: ); scanf( %d%d, &x, &y); if (x < y) min = x; else min = y; printf( The minimum value is %d\n, min); return 0; if-else statements An example of a syntax error:! if ( a!= b) {! a = a + 1;! b = b + 1;! ;! else! c = a + b;

11 if-else statements The syntax error occurs because the semicolon following the right brace creates an empty statement, and consequently the else has nowhere to attach! dangling else There is an ambiguity when an else is omitted from a nested if statement. This is the dangling else problem.! if (n > 0)! if (a > b)!! c = a;! else!! c = b;

12 dangling else For example consider a conditional statement to calculate shipping charges: 1. To ship a 1 lb packet within Canada, the cost is $5, except for Yukon. To ship to Yukon (which is very remote), the cost $ To ship a 1 lb packet outside Canada, the cost is $20 dangling else The following is an if-statement that computes the shippingcharge : double shippingcharge; shippingcharge = 5.00; if ( country == "CANADA") if ( province == "YT" ) shippingcharge = 10.00; else shippingcharge = 20.00;

13 dangling else Can be syntactically interpreted in two ways: double shippingcharge; shippingcharge = 5.00; if ( country == "CANADA") if ( province == "YT" ) shippingcharge = 10.00; else shippingcharge = 20.00; double shippingcharge; shippingcharge = 5.00; if ( country == "CANADA") if ( province == "YT" ) shippingcharge = 10.00; else shippingcharge = 20.00; = ambiguity dangling else C interprets it the following way: double shippingcharge; shippingcharge = 5.00; if ( country == "CANADA") if ( province == "YT" ) shippingcharge = 10.00; else shippingcharge = 20.00;

14 dangling else C associates the else with the closest previous else-less if. dangling else If that s not what you want, use braces to force the proper association.! if (n > 0) {! if (a > b)!! c = a;!! else!! c = b;

15 dangling else C interprets it the following way: double shippingcharge; shippingcharge = 5.00; if ( country == "CANADA") { if ( province == "YT" ) shippingcharge = 10.00; else shippingcharge = 20.00; dangling else /* Find the maximum of three real values */ #include <stdio.h> int main(void) { double x, y, z, max; printf( Input three real numbers: ); scanf( %lf%lf%lf, &x, &y, &z); if (x > y) if (x > z) max = x; else max = z; else if ( y < z) max = z; else max = y; printf( The maximum value is %f\n, max); return 0;

16 nested if Sometimes the if-else statement is used for a multi-way decision. The expressions are evaluated in order; if any expression is true, the statement associated with it is executed, and this statement terminates the whole chain. nested if The last else can be used to handle the default case where none of the other conditions is satisfied.

17 nested if if (expression1) statement1; else if (expression2) statement2; else if (expression3) statement3;... else statementn; nested if int posn=0, negn=0, zeron=0; if (x > 0) posn = posn + 1; else if (x < 0) negn = negn + 1; else zeron = zeron + 1;

18 nested if if (score == 100) printf( Superb\n ); else if (score >= 90) printf( Excellent\n ); else if (score >= 80) printf( Very good\n ); else if (score >= 70) printf( Good\n ); else if (score >= 60) printf( Satisfactory\n ); else if (score >= 50) printf( Pass\n ); else printf( Fail\n ); what happens here? In mathematics the expression 2<k<7 means that k is greater than 2, and less than 7. int k = 8; if (2 < k < 7) printf( true ); else printf( false );

19 what happens here? true is always printed. Why? 2 < k < 7 is equivalent to (2 < k) < 7 Because 2 < k is true, its value is 1. Therefore 2 < k < 7 is equivalent to 1 < 7 Which is obviously true. what happens here? The correct way to write a test is: 2 < k && k < 7 which is equivalent to: ((2 < k) && (k < 7))

20 switch statements The switch statement is a multiway conditional statement generalizing the ifelse statement. It is a far neater way of writing multiple if statements. switch statements The switch statement evaluates the value of an expression and branches to one of the case labels. Duplicate labels are not allowed, so only one case will be selected. The expression must evaluate an integer, or character.

21 switch statements Is condition 1 true? no yes break Is condition 2 true? no yes break default switch statements switch (expression){ case label1: statement1; case label2: statement2;... case labeln: statementn; default: default_statement;

22 switch statements The case labels can be in any order and must be integers. switch statements How does it work? Evaluate a switch expression. Go to the case label having a value that matches the value of the expression found in step 1.

23 switch statements If a match is not found, go to the default label; if there is no default label, terminate the switch. Terminate the switch when a break is encountered or by falling off the end. default label The default label can be put anywhere in the switch. When C sees a switch statement, it evaluates the expression and then looks for a matching case label. If none is found, the default label is used.

24 default label There may be at most one default label in a switch. Typically, it occurs last although it can occur anywhere. break statement A break statement inside a switch means that execution will continue after the switch statement. If a break statement is not there, execution falls through to the next statement.

25 break statement switch (expression){ case label1: statement1; break; case label2: statement2; break;... case labeln: statementn; break; default: default_statement; break statement The break statement interrupts the normal flow of control. switch (x) { case 1: x = x + 1; case 2: x = x + 2; break; // exit switch statement case 3: x = x + 3; default: x = 0; // break jumps to here

26 falling through At the end of the first case, there is no break, so the program falls through. Therefore, when x=1, the following statements will be executed: x = x + 1;! x = x + 2; falling through End every case in a switch with a break or the comment //Fall through switch (x) { case 1: x = x + 1; // Fall through case 2: x = x + 2; break; // exit switch statement case 3: x = x + 3; // Fall through default: x = 0;

27 if switch int x, y, r; if (y == 1) r = x; else if (y == 2) r = x * x; else if (y == 3) r = x * x * x; else printf( not a valid calculation ); if switch int x, y, r; switch (y) { case 1 : r = x; break; case 2 : r = x * x; break; case 3 : r = x * x * x; break; default : printf( not a valid calculation );

28 not always switch 52 lines of case statements switch (score) { case 100 : printf( Superb\n ); break; case 99 : ; case 98 : ;... case 90 : printf( Excellent\n ); break;... case 59 : ; case 58 : ;... case 50 : printf( Pass\n ); break; default : printf( Fail\n ); Sometimes switch is not the most appropriate structure Case study: quadratic

29 Case study: quadratic Input: a, b, c will cause a divide by zero error true if a = 0? false calculate Case study: quadratic if (a == 0) printf( Error: DIV by zero\n"); else { A = 2 * a; D = b * b - 4 * a * c; S = sqrt(fabs(d));

30 Case study: quadratic Input: a, b, c true if a = 0? false calculate false if b = 0? true calculate trivial case no roots ➊ further processing 1 root Case study: quadratic if (a == 0) if (b == 0){ printf( Sorry, no roots\n"); else { x = -c / b; printf("linear equation: root = %.2f\n", x); else { A = 2 * a; D = b * b - 4 * a * c; S = sqrt(fabs(d)); // more calculations

31 Case study: quadratic if (a == 0) if (b == 0){ printf( Sorry, no roots\n"); else { x = -c / b; printf("linear equation: root = %.2f\n", x); else { A = 2 * a; D = b * b - 4 * a * c; S = sqrt(fabs(d)); // more calculations Case study: quadratic calculate calculate 2 complex roots true if D < 0? false calculate single real root true if D = 0? false calculate 2 real roots

32 Case study: quadratic if (a == 0) if (b == 0){ printf( Sorry, no roots\n"); else { x = -c / b; printf("linear equation: root = %.2f\n", x); else { A = 2 * a; D = b * b - 4 * a * c; S = sqrt(fabs(d)); if (D < 0){ // roots are complex if (D == 0){ // roots are real and equal if (D > 0){ // roots are real Case study: quadratic if (a == 0) if (b == 0){ printf( Sorry, no roots\n"); else { x = -c / b; printf("linear equation: root = %.2f\n", x); else { A = 2 * a; D = b * b - 4 * a * c; S = sqrt(fabs(d)); if (D < 0){ // roots are complex else if (D == 0){ // single real root else { // roots are real

33 Case study: quadratic if (D < 0){ xr = -b / A; xi = S / A; printf("complex roots: root1 = %.2f+%.2fi\n", xr, xi); printf(" root2 = %.2f-%.2fi\n", xr, xi); else if (D == 0){ x = -b / A; printf("single root = %.2f\n", x); else { x1 = (-b + S) / A; x2 = (-b + S) / A; printf("real roots: root1 = %.2f\n", x1); printf(" root2 = %.2f\n", x2);

Computer Programming. Decision Making (2) Loops

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

More information

Chapter 5: Control Structures

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

More information

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

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

More information

Lecture 10. Daily Puzzle

Lecture 10. Daily Puzzle Lecture 10 Daily Puzzle Imagine there is a ditch, 10 feet wide, which is far too wide to jump. Using only eight narrow planks, each no more than 9 feet long, construct a bridge across the ditch. Daily

More information

Chapter 4. Flow of Control

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

More information

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Flow of Control. Flow of control The order in which statements are executed. Transfer of control 1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Ahmet Selman Bozkir, Gultekin Isik, Yasin Sahin 1 Today Condi/onal Branching Logical Expressions

More information

DECISION MAKING STATEMENTS

DECISION MAKING STATEMENTS DECISION MAKING STATEMENTS If, else if, switch case These statements allow the execution of selective statements based on certain decision criteria. C language provides the following statements: if statement

More information

Control Structure: Selection

Control Structure: Selection Control Structure: Selection Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure Selection Structure Single

More information

MODULE 2: Branching and Looping

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

More information

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

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

More information

Programming for Electrical and Computer Engineers. Loops

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

More information

Flow Control. CSC215 Lecture

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

More information

Control Statements. If Statement if statement tests a particular condition

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

More information

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 15 Branching : IF ELSE Statement We are looking

More information

Lecture 6. Statements

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

More information

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

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

More information

Decision Making and Loops

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

More information

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu Physics 2660: Fundamentals of Scientific Computing Lecture 5 Instructor: Prof. Chris Neu (chris.neu@virginia.edu) Reminder I am back! HW04 due Thursday 22 Feb electronically by noon HW grades are coming.

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

Lecture 5 Tao Wang 1

Lecture 5 Tao Wang 1 Lecture 5 Tao Wang 1 Objectives In this chapter, you will learn about: Selection criteria Relational operators Logical operators The if-else statement Nested if statements C++ for Engineers and Scientists,

More information

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

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

More information

Conditional Statement

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?

More information

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.

More information

DECISION CONTROL AND LOOPING STATEMENTS

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

More information

Introduction to C Programming

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

More information

COMP 208 Computers in Engineering

COMP 208 Computers in Engineering COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP 208 - Lecture 14 1 Review: basics of C C is case sensitive 2 types of comments: /* */,

More information

Conditions and Logical Expressions. C Programming

Conditions and Logical Expressions. C Programming Conditions and Logical Expressions C Programming Lecture Topics Using Relational and Logical Operators to Construct and Evaluate Logical Expressions If-Else Statements Flow of Control is Sequential unless

More information

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

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

More information

Course Outline Introduction to C-Programming

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,

More information

Control structures in C. Going beyond sequential

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

More information

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM

More information

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

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

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 04 - Conditionals Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Control Structure Conditions

More information

Conditional Statement

Conditional Statement Conditional Statement 1 Logical Expressions n Uses relational and logical operators n Informally, specifies a condition which can be true or false n Evaluates to value 0 or 1 0 implies the condition is

More information

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

More information

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

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

More information

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis. Birla Institute of Technology & Science, Pilani Computer Programming (CSF111) Lab-4 ---------------------------------------------------------------------------------------------------------------------------------

More information

Decision Making in C

Decision Making in C Decision Making in C Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed

More information

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input CpSc Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input Overview For this lab, you will use: one or more of the conditional statements explained below scanf() or fscanf() to read

More information

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

More information

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso 1 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop 2 A repetition structureallows

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

5. Selection: If and Switch Controls

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

More information

Introduction. C provides two styles of flow control:

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

More information

Introduction to C. CS 2060 Week 1. Prof. Jonathan Ventura. Outline Introduction Introduction to C Homework 1 C Coding Style Wrap-up

Introduction to C. CS 2060 Week 1. Prof. Jonathan Ventura. Outline Introduction Introduction to C Homework 1 C Coding Style Wrap-up Outline Introduction Homework 1 C Coding Style Wrap-up CS 2060 Week 1 Outline Introduction Homework 1 C Coding Style Wrap-up 1 Introduction Why C? Syllabus and Course Structure 2 First C example: Hello,

More information

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem. 60-140-1 Lecture for Thursday, Sept. 18, 2014. *** Dear 60-140-1 class, I am posting this lecture I would have given tomorrow, Thursday, Sept. 18, 2014 so you can read and continue with learning the course

More information

SELECTION. (Chapter 2)

SELECTION. (Chapter 2) SELECTION (Chapter 2) Selection Very often you will want your programs to make choices among different groups of instructions For example, a program processing requests for airline tickets could have the

More information

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14 Statements Control Flow Statements Based on slides from K. N. King Bryn Mawr College CS246 Programming Paradigm So far, we ve used return statements and expression statements. Most of C s remaining statements

More information

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C School of Electrical and Computer Engineering Cornell University revision: 2018-09-03-15-59 1 Statements, Syntax, Semantics, State

More information

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17) BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17) 1 Learning Outcomes At the end of this lecture, you should be able to: 1. Explain the concept of selection control structure

More information

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point? CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point? Overview For this lab, you will use: one or more of the conditional statements explained below scanf()

More information

More examples for Control statements

More examples for Control statements More examples for Control statements C language possesses such decision making capabilities and supports the following statements known as control or decision-making statements. 1. if statement 2. switch

More information

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS Objectives By the end of this section you should be able to:

More information

BRANCHING if-else statements

BRANCHING if-else statements BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional

More information

Module 4: Decision-making and forming loops

Module 4: Decision-making and forming loops 1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

More information

Decisions. Arizona State University 1

Decisions. Arizona State University 1 Decisions CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 4 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

Identify skills and personality traits of successful problem solving. Apply standard problem-solving techniques to aid in problem solving.

Identify skills and personality traits of successful problem solving. Apply standard problem-solving techniques to aid in problem solving. Identify skills and personality traits of successful problem solving. Apply standard problem-solving techniques to aid in problem solving. Apply problem-solving techniques to programming activities. Generate

More information

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Theory of control structures

Theory of control structures Theory of control structures Paper written by Bohm and Jacopini in 1966 proposed that all programs can be written using 3 types of control structures. Theory of control structures sequential structures

More information

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Decisions Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To be able to implement decisions using if statements To learn

More information

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

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

More information

Following is the general form of a typical decision making structure found in most of the programming languages:

Following is the general form of a typical decision making structure found in most of the programming languages: Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined

More information

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso 1 Topics Multiple Selection vs Binary Selection switch Statement char data type and getchar( ) function Reading newline characters 2 So far, we have

More information

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++ Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

More information

Decision Making and Branching

Decision Making and Branching INTRODUCTION Decision Making and Branching Unit 4 In the previous lessons we have learned about the programming structure, data types, declaration of variables, tokens, constants, keywords and operators

More information

Physics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu

Physics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu Physics 2660: Fundamentals of Scientific Computing Lecture 7 Instructor: Prof. Chris Neu (chris.neu@virginia.edu) Reminder HW06 due Thursday 15 March electronically by noon HW grades are starting to appear!

More information

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable CS 201 Selection Structures (2) and Repetition Debzani Deb Multiple-Alternative Decision Form of Nested if Nested if statements can become quite complex. If there are more than three alternatives and indentation

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes Overview For this lab, you will use: one or more of the conditional statements explained below

More information

Chapter 8. More Control Statements

Chapter 8. More Control Statements Chapter 8. More Control Statements 8.1 for Statement The for statement allows the programmer to execute a block of code for a specified number of times. The general form of the for statement is: for (initial-statement;

More information

Programming Lecture 4

Programming Lecture 4 Five-Minute Review 1. What are classes and objects? What is a class hierarchy? 2. What is an expression? A term? 3. What is a variable declaration? 4. What is an assignment? What is precedence? 5. What

More information

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

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

More information

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm 1 16 quizzes taken Average: 37.9 Median: 40.5 Quiz 1 Results 16 Scores: 45 45 44 43 43 42 41 41 40 36 36 36 34 31 28 21 Avg

More information

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

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

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

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

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

More information

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details. Objective of this lecture: At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details. Fundamental Programming Structures in C

More information

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t. CS 221 Lecture Tuesday, 4 October 2011 There are 10 kinds of people in this world: those who know how to count in binary, and those who don t. Today s Agenda 1. Announcements 2. You Can Define New Functions

More information

Ch. 7: Control Structures

Ch. 7: Control Structures Ch. 7: Control Structures I. Introduction A. Flow of control can be at multiple levels: within expressions, among statements (discussed here), and among units. B. Computation in imperative languages uses

More information

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 17. Fundamental Concepts Expressed in JavaScript Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program

More information

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation: Control Structures Sequence Statements execute in sequence, one after the other, such as the following solution for a quadratic equation: double desc, x1, x2; desc = b * b 4 * a * c; desc = sqrt(desc);

More information

Lab - 8 Awk Programming

Lab - 8 Awk Programming Lab - 8 Awk Programming AWK is another interpreted programming language which has powerful text processing capabilities. It can solve complex text processing tasks with a few lines of code. Listed below

More information

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

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

More information

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP Administration Conditional Statements CS 99 Summer 2000 Michael Clarkson Lecture 4 Lab 2 due now on floppy Lab 3 due tomorrow via FTP need Instruct account password Lab 4 posted this afternoon Prelim 1

More information

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester Programming Language Control Structures: Selection (switch) Eng. Anis Nazer First Semester 2018-2019 Multiple selection choose one of two things if/else choose one from many things multiple selection using

More information

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions Announcements Thursday Extras: CS Commons on Thursdays @ 4:00 pm but none next week No office hours next week Monday or Tuesday Reflections: when to use if/switch statements for/while statements Floating-point

More information

Chapter 3 Structured Program Development

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

More information

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Dr. R. Z. Khan, Associate Professor, Department of Computer Science ALIGARH MUSLIM UNIVERSITY Department of Computer Science Course: CSM-102: Programming & Problem Solving Using C Academic Session 2015-2016 UNIT-2: Handout-3 Topic: Control Structures (Selection & Repetition)

More information

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

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

More information

UIC. C Programming Primer. Bharathidasan University

UIC. C Programming Primer. Bharathidasan University C Programming Primer UIC C Programming Primer Bharathidasan University Contents Getting Started 02 Basic Concepts. 02 Variables, Data types and Constants...03 Control Statements and Loops 05 Expressions

More information

Selection Structures II

Selection Structures II Lecture 7 Selection Structures II Nested if and switch statements CptS 121 Summer 2016 Armen Abnousi If () if() Nested if statements An if- statement can be placed inside another if- statement. Every matches

More information

Tail recursion. Decision. Assignment. Iteration

Tail recursion. Decision. Assignment. Iteration Computer Programming Tail recursion. Decision. Assignment. Iteration Marius Minea marius@cs.upt.ro 7 October 2014 Two ways of writing recursion unsigned max(unsigned a, unsigned b) { return a > b? a :

More information

Chapter 6. Decision and Control Statements

Chapter 6. Decision and Control Statements Chapter 6. Decision and Control Statements Once a decision was made, I did not worry about it afterward. Harry Truman Calculations and expressions are only a small part of computer programming. Decision

More information

Welcome to... CS113: Introduction to C

Welcome to... CS113: Introduction to C Welcome to... CS113: Introduction to C Instructor: Erik Sherwood E-mail: wes28@cs.cornell.edu Course Website: http://www.cs.cornell.edu/courses/cs113/2005fa/ The website is linked to from the courses page

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Lecture 5. Daily Puzzle. Variables and Identifiers. Place 10 lumps of sugar in 3 cups so there would be an odd number of lumps in every cup.

Lecture 5. Daily Puzzle. Variables and Identifiers. Place 10 lumps of sugar in 3 cups so there would be an odd number of lumps in every cup. Lecture 5 Variables and Identifiers Daily Puzzle Place 10 lumps of sugar in 3 cups so there would be an odd number of lumps in every cup. Announcements Lecture sequence on the course outline is changed

More information

Dept. of CSE, IIT KGP

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

More information