The If Statement - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

The If Statement

Description:

Used in combination with 'else' to create many possible ... the program will run the code between the curly braces AFTER the 'else'! main () { float temp; ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 17
Provided by: CU14
Category:
Tags: run | statement | up

less

Transcript and Presenter's Notes

Title: The If Statement


1
The If Statement
  • ATS 315

2
if
  • Used to make decisions in a program.
  • Used in combination with else to create many
    possible actions in a program.

3
if
  • This example scans in a temperature from the user.

main () float temp scanf (f,temp) if (
temp lt 0.) printf (It is cold!\n)
4
if
  • If the temperature is less than 0.0

main () float temp scanf (f,temp) if (
temp lt 0.) printf (It is cold!\n)
5
if
  • then the program will run the stuff inside the
    curly braces!

main () float temp scanf (f,temp) if (
temp lt 0.) printf (It is cold!\n)
6
if
  • In this case, that means it prints It is cold!

main () float temp scanf (f,temp) if (
temp lt 0.) printf (It is cold!\n)
7
if
  • More complicated actions can happen with the
    else statement.

main () float temp scanf (f,temp) if (
temp lt 0.) printf (It is cold!\n) else
printf (Not too bad\n)
8
if
  • Now if the condition is FALSE

main () float temp scanf (f,temp) if (
temp lt 0.) printf (It is cold!\n) else
printf (Not too bad\n)
9
if
  • the program will run the code between the curly
    braces AFTER the else!

main () float temp scanf (f,temp) if (
temp lt 0.) printf (It is cold!\n) else
printf (Not too bad\n)
10
if
  • In this case, it will print Not too bad.

main () float temp scanf (f,temp) if (
temp lt 0.) printf (It is cold!\n) else
printf (Not too bad\n)
11
if is tricky, too
  • Dont use use

main () int i,j scanf (d,i) scanf
(d,j) if ( ij) printf (They are the
same!\n)
12
if is tricky, too
  • is for assigning a value to the variable.
  • is for testing a value in the variable.

main () int i,j scanf (d,i) scanf
(d,j) if ( ij) printf (They are the
same!\n)
13
if is tricky, too
  • In general, floats cannot be equal, so dont test
    for that.
  • Rarely a problem.

main () if (potTemp 298.7) printf
(Will not happen!\n)
14
if is tricky, too
  • Cannot use for strings.

main () if (mystation yourstation)
printf (Same station!\n)
15
if is tricky, too
  • Use !strcmp(string1,string2) for strings
  • Means string compare.

main () if (!strcmp(mystation,yourstation))
printf (Same station!\n)
16
In Assignment 8
while(!feof(fin)) fscanf(fin,s d d d d
d,station,tempcode,) / DECODE THE
OBSERVATION / tempF ((float) tempcode
)/10.0 blah blah blah / IS THIS THE STATION
REQUESTED BY THE USER? / if (!strcmp(mystation,s
tation) /PRINT OUT THE OBS FOR THE CITY
/
Write a Comment
User Comments (0)
About PowerShow.com