Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Objectives You should be able to describe:
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Week 3 - Friday.  What did we talk about last time?  Bitwise practice  Precedence  Control flow.
Week 3 - Monday.  What did we talk about last time?  Math library  Preprocessor directives  Lab 2.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 3- Flow Control. Overview n Why flow control n Branches vs. Loops n Branch statements n Loop Statements n Complex loops n Boolean variables n.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 ENERGY 211 / CME 211 Lecture 6 October 3, 2008.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
A First Book of C++ Chapter 5 Repetition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Week 3 - Friday CS222.
Selections Java.
The switch Statement, and Introduction to Looping
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Arrays, For loop While loop Do while loop
Switch Statements, Do While, Break, Continue, Goto, Comma Operator
Outline Altering flow of control Boolean expressions
Chapter 7 Additional Control Structures
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chap 7. Advanced Control Statements in Java
Flow of Control.
CSC215 Lecture Control Flow.
Controlling Program Flow
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Week 3 - Wednesday

 What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations

Sometimes the slightest shift in the way you look at things, a seemingly insignificant change in perspective can alter your life forever. Anonymous Sometimes the slightest shift in the way you look at things, a seemingly insignificant change in perspective can alter your life forever. Anonymous

 Things smaller than int will be promoted to int  What are the following?  4 & 113  15 | 39  31 << 4  108 >> 5  ~80

 The computer uses bitwise operations for many things  These operations are available for our use and are very fast  Shifting is faster than multiplying or dividing by powers of 2  You can keep a bitmask to keep track of 32 different conditions  That's quite a lot of functionality for 4 bytes!

 Operators in every programming language have precedence  Some of them are evaluated before others  Just like order of operations in math  * and / have higher precedence than + and –  = has the very lowest precedence  I don't expect you to memorize them all, but  Know where to look them up  Don't write confusing code

Type OperatorsAssociativity Primary Expression () []. -> expr ++ expr -- Left to right Unary * & + - ! ~ ++ expr -- expr ( typecast ) sizeof Right to left Binary * / % Left to right + - >> << = == != & ^ | && || Ternary ?: Right to left Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left Comma, Left to right

 What happens here?  x++ >> 5 == 4 % 12 & 3  It's also worth noting that precedence doesn't tell the whole story  What about multiple assignments in a single line of code?  C doesn't give you guarantees about what happens when  The following could have different results on different compilers: printf("%d %d", x++, (x + 5)); a[x] = x++; x = x++; printf("%d %d", x++, (x + 5)); a[x] = x++; x = x++;

 Sequences of statements surrounded by braces are treated like a single statement with no value  Braces can be thrown in whenever you want  We used to say that "braces were optional" for one- line blocks, but this is the more accurate way to look at it  An expression can always become a statement int a = 150; a; //legal in C, illegal in Java int a = 150; a; //legal in C, illegal in Java

 Like Java, the body of an if statement will only execute if the condition is true  The condition is evaluated to an int  True means not zero  An else is used to mark code executed if the condition is false Sometimes this is natural and clear; at other times it can be cryptic.

The if part Any expression that has a value Any single statement ending in a semicolon or a block in braces if( condition ) statement

Two different outcomes if( condition ) statement1 else statement2

 We can nest if statements inside of other if statements, arbitrarily deep  Just like Java, there is no such thing as an else if statement  But, we can pretend there is because the entire if statement and the statement beneath it (and optionally a trailing else ) is treated like a single statement

 switch statements allow us to choose between many listed possibilities  Execution will jump to the matching label or to default (if present) if none match  Labels must be constant (either literal values or #define constants)  Execution will continue to fall through the labels until it reaches the end of the switch or hits a break  Don't leave out break statements unless you really mean to!

switch( data ) { case constant1: statements1 case constant2: statements2 … case constantn: statementsn default: default statements }

 C has relatively relaxed syntax rules  What the hell is that?! int n = (count + 7) / 8; switch (count % 8) { case 0: do {*to++ = *from++; case 7: *to++ = *from++; case 6: *to++ = *from++; case 5: *to++ = *from++; case 4: *to++ = *from++; case 3:*to++ = *from++; case 2: *to++ = *from++; case 1: *to++ = *from++; } while (--n > 0); } int n = (count + 7) / 8; switch (count % 8) { case 0: do {*to++ = *from++; case 7: *to++ = *from++; case 6: *to++ = *from++; case 5: *to++ = *from++; case 4: *to++ = *from++; case 3:*to++ = *from++; case 2: *to++ = *from++; case 1: *to++ = *from++; } while (--n > 0); }

 C has three loops, just like Java  while loop ▪ You don't know how many times you want to run  for loop ▪ You know how many times you want to run  do-while loop ▪ You want to run at least once  Like if statements, the condition for them will be evaluated to an int, which is true as long as it is non-zero  All loops execute as long as the condition is true

 A while loop is the keyword while followed by a pair of parentheses  Within the parentheses is a condition  If the condition is true, the body of the loop will be executed  At the end of the loop, the condition is checked again

while( condition ) statement

 A for loop consists of three parts:  Initialization  Condition  Increment  The initialization is run when the loop is reached  If the condition is true, the body of the loop will be executed  At the end of the loop, the increment will be executed and the condition checked again  If the condition is empty (nothing in it), it is considered true

Way to Progress Ending Point Starting Point for ( initialization ; condition ; increment ) statement

 C has a comma operator  Expressions can be written and separated by commas  Each will be evaluated, and the last one will give the value for the entire expression int a = 10; int b = 5; int c = a, b, ++a, a + b; //16 int a = 10; int b = 5; int c = a, b, ++a, a + b; //16

 Sometimes you want to do multiple things on each iteration  Consider this code to reverse an array  You can even use a comma in the condition part, but it doesn't usually make sense int start; int end; int temp; for(start = 0, end = length - 1; start < end; start++, end-- ) { temp = array[start]; array[start] = array[end]; array[end] = temp; } int start; int end; int temp; for(start = 0, end = length - 1; start < end; start++, end-- ) { temp = array[start]; array[start] = array[end]; array[end] = temp; }

 As in Java, there are do-while loops which are useful only occasionally  They work just like while loops except that that they are guaranteed to execute at least once  Unlike a while loop, the condition isn’t checked the first time you go into the loop  Sometimes this is useful for getting input from the user  Don't forget the semicolon at the end!

do statement while( condition );

 Loops can go on forever if you aren’t careful int n = 40; int i = 1; while( i <= 40 ) { printf("%d", i); //supposed to print all the numbers //less than 40, but i never increases } int n = 40; int i = 1; while( i <= 40 ) { printf("%d", i); //supposed to print all the numbers //less than 40, but i never increases }

 Infinite for loops are unusual, but possible:  This situation is more likely: for( ; ; ) printf("Hey!"); for( ; ; ) printf("Hey!"); int i; for(i = 0; i < 10; i++ ) { printf("%d", i); //lots of other code i--; //whoops, maybe changed from while? } int i; for(i = 0; i < 10; i++ ) { printf("%d", i); //lots of other code i--; //whoops, maybe changed from while? }

 Overflow and underflow will make some badly written loops eventually terminate int i; for( i = 1; i <= 40; i-- ) //whoops, should have been i++ printf("%d", i); int i; for( i = 1; i <= 40; i-- ) //whoops, should have been i++ printf("%d", i);

 Being off by one is a very common loop error int i; for( i = 1; i < 40; i++ ) //runs 39 times printf("%d", i); int i; for( i = 1; i < 40; i++ ) //runs 39 times printf("%d", i);

 If the condition isn’t true to begin with, the loop will just be skipped int i; for( i = 1; i >= 40; i++ ) //oops, should be <= printf("%d", i); int i; for( i = 1; i >= 40; i++ ) //oops, should be <= printf("%d", i);

 A misplaced semicolon can cause an empty loop body to be executed  Everything looks good, loop even terminates  But, only one number will be printed: 41  Misplaced semicolon usually makes a while loop infinite int i; for( i = 1; i <= 40; i++ ); //semicolon is wrong { printf("%d", i); } int i; for( i = 1; i <= 40; i++ ); //semicolon is wrong { printf("%d", i); }

 Fundamental Linux and systems programming concepts  Lab 3

 Read LPI chapters 2 and 3  Project 1 due on Friday by midnight!