Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
PSEUDOCODE & FLOW CHART
Lecture 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Chapter 3 - Structured Program Development
Introduction to Computers and Programming Lecture 5 New York University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
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.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
© 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.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
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.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
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.
The if…else Selection Statement
Algorithm: procedure in terms of
- Standard C Statements
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Numbering System TODAY AND TOMORROW 11th Edition
Lecturer CS & IT Department UOS MBDIN
Chapter 4 – Control Structures Part 1
Programming Fundamentals
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
3 Control Statements:.
Conditions and Boolean Expressions
Decision making If statement.
Chapter 3 - Structured Program Development
Chapter 2 - Introduction to C Programming
EPSII 59:006 Spring 2004.
Chapter 2 - Introduction to C Programming
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld

Control Structures Control the flow of a program Normally, in C, statements are executed in sequential order Control structures allow the programmer to specify that a statement other than the next be executed –i.e. programmer can control what’s executed in which order

Three Basic Control Structures All programs can be written with just these types of structures –sequence structure i.e. one after the other –selection structure e.g. if, if else, switch, etc. –repetition structure i.e. repeat some actions

The if structure If some condition is true do this Example: if ( x == y ) { printf( “ x is equal to y!\n “ ) ; } Every programming language has some form of an if statement.

Another if example #include void main() { int grade; printf("Please enter a grade\n"); scanf("%d",&grade); if (grade >=65) printf("You passed!\n"); }

if with a twist: if else #include void main() { int grade; printf("Please enter a grade\n"); scanf("%d",&grade); if (grade >=65) printf("You passed!\n"); else printf("You failed!\n"); }

What is a “Flow Chart?” a visual tool that helps you understand the flow of your program You can think of flow charts as pipes of water: –You pour water in the top. –Water pours through the pipes and is pulled downward

Flow Chart Basics 1 Rectangles represent statements of work. For example: printf() Diamonds (decision symbol) contain conditions flow line

Flow Chart Basics 2 print “passed” grade >=65 true Connector symbol false

if/else Flow Chart grade >=65 Print “You passed” Print “You failed” True False

Blocks are started with a left brace { } and ended with a right brace you can declare variables anywhere after a left brace (as long as it’s before executable statements)

Blocks, continued To run several lines of code together, you must include them within a block For example: if ( grade >= 65 ) { printf ( "You passed!!!\n “ ); printf ( "Congratulations!\n “ ); }

Indentation Everything within the block of code should be indented –helps you see the block at a quick glance. Avoid writing code like this: if (grade >= 60) { printf ("You passed!!!\n"); printf ("Congratulations!\n"); } This is valid C code, but it is not easy to view the block: bad style

Example /* Introduction to If/Else Statements */ #include int main () { int grade; /* variable to hold user input */ printf ( "Enter your course grade: “ ); scanf ( "%d", &grade ); if ( grade >= 65 ) { printf ( "You passed!!! \n “ ); printf ( "Congratulations!\n “ ); } else { printf ("You failed!\n"); } } /* end main program */ Note the indentation

Relational Operators (revisited) Not Equal to!= Equal to== Less than or equal to<= Greater than or equal to>= Less than< Greater than> MeaningOperator

Testing for Equality To test for equality, use the == operator. if ( grade == 100 ) { printf ( “ Perfect Score! ” ) ; } To test for inequality, use the != operator if (grade != 100) { printf ( “ Not perfect! ” ); }

Equality v. Assignment (revisited) Remember Gets not Equals! if ( grade = 100 ) printf( “ Perfect Score! ” ); In this case, we are using a single = character. (We really want to use == ) What will it actually print? –Depends on whether grade is TRUE or FALSE

Understanding Truth (in the Boolean Sense) In C (unlike life) truth is very simple. –0 is False –Anything else is True For example: –-1 is true –299 is true –0 is false

The Conditional Operator #include void main() { int grade; printf("Please enter a grade\n"); scanf("%d",&grade); printf("%s\n", (grade>=65) ? "You passed" : "You failed"); } %s is the format specifier for text strings. The conditional Statement. True Option False Option ? short- cut operator

Truth Example /* Understanding Truth */ #include int main() { int x = -100; int y = 299; int z = 0; if ( x ) printf ("x: %d\n", x); if ( y ) printf ("y: %d\n", y); if ( z ) printf ("z: %d\n", z); } This is a completely valid program. Output: x: -100 y: 299 Note: z is not printed because the if condition fails.

Pseudocode The implementation of an algorithm Not code, not English Ex: –Read a number –If the number is greater than 90, print ‘A’ –Else, if the number is greater than 80, print ‘B’ –Else, if the number is greater than 70, print ‘C’

Code Implementation (Style #1) #include void main() { int grade; scanf("%d",&grade); if (grade>90) printf("You got an A\n"); else if (grade > 80) printf("You got a B\n"); else if (grade > 75) printf("You got a C\n"); else printf("Sorry, you'll have to repeat the course\n"); }

Code Implementation #2 #include void main() { int grade; scanf("%d",&grade); if (grade>90) printf("You got an A\n"); else if (grade > 80) printf("You got a B\n"); else if (grade > 75) printf("You got a C\n"); else printf("Sorry, you'll have to repeat the course\n"); }

Incorrect Code Implementation #include void main() { int grade; scanf("%d",&grade); if (grade>90) printf("You got an A\n"); if (grade > 80) printf("You got a B\n"); if (grade > 75) printf("You got a C\n"); else printf("Sorry, you'll have to repeat the course\n"); }