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

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 6: Loops Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Loops.
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 Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
1 Chapter 6 Loops. Iteration Statements C’s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
CSE 220 – C Programming Loops.
C Functions -Continue…-.
C Program Controls + Flow Structure
Functions and Structured Programming
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
JavaScript: Control Statements.
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company. 1
Programmazione I a.a. 2017/2018.
Flow of Control.
Scope, Parameter Passing, Storage Specifiers
Looping.
Flow of Control.
- Additional C Statements
Outline Altering flow of control Boolean expressions
CS1100 Computational Engineering
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Flow of Control.
Homework Any Questions?.
C Programming Getting started Variables Basic C operators Conditionals
Dale Roberts, Lecturer IUPUI
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Chapter 4 - Program Control
Programming Languages and Paradigms
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Chap 7. Advanced Control Statements in Java
Flow of Control.
CSC215 Lecture Control Flow.
Presentation transcript:

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

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function is called, and it disappears when the function is exited Temporary storage for the lifetime of the function or block Its memory is allocated in the function’s stack frame Such a variable is called an automatic variable Default and implicit int foo(int n){ int i; … }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Scope Rules (contd.) Function parameters (e.g., variable n in foo) are also automatic variables If we put static in the declaration of a local variable, it remains in existence throughout the program Variable i in the example below is called a static local variable int foo(int n) { static int i; … }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Scope Rules (contd.) A variable declared outside any function is an external variable Permanent storage for the lifetime of the program Implicit The scope of a variable or a function is the part of the program within which the variable or the function can be used The scope of a local variable is the function in which the variable is declared The scope of an external variable or a function is from the point of its declaration to the end of the file An external variable can be accessed by all functions that follow its declaration

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Scope Rules (contd.) Because of the scope rule, a function should be defined before it is used (called) However, C allows a function to be declared before it is defined Forward declaration #include int foo(int n); int main(void) { int x; … foo(x); … } int foo(int n) { … } #include int foo(int n) { … } int main(void) { int x; … foo(x); … }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Scope Rules (contd.) When we have a newly declared variable in the scope of another variable x with the same name, new x hides old x in the scope of new x #include int i; /* external variable */ int foo(int n); /* forward declaration */ int main(void) { int x; … x = i; /* i refers to the external variable i */ … } int foo(int n) /* function definition */ { int i; … i = n; … }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Static Scope and Block Structure A block is a grouping of declarations and statements Enclosed with { and } A declaration D belongs to a block B if B is the most closely nested block containing D D is located within B, but not within any block that is nested within B Static-scope rule If declaration D of name x belongs to block B, then the scope of D is all of B, except for any blocks B’ nested to any depth within B, in which x is re-declared A name x is re-declared in B’ if some other declaration D’ of the same name x belongs to B’

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Blocks in C main() { int a = 1; int b = 1; { int b = 2; { int a = 3; printf(“%d%d\n”, a, b); } { int b = 4; printf(“%d%d\n”, a, b); } printf(“%d%d\n”, a, b); } printf(“%d%d\n”, a, b); }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Sequential Flow of Control Unless otherwise stated, statements in a program are executed one after another in the order they placed in the program

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee The if statement has the following form: if ( expression ) statement1 else statement2 The else part is optional If expression is evaluated to true, then statement1 is executed If it is false, statement2 is executed If it is false and there is no else part, the if statement does nothing If Statement

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee If Statement (contd.) The following if statement sets abs to be the absolute value of x if (x > 0) abs = x; else abs = -x; The following example sets min to be the minimum of i and j min = i; if (j < min) min = j;

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee If Statement (contd.) statement1 or statement2 should be a single statement Compound statement A series of declarations and statements surrounded by braces { declarations statements } A compound statement itself is a (single) statement if (x < 0) { temp = x; x = y; y = temp; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Cascaded if A cascaded if statement can be used to write a multi-way decision What does the program below do? #include int main(void) { char op; int a = 20, b = 4; printf("Select operator (+, -, *, /) : "); scanf("%c", &op); if (op == '+') printf("20 %c 4 = %d\n", op, a+b); else if (op == '-') printf("20 %c 4 = %d\n", op, a-b); else if (op == '*') printf("20 %c 4 = %d\n", op, a*b); else if (op == '/') printf("20 %c 4 = %d\n", op, a/b); else printf("Wrong operator!\n"); return 0; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Switch Statement The switch statement also describes a multi-way decision that tests whether an expression matches one of several values The default case is optional If the default case does not exist and none of the cases match, then no action will take place switch(op) { case '+': printf("20 %c 4 = %d\n", op, a+b); break; case '-': printf("20 %c 4 = %d\n", op, a-b); break; case '*': printf("20 %c 4 = %d\n", op, a*b); break; case '/': printf("20 %c 4 = %d\n", op, a/b); break; default: printf("Wrong operator!\n"); break; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Switch Statement (contd.) The break statement causes an immediate exit from the switch statement If break is absent in a case, execution falls through to the next case switch(op) { case '+': printf("20 %c 4 = %d\n", op, a+b); break; case '-': printf("20 %c 4 = %d\n", op, a-b); break; case '*': printf("20 %c 4 = %d\n", op, a*b); break; case '/': printf("20 %c 4 = %d\n", op, a/b); break; default: printf("Wrong operator!\n"); break; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee For Statement The for statement is a convenient way to write a loop that has a counting variable for (expr1; expr2; expr3) statement expr1 is an initialization that is executed only once at the beginning of the loop expr2 is a termination condition expr3 is executed at the end of each loop iteration

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee For Statement (contd.) A for loop that computes the sum of integers from 1 to 100 #include int main(void) { int i, sum = 0; for (i=1; i<=100; i++) sum = sum + i; printf(" = %d\n", sum); return 0; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee While Statement The while statement has the following form while (expr) statement expr is evaluated If it is true (non-zero), statement is executed and expr is evaluated again This process continues until expr becomes false (zero)

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee While Statement (contd.) Finding the minimum integer i such that 1+2+…+ i > 1000 In general, for loops are better if we know the number of iterations in advance, and while loops are better otherwise #include int main(void) { int i = 0, sum = 0; while (sum <= 1000) { i = i + 1; sum = sum + i; } printf("minimum i which satisfies (1+2+…+i) > 1000 is %d\n", i); return 0; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Do While Statement The do while statement is similar to the while statement, but expression is tested after the loop body (i.e., statement) do statement while (expression); the loop body is executed at least once

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Break Statement Causes an exit from the innermost enclosing loop or switch statement while(1) { scanf(“%1f”, &x); if (x < 0.0) break; printf(“%f\n”, x*x); }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Continue Statement Causes the current iteration of a loop to stop and the next iteration of the loop begin Printing all integers less than 100 that are not divisible by 3 #include int main(void) { int i; for (i=0; i<100; i++) { if ( i % 3 == 0) continue; printf("%d ", i); } printf("\n"); return 0; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Goto Statement goto label ; Control is unconditionally transferred to a labeled statement In general, goto should be avoided Using many gotos makes the program hard to read … goto error; … error: { printf(“error\n” ); exit(1); }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee exit(int status) Defined in stdlib.h (Standard library) Never returns Terminates the program 0 (EXIT_SUCCESS) stands for successful program completion 1 (EXIT_FAILURE) stands for unsuccessful program completion #include void main() { int a = 0; if (a == 1) exit(1); else exit(0); a = 1; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee getchar() and putchar() getchar() Gets a value from the keybord and returns the value putchar(c) The value of c is written to the standard output Both defined in stdio.h (standard I/O library) #include void main() { int c; while ( ( c = getchar() ) != EOF ) { putchar( c ); }