- Standard C Statements

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 3 - Structured Program Development
Lec3: Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Structured Program Development in C
Lecture 3 Structured Program Development in C
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 3 Structured Program Development. Objectives To understand basic problem-solving techniques. To be able to develop algorithms through the process.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2000 Prentice Hall, Inc. All rights reserved. Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Decision making If.. else statement.
Chapter 4 – C Program Control
The if…else Selection Statement
Algorithm: procedure in terms of
Chapter 4 C Program Control Part I
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Week 4 – Repetition Structures / Loops
Programming Fundamentals
The while Looping Structure
Lecture 2: Logical Problems with Choices
Structured Program
Chapter 4 - Program Control
Chapter 3 - Structured Program Development
-2- Introduction to C Programming
3 Control Statements:.
The while Looping Structure
Chapter 3 - Structured Program Development
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
-2- Introduction to C Programming
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Lec 6 Loop Statements Introduction to Computer Programming
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
Structural Program Development: If, If-Else
Presentation transcript:

- Standard C Statements Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Program Control - Standard C Statements

Outline This Topic Introduces selection structure if if/else repetition control structures while

Selection Structure: if Used to choose among alternative courses of action Pseudocode: If (student’s grade is greater than or equal to 60) Print “Passed” If condition true Print statement executed and program goes on to next statement If false, print statement is ignored and the program goes onto the next statement Indenting makes programs easier to read C ignores whitespace characters Pseudocode statement in C: if ( grade >= 60 ) printf( "Passed\n" ); C code corresponds closely to the pseudocode

The if Selection Structure (cont.) A decision can be made on any expression. zero - false nonzero - true Example: (3 – 4) is true true false grade >= 60 print “Passed”

Selection Structure: if/else if: only performs an action if the condition is true if/else: Specifies an action to be performed both when the condition is true and when it is false Pseudocode: If (student’s grade is greater than or equal to 60) Print “Passed” else Print “Failed” Note spacing/indentation conventions C code: if ( grade >= 60 ) printf( "Passed\n"); else printf( "Failed\n");  

The if/else Selection Structure (cont.) Ternary conditional operator (?:) Takes three arguments (condition, value if true, value if false) Creates an if/else expression. Recall that expressions are computations that yield a single value. Our pseudocode could be written: printf( "%s\n", grade >= 60 ? "Passed" : "Failed" ); Or it could have been written: grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” );

The if/else Selection Structure Compound statement: Set of statements within a pair of braces Example: if ( grade >= 60 ) printf( "Passed.\n" ); else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); } Without the braces, else printf( "You must take this course again.\n" ); the statement would be executed under every condition.

The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled repetition Definite repetition: know how many times loop will execute Control variable used to count repetitions Sentinel-controlled repetition Indefinite repetition Used when number of repetitions not known Sentinel value indicates "end of data“

Essentials of Counter-Controlled Repetition Counter-controlled repetition requires The name of a control variable (or loop counter) The initial value of the control variable A condition that tests for the final value of the control variable (i.e., whether looping should continue) An increment (or decrement) by which the control variable is modified each time through the loop Example: int counter = 1; /* initialization */ while ( counter <= 10 ) { /* repetition condition */ printf( "%d\n", counter ); ++counter; /* increment */ } The statement int counter = 1; Names counter Declares it to be an integer Reserves space for it in memory Sets it to an initial value of 1 This is not an executable statement, it is a declaration.

Repetition Structure: while 1 /* Fig. 3.6: fig03_06.c 2 Class average program with 3 counter-controlled repetition */ 4 #include <stdio.h> 5 6 int main() 7 { 8 int counter, grade, total, average; 9 10 /* initialization phase */ 11 total = 0; 12 counter = 1; 13 14 /* processing phase */ 15 while ( counter <= 10 ) { 16 printf( "Enter grade: " ); 17 scanf( "%d", &grade ); 18 total = total + grade; 19 counter = counter + 1; 20 } 21 22 /* termination phase */ 24 printf( "Class average is %d\n", average ); 25 26 return 0; /* indicate program ended successfully */ 27 } printf( "Enter grade, -1 to end: " ); scanf( "%d", &grade ); while ( grade != -1 ) { total = total + grade; counter = counter + 1; } /* termination phase */ if ( counter != 0 ) { average = ( float ) total / counter; printf( "Class average is %.2f", average ); } else printf( "No grades were entered\n" ); Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81 Program Output: