Program Control Structures

Slides:



Advertisements
Similar presentations
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Advertisements

C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
C++ for Engineers and Scientists Third Edition
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Computer Science 210 Computer Organization Introduction to C.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Chapter 4 Selection Structures: Making Decisions.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Chapter 5: Structured Programming
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Iterations Very Useful: Ability to repeat a block of code Example:
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Khalid Rasheed Shaikh Computer Programming Theory 1.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
The Repetition control structure using while loop.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
ECE Application Programming
Review 1.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Chapter 5: Structured Programming
Computer Science 210 Computer Organization
ECE Application Programming
ECE Application Programming
Lecture 7: Repeating a Known Number of Times
C Program Controls + Flow Structure
ECE Application Programming
The Selection Structure
Chapter 4 - Program Control
CS1010 Programming Methodology
Computer Science 210 Computer Organization
Chapter 11 Introduction to Programming in C
Structured Program
Chapter 4 - Program Control
Chapter 11 Introduction to Programming in C
Homework Any Questions?.
Computer Programming Techniques Semester 1, 1998
Chapter 11 Introduction to Programming in C
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Repetition Statements (Loops) - 2
Introduction to Programming
DATA TYPES There are four basic data types associated with variables:
Lecture 4: Selection Control Structure
Presentation transcript:

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 Repetition 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) 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 gerater 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; default: /*Catch all other characters*/ ++nOther; } /*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 Modify the loop so that is output

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 Continue counting until Maximum number of files entered (5) 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; files++; }/*End of while file processing loop*/ Request and get user input Use conditions to update variables 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) Link the given library into the program. e.g. include math library by using option -lm -ldirectory-name (upper case I) Add directory to search path for include files -O N Set optimisation level to N -Dmacro[=defn] Define a macro -s This option instructs the compiler to remove any symbol and object relocation information from the program. This is used to reduce the size of program and runtime overhead. Coupled with -O2, these options produced programs that are used in production. Note that this option should not be used in conjunction with -g as it removes the debugging information as well. 22

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