View
224
Download
3
Embed Size (px)
Citation preview
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 <stdio.h>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 <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");
}
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 >=65true
Connector symbol
false
if/else Flow Chart
grade >=65
Print “You passed”
Print “You failed”
TrueFalse
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 <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
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 <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
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.
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 <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");}
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");}
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");}