INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Control Structures Corresponds with Chapters 3 and 4.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are if and if-else statements the switch statement.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Control statements Mostafa Abdallah
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.
Controlling Program Flow with Decision Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
LOGICAL CONTROL STRUCTURES (chp. 8)
UMBC CMSC 104 – Section 01, Fall 2016
Control Structures and Data Files
Engineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C Fundamental Concepts
Lecturer CS & IT Department UOS MBDIN
JavaScript: Control Statements.
JavaScript: Control Statements I
The University of Texas – Pan American
Chapter 8 JavaScript: Control Statements, Part 2
Conditional Construct
Control Structures: Selection Statement
Professor Jodi Neely-Ritz CGS3460
Basic Concepts – Conditionals
3 Control Statements:.
Repetition Control Structure
Computer Science Core Concepts
Program Flow.
Control Statements Paritosh Srivastava.
Control Structures: Selection Statement
Chap 7. Advanced Control Statements in Java
REPETITION Why Repetition?
Chapter 8 JavaScript: Control Statements, Part 2
Structural Program Development: If, If-Else
Presentation transcript:

INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files

INTEC CS160 - Jeanine Ingber Algorithm Development

INTEC CS160 - Jeanine Ingber Structured Programming Sequence Selection Repetition yesno yes no

INTEC CS160 - Jeanine Ingber Conditional Expressions

INTEC CS160 - Jeanine Ingber Relational Operators ==equality !=non equality <less than >greater than <=less than equal to >=greater than equal to

INTEC CS160 - Jeanine Ingber Logical Operators !not &&and ||or

INTEC CS160 - Jeanine Ingber Operator Precedence 1 >= 2 == != 3 && 4 ||

INTEC CS160 - Jeanine Ingber Selection Statements

INTEC CS160 - Jeanine Ingber Selection Statements if if else switch

INTEC CS160 - Jeanine Ingber If statement if(Boolean expression) statement;//single statement if(Boolean expression) {//more than one statement statement1;. statement n; }

INTEC CS160 - Jeanine Ingber If statement - examples if (x>0) k++; if(x>0) { x=sqrt(x); k++; }

INTEC CS160 - Jeanine Ingber if - else statement if(Boolean expression) statement; else statement; if(Boolean expression) { statement block } else { statement block }

INTEC CS160 - Jeanine Ingber nested if-else if(x > y) if(y < z) k++; else m++; else j++;

INTEC CS160 - Jeanine Ingber Practice! int x=9, y=7, z=2, k=0, m=0, j=0; if(x > y) if(y < z) k++; else m++; else j++; What are the values of j, k and m?

INTEC CS160 - Jeanine Ingber Switch Statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); }

INTEC CS160 - Jeanine Ingber Switch Statement Expression must be of type integer or character The keyword case must be followed by a constant break statement is required unless you want all subsequent statements to be executed.

INTEC CS160 - Jeanine Ingber Practice! Convert the following nested if/else statements to a switch statement: if (rank==1 || rank==2) printf("Lower division \n"); else { if (rank==3 || rank==4) printf("Upper division \n"); else { if (rank==5) printf("Graduate student \n"); else printf("Invalid rank \n"); }

INTEC CS160 - Jeanine Ingber Loop Structures

INTEC CS160 - Jeanine Ingber repetition while statement do while statement for statement

INTEC CS160 - Jeanine Ingber while statement while(expression) statement; while(expression) { statement;. }

INTEC CS160 - Jeanine Ingber do while do statement; while(expression); do { statement1; statement2;. } while(expression); note - the expression is tested after the statement(s) are executed, so statements are executed at least once.

INTEC CS160 - Jeanine Ingber for statement for(initialization; test; increment/decrement) statement; for(initialization; test; increment/decrement) { statement;. }

INTEC CS160 - Jeanine Ingber for statement initalize test increment/ decrement true statement(s)

INTEC CS160 - Jeanine Ingber for statement - examples int sum =0; for(int I=1;I<10;I+=2) sum = sum + I; int fact =1; for(int n=5;n>1;n- -) fact = fact * n;

INTEC CS160 - Jeanine Ingber Practice! Determine the number of times that each of the following for loops are executed. for (k=3; k<=20; k++) { statements ; } for (k=3; k<=20; ++k) { statements ; } for (count=-2; count<=14; count++) { statements ; }

INTEC CS160 - Jeanine Ingber break statement break; –terminates loop –execution continues with the first statement following the loop

INTEC CS160 - Jeanine Ingber continue statement continue; –forces next iteration of the loop, skipping any remaining statements in the loop

INTEC CS160 - Jeanine Ingber Data Files

INTEC CS160 - Jeanine Ingber Data Files Each data file must have a file pointer –file pointer must be defined FILE *sensor1 ; FILE *balloon; –file pointer must be associated with a specific file using the fopen function sensor1 = fopen(sensor1.dat, r); balloon = fopen(balloon.dat, w);

INTEC CS160 - Jeanine Ingber I/O Statements Input file - use fscanf instead of scanf –fscanf(sensor1, %1f %lf, &t, &motion); Output file - use fprint instead of printf –fprintf(balloon, %f %f %f\n, time, height, velocity);

INTEC CS160 - Jeanine Ingber Reading Data Files counter controlled loop –for loop sentinel controlled loop –while loop end of file controlled loop –while loop