introduction to computers and programming class 6 introduction to c professor avi rosenfeld
Post on 21-Dec-2015
224 views
TRANSCRIPT
![Page 1: Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/1.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/2.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/5.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/6.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/7.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/8.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/16.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/21.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/23.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062313/56649d605503460f94a41a10/html5/thumbnails/24.jpg)
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");}