Top Banner
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld
24

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.

Introduction to Computers and Programming

Class 6

Introduction to C

Professor Avi Rosenfeld

Page 2: 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

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

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

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

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.

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

Another if example

#include <stdio.h>void main(){int grade;printf("Please enter a grade\n");scanf("%d",&grade);if (grade >=65)

printf("You passed!\n");}

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

if with a twist: if else

#include <stdio.h>

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");

}

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

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

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

Flow Chart Basics 1

Rectangles represent statements of work. For example:

printf()

Diamonds(decision symbol)

contain conditions flow

line

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

Flow Chart Basics 2

print “passed”grade >=65true

Connector symbol

false

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

if/else Flow Chart

grade >=65

Print “You passed”

Print “You failed”

TrueFalse

                

 

 

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

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)

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

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“ );

}

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

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

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

Example

/* Introduction to If/Else Statements */#include <stdio.h>

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 theindentation

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

Relational Operators (revisited)

Not Equal to!=

Equal to==

Less than or equal to<=

Greater than or equal to

>=

Less than<

Greater than>

MeaningOperator

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

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!”);}

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

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

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

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

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

The Conditional Operator#include <stdio.h>

void main()

{

int grade;

printf("Please enter a grade\n");

scanf("%d",&grade);

printf("%s\n", (grade>=65) ? "You passed" : "You failed");

}

%s is theformatspecifier fortext strings.

The conditionalStatement.

TrueOption

FalseOption

? short-cut operator

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

Truth Example

/* Understanding Truth */#include <stdio.h>

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 completelyvalid program.

Output:x: -100y: 299

Note: z is notprinted becausethe if conditionfails.

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

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’

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

Code Implementation (Style #1)

#include <stdio.h>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");}

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

Code Implementation #2

#include <stdio.h>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");}

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

Incorrect Code Implementation

#include <stdio.h>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");}