Sie sind auf Seite 1von 18

Engineering H192 - Computer Programming

Control Statements

Lecture 8

Winter Quarter The Ohio State University Lect 8 P. 1


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

IF
If you can keep your head when all about you
Are losing theirs and blaming it on you,
If you can trust yourself when all men doubt you
But 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 Kipling

Winter Quarter The Ohio State University Lect 8 P. 2


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / 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.
Winter Quarter The Ohio State University Lect 8 P. 3
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / 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.
Winter Quarter The Ohio State University Lect 8 P. 4
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / 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 ;

Winter Quarter The Ohio State University Lect 8 P. 5


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / 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;

}
Winter Quarter The Ohio State University Lect 8 P. 6
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / else Selection Structures


• The if selection structure is often written as:

if ( this logical expression is true ) no semi-colon!


statement ;

• And, the if / else selection structure is often written as:

if ( this logical expression is true )


statement ;
else
statement ;

Winter Quarter The Ohio State University Lect 8 P. 7


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / 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;
else
c=b;
}

Winter Quarter The Ohio State University Lect 8 P. 8


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / 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 ;

Winter Quarter The Ohio State University Lect 8 P. 9


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / 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 ;
Winter Quarter The Ohio State University Lect 8 P. 10
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

if / else if / else Selection Structures


if ( 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 ;
}

Winter Quarter The Ohio State University Lect 8 P. 11


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

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

Winter Quarter The Ohio State University Lect 8 P. 12


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

Grading 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) ;
Winter Quarter The Ohio State University Lect 8 P. 13
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

Grading Program Using if / else if / else


if (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) ;
  else
printf ("Your score of %d is an E\n", score) ;
}

Winter Quarter The Ohio State University Lect 8 P. 14


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

A Little Help for Assignment G07

• Assignment G07 requires us to make up (generate)


a random number between 0 and 10.

• Often, computer system have routines already


available for generating a sequence of pseudo-
random numbers.

• For today, we can use one such utility function:


int answer ;
answer = rand ( ) % 11 ;
Winter Quarter The Ohio State University Lect 8 P. 15
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

A Little Help for Assignment G07


• But if we just use:
answer = rand ( ) % 11 ;
we'll get the same "random" value each time we run
the program.

• So, how do we get something more "random".

• We must first "seed" the random number generator:


srand ( some_random_number ) ;
• So where do we get "some_random_number" ?

Winter Quarter The Ohio State University Lect 8 P. 16


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

A Little Help for Assignment G07

• On a UNIX system, the time of day (expressed in


seconds) is "sort of" a random number:
srand ( time (NULL) ) ;
• Where do these functions "live"?
– rand, srand functions are in <stdlib.h>
– time function is in <time.h>
• Check C Library resources

Winter Quarter The Ohio State University Lect 8 P. 17


Gateway Engineering Education Coalition
Engineering H192 - Computer Programming

A Little Help for Assignment G07


• How to control the game? (Use bottom test loop)

int nguess, guess, answer ;


/* seed random number generator here */
nguess = 0 ;
answer = rand ( ) % 11 ;
do
{
nguess++ ;
/* game playing logic goes here */
} while ( guess != answer && nguess < 4 ) ;
Winter Quarter The Ohio State University Lect 8 P. 18
Gateway Engineering Education Coalition

Das könnte Ihnen auch gefallen