DOC PREVIEW
OSU ENGR H192 - Lecture 08 - Control Statements

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Control StatementsIFif / else if / else Selection StructuresSlide 4if / else Selection StructuresSlide 6Slide 7Slide 8if / else if / else Selection StructuresSlide 10Slide 11Simple Program Using if / else if / elseGrading Program Using if / else if / elseGrading Program Using if / else if / elseEngineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 1Winter QuarterControl StatementsLecture 8Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 2Winter QuarterIFIf you can keep your head when all about youAre losing theirs and blaming it on you,If you can trust yourself when all men doubt youBut make allowance for their doubting too,If you can wait and not be tired by waiting,Or being lied about, don't deal in lies,Or being hated, don't give way to hating,And yet don't look too good, nor talk too wise:THEN Yours is the Earth and everything that's in it,And--which is more--you'll be a Man, my son! --Rudyard KiplingEngineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 3Winter Quarterif / else if / else Selection Structures•Selection structures permit alternate actions based on the evaluation of logical expressions.•The logical expressions evaluate as either true or false, and the action takes place if and only if the expression is true.•When the expression is false, the program may take alternate action(s), or it may evaluate another expression to determine what to do next.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 4Winter Quarterif / else if / else Selection Structures•A simple if structure is called a single-selection structure because it either selects or ignores a single action.•The if / else structure is called a double-selection structure because it selects between two different actions.•Nested if / else structures test for multiple cases by placing if / else structures inside other if / else structures.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 5Winter Quarterif / else Selection Structures•Syntax for the if selection structure is as follows:if ( this logical expression is true ) statement ;•Syntax for the if / else selection structure is as follows:if ( this logical expression is true ) statement ; else statement ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 6Winter Quarterif / else Selection Structures•A very simple program:#include <stdio.h>int main ( ){ int a = 1, b = 2, c ; if (a > b) c = a; else c = b;}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 7Winter Quarterif / else Selection Structures•The if selection structure is often written as:if ( this logical expression is true ) statement ;•And, the if / else selection structure is often written as:if ( this logical expression is true )statement ; else statement ;no semi-colon!Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 8Winter Quarterif / else Selection Structures•Often, the earlier example is written this way:#include <stdio.h>int main ( ){ int a = 1, b = 2, c ; if (a > b) c = a ; elsec = b ;}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 9Winter Quarterif / else if / else Selection Structures•Syntax for the if / else if / else selection structure is as follows:if ( this logical expression is true ) statement ;else if ( this logical expression is true )statement ; else statement ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 10Winter Quarterif / else if / else Selection Structures•The actual syntax for the multiple if / else if / else selection structure is as follows:if ( this logical expression is true ) statement ;else if ( this logical expression is true )statement ; else if ( this logical expression is true )statement ;else statement ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 11Winter Quarterif / else if / else Selection Structuresif ( this logical expression is true ){ Execute statements in this block ;}else if ( this logical expression is true ){ Execute statements in this block ; }else{ Execute statements in this block ;}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 12Winter QuarterSimple Program Using if / else if / else #include <stdio.h>int main ( ){ int a , b ; printf ("Enter values for a and b > ") ; scanf ("%d%d", &a, &b ) ; if ( a < b ) printf ("a is less than\n") ; else if ( a == b ) printf (" a is equal to b\n") ; else printf ("a is larger than b\n") ;}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 13Winter QuarterGrading Program Using if / else if / else /* This program associates letter grades with numeric test scores */#include <stdio.h> int main ( ){int score ;printf ("enter your test score >") ;scanf ("%d", &score) ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 8 P. 14Winter QuarterGrading Program Using if / else if / elseif (score >= 90)printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score < 90)printf ("Your score of %d is a B\n", score) ; else if (score >= 70)printf ("Your score of %d is a C\n", score) ; else if (score >= 60)printf ("Your score of %d is a D\n", score) ; elseprintf ("Your score of %d is an E\n", score)


View Full Document

OSU ENGR H192 - Lecture 08 - Control Statements

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 08 - Control Statements
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 08 - Control Statements and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 08 - Control Statements 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?