Program Control Structures

Similar documents
Introduction to the C Programming Language

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Program Design: Using the Debugger

Introduction. C provides two styles of flow control:

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

CSC 270 Survey of Programming Languages

COSC121: Computer Systems: Runtime Stack

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Week 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions. Alvin Valera

Decision Making and Loops

Control Flow, Functions and Basic Linkage

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

CSE 130 Introduction to Programming in C Control Flow Revisited

Slides adopted from T. Ferguson Spring 2016

CS 314 Principles of Programming Languages. Lecture 9

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

Lecture 5: C programming

Prepared by: Shraddha Modi

Linux environment. Graphical interface X-window + window manager. Text interface terminal + shell

SELECTION STATEMENTS:

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

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

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

A control structure refers to the way in which the Programmer specifies the order of executing the statements

CpSc 1111 Lab 4 Formatting and Flow Control

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

CSE 303 Lecture 8. Intro to C programming

Shared Memory Programming Model

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

EC 413 Computer Organization

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

Programming for Engineers Iteration

UIC. C Programming Primer. Bharathidasan University

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Introduction to C Part 1 HLL s and the Basic Syntax. (Ch11 & 12)

Topic 6: A Quick Intro To C

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Programming for Electrical and Computer Engineers. Loops

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

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

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

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

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

Decisions II. Switch Statement. If else allows a 2 way decision Switch allows for n-way decisions

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

Essar Placement Paper

Introduction to C An overview of the programming language C, syntax, data types and input/output

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Review of Scientific Programming in C and Fortran. Michael McLennan Software Architect HUBzero Platform for Scientific Collaboration

Compiling applications for the Cray XC

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

Subject: PIC Chapter 2.

CpSc 1111 Lab 5 Formatting and Flow Control

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Technical Questions. Q 1) What are the key features in C programming language?

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Problem Solving in C. Stepwise Refinement. ...but can stop refining at a higher level of abstraction. Same basic constructs. as covered in Chapter 6

2. Numbers In, Numbers Out

C introduction: part 1

DECISION CONTROL AND LOOPING STATEMENTS

EE/CSCI 451 Introduction to Parallel and Distributed Computation. Discussion #4 2/3/2017 University of Southern California

block structure, scope and macros

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Repetition and Loop Statements Chapter 5

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Fundamentals of Programming & Procedural Programming

Exercises for Introduction to Programming in C

Control Structure: Loop

Introduction to Computing Lecture 09: Functions (Part II)

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

Fundamental of Programming (C)

Embedded Systems. Introduction. The C Language. Introduction. Why C instead ASM. Introduction to C Embedded Programming language

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Introductory OpenMP June 2008

C, C++, Fortran: Basics

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

EL2310 Scientific Programming

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

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

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

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

Structured programming. Exercises 3

Chapter 9: Functions. Chapter 9. Functions. Copyright 2008 W. W. Norton & Company. All rights reserved.

MPLAB XC8 C Compiler Version 2.00 Release Notes for AVR MCU

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation

Transcription:

Program Control Structures Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email m.griffiths@sheffield.ac.uk

Control Sequence Structures Selection Structures if else statements switch structures Repetition Structures for loops while loops

Conditional Statements Using if else The if statement allows decision making functionality to be added to applications. General form of the if statement is: if(condition) statement;

Conditional Operators Compare values using conditional operators. == equal to > greater than < less than >= greater than or equal to <= less than or equal to

Using else An alternative form of the if statement is if(condition) statement; else statement; If the condition is true the first statement is executed if it is false the second statement is executed.

Simple if Example Demonstrating Syntax int main(void) { float f1; printf("enter a floating point number.\n"); scanf("%f",&f1); if(f1<0) printf("please enter a value greater 0\n"); else if (f1>100) printf("f1 is greater than 100\n"); else printf("the value is %f\n",f1); } return 0;

Demonstration Build and run the example if1.c and if2.c Note the condition operator in round brackets Use of {} when multiple statements are used Test the program with different conditions Build and run if3.c What is wrong with this code? Was there are compiler warning? Build and run the programs ifelse.c and ifelseifelse.c

Multiple selection Structures Using Switch Used for testing variable separately and selecting a different action switch(file) { case 'm': case 'M': ++nmaxima; break; case 't': case 'T': ++ntitania; break; default: /*Catch all other characters*/ ++nother; break; } /*End of file check switch */

Demonstration Build and run the example switch1.c What happens when the break statements are commented out (put a // at the start of the line) switch2.c is a menu application The program is case sensitive Modify the program so that the user can type either upper or lower case characters

Repetition Using while Execute commands until the conditions enclosed by the while statement return false. while(conditions) { statements; }

while Good practice to always use {} in a do while loop while(conditions) { statements ; Statements ; }

do while Good practice to always use {} in a do while loop do { statements ; Statements ; } while(conditions)

Demonstration Build and run the example while1.c Modify the loop so that it counts to 20 Modify the loop so that it counts to 20 in steps of 2 Build and run dowhile.c

While example demonstrating a countdown int main (void) { int n; printf( "Enter the starting number\n"); scanf("%d".&n); while (n>0) { printf("%d, ",n); --n; } printf("fire!\n"); return 0; } Try this code then modify it by introducing a second variable m initialised to 10. Use the && operator to add a test m<10 to the Condition in the while statement.

Example of while and if statement usage while(files<=5) { printf("enter file location(1=titania, 2=Maxima): "); scanf("%d", &result); if(result==1) ntitania_files = ntitania_files+1; else if(result==2) nmaxima_files = nmaxima_files+1; else nother_files=nother_files+1; Continue counting until Maximum number of files entered (5) Request and get user input Use conditions to update variables files++; }/*End of while file processing loop*/ Increment counter

Counter Controlled Repetition Components of a typical for loop structure for(expression1; expression2; expression3) statement; example for(counter=1; counter<=10, counter++) statement;

Demonstration Build and run the example for1.c Build and run nestedfor1.c

for loop example Example program for.c Modify the program so that it performs a count down main() { int counter, nsteps=10; /* initialisation, repetition condition and increment */ /* are all included in the for structure header */ for(counter=1; counter<=nsteps; counter++) printf("%d\n", counter); return 0; }

Practical Examples basic coding Inspect, Compile and run the following Finding a root using the Newton-Raphson method While statement Finding a root by method of bisection If statement, while statement And simple one line function!

Compilers Language GNU Portland Intel C gcc pgcc icc C++ g++ pgcc icpc Fortran 77 g77 pgf77 Fortran90/95 gfortran pgf90 ifort

Invoking the Compiler Compiling FORTRAN Programs g77 o mycode [options] mycode.f Compiling c/c++ Programs gcc o mycode [options] mycode.c

Compiler Options Option Action -s remove any symbol and object relocation information from the program. This is used to reduce the size of program and runtime overhead -c Compile, do not link. -o exefile Specifies a name for the resulting executable. -g Produce debugging information (no optimization). -Ilibrary_name (lower case L) -ldirectory-name (upper case I) Link the given library into the program. e.g. include math library by using option -lm Add directory to search path for include files -O N Set optimisation level to N -Dmacro[=defn] Define a macro

Summary Program Structure Control Structures for C Programming Compilers and development Environments