Philip Bernhard, PhD Spring 2018

Slides:



Advertisements
Similar presentations
For loops For loops are controlled by a counter variable. for( c =init_value;c
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 TDBA66, VT-03, Lecture - Ch. 5 Repetition Loops are common in programming (computers can do boring things over and over again) Type of loopWhen usedC-structures.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
CONTROLLING PROGRAM FLOW
Introduction to C Programming CE Lecture 4 Further Control Structures in C.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
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;
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Iteration statement while do-while
Programming Fundamentals
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Repetition while, do-while, and for loops revisited
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Chapter 13 Control Structures
Looping.
Outline Altering flow of control Boolean expressions
C Programming Variables.
Lec 7.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
EKT150 : Computer Programming
Chapter 8 The Loops By: Mr. Baha Hanene.
Repetition and Loop Statements
Incremental operators
UMBC CMSC 104 – Section 01, Fall 2016
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
CprE 185: Intro to Problem Solving (using C)
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Iteration Statement for
CprE 185: Intro to Problem Solving (using C)
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
Looping Structures.
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 13 Control Structures
Presentation transcript:

Philip Bernhard, PhD Spring 2018 CSE 1002 Fundamentals of Software Development 2 Chapter 6 – C Control Statements Philip Bernhard, PhD Spring 2018

C Control Statements: Loops Three types of loops: for while do while What’s the difference?

C Control Statements: Loops Theoretically, equivalent. Preferable in different situations: for: iterating over a fixed, discrete range while: iterating until some (arbitrary) condition becomes false do while: same as a while-loop, body is guaranteed to be executed at least once.

What does this do? /* summing.c -- sums integers entered interactively */ #include <stdio.h> int main(void) { long num; long sum = 0L; /* initialize sum to zero */ int status; printf("Please enter an integer to be summed "); printf("(q to quit): "); status = scanf("%ld", &num); while (status == 1) { /* == means "is equal to" */ sum = sum + num; printf("Please enter next integer (q to quit): "); } printf("Those integers sum to %ld.\n", sum); return 0;

Sample Execution Please enter an integer to be summed (q to quit): 44 Please enter next integer (q to quit): 33 Please enter next integer (q to quit): 88 Please enter next integer (q to quit): 121 Please enter next integer (q to quit): q Those integers sum to 286.

Avoid Complicated Conditions main(argc, argv) int argc; char **argv; { while ((*argv != argv[1] && (*argv = argv[1]) && (argc = 0) || (*++argv && (**argv && ((++argc)[*argv] && (**argv <= argc[*argv] || (**argv += argc[*argv] -= **argv = argc[*argv] - **argv)) && --argv || putchar(**argv) && ++*argv--) || putchar(10))))); }

Which is More Readable? int n = 0; while (n++ < 3) printf("n is %d\n", n); int n = 1; while (n <= 3) { n++; }

Relational Operators < less than <= less than or equal to == equal to >= greater than or equal to > greater than != not equal to

Do Not Use == With Real Types // cmpflt.c -- floating-point comparisons #include <math.h> #include <stdio.h> int main(void) { const double ANSWER = 3.14159; double response; printf("What is the value of pi?\n"); scanf("%lf", &response); while (fabs(response - ANSWER) > 0.0001) { printf("Try again!\n"); } printf("Close enough!\n"); return 0;

Sample Execution What is the value of pi? 3.14 Try again! 3.1416 Close enough!

The For Loop // sweetie2.c -- a counting loop using for #include <stdio.h> int main(void){ const int NUMBER = 22; int count; for (count = 1; count <= NUMBER; count++) { printf("Be my Valentine!\n"); printf(“For the %dth Time!\n“,count); } return 0;

Sample Execution Be my Valentine! Request #1 Request #2 : Request #22

The Over-Complicated For Loop // sweetie2.c -- a counting loop using for // Some C programmers love this crap! #include <stdio.h> int main(void){ const int NUMBER = 22; int count; for (count = 1; count <= NUMBER; printf("Be my Valentine!\n"), printf(“For the %dth Time!\n“,count), count++) ; return 0; }

And Another One… /* zeno.c -- series sum */ #include <stdio.h> int main(void) { int t_ct; // term count double time, power_of_2; int limit; printf("Enter the number of terms you want: "); scanf("%d", &limit); for (time=0, power_of_2=1, t_ct=1; t_ct <= limit; t_ct++, power_of_2 *= 2.0) time += 1.0/power_of_2; printf("time = %f when terms = %d.\n", time, t_ct); } return 0;

Sample Execution Here is the output for 15 terms: Enter the number of terms you want: 15 time = 1.000000 when terms = 1. time = 1.500000 when terms = 2. time = 1.750000 when terms = 3. time = 1.875000 when terms = 4. time = 1.937500 when terms = 5. time = 1.968750 when terms = 6. time = 1.984375 when terms = 7. time = 1.992188 when terms = 8. time = 1.996094 when terms = 9. time = 1.998047 when terms = 10. time = 1.999023 when terms = 11. time = 1.999512 when terms = 12. time = 1.999756 when terms = 13. time = 1.999878 when terms = 14. time = 1.999939 when terms = 15.

Another Version /* zeno.c -- series sum */ #include <stdio.h> int main(void) { int t_ct; // term count double time, power_of_2; int limit; printf("Enter the number of terms you want: "); scanf("%d", &limit); time=0; power_of_2=1; for (t_ct=1; t_ct <= limit; t_ct++) { time += 1.0/power_of_2; printf("time = %f when terms = %d.\n", time, t_ct); power_of_2 *= 2.0; } return 0;

The do-while Loop /* do_while.c -- exit condition loop */ #include <stdio.h> int main(void){ const int secret_code = 13; int code_entered; do { printf("To enter the triskaidekaphobia therapy club,\n"); printf("please enter the secret code number: "); scanf("%d", &code_entered); } while (code_entered != secret_code); printf("Congratulations! You are cured!\n"); return 0; }

Sample Execution To enter the triskaidekaphobia therapy club, please enter the secret code number: 12 please enter the secret code number: 14 please enter the secret code number: 13 Congratulations! You are cured!

Nested Loops /* rows1.c -- uses nested loops */ #include <stdio.h> #define ROWS 6 #define CHARS 10 int main(void) { int row; char ch; for (row = 0; row < ROWS; row++) for (ch = 'A'; ch < ('A' + CHARS); ch++) printf("%c", ch); printf("\n"); } return 0;

Sample Execution ABCDEFGHIJ

Nested Loops // rows2.c -- using dependent nested loops #include <stdio.h> int main(void){ const int ROWS = 6; const int CHARS = 6; int row; char ch; for (row = 0; row < ROWS; row++){ for (ch = ('A' + row); ch < ('A' + CHARS); ch++) printf("%c", ch); printf("\n"); } return 0;

Sample Execution ABCDEF BCDEF CDEF DEF EF F

Nested Loops – Mixing Loop Types // rows2.c -- using dependent nested loops #include <stdio.h> int main(void){ const int ROWS = 6; const int CHARS = 6; int row; char ch; for (row = 0; row < ROWS; row++){ ch = 'A' + row; while (ch < ('A' + CHARS)) { printf("%c", ch); ch++; } printf("\n"); return 0;

Nested Loops – Mixing Loop Types // rows2.c -- using dependent nested loops #include <stdio.h> int main(void){ const int ROWS = 6; const int CHARS = 6; char ch; int row = 0; while (row < ROWS){ for (ch = ('A' + row); ch < ('A' + CHARS); ch++) printf("%c", ch); printf("\n"); row++; } return 0;