Book Report Essay

1621 Words Dec 18th, 2015 7 Pages
Selection statements
Selection is used to select which statements are to be performed next based on a condition being true or false.
Relational expressions
In the solution of many problems, different actions must be taken depending on the value of the data. The if statement in C I used to implement such s decision structure in its simplest form – that of selecting a statement to be executed only if a condition is satisfied.
Syntax:
if(condtion) statement executed if condition is true
When an executing program encounters the if statement, the condition is evaluated to determine its numerical value, which is then interpreted as either true or false. If the condition evaluates to any non-0 value (positive or negative), the condition is
…show more content…
#include <stdio.h> main() { int idNum; float miles; printf(“ Please type in car number and mileage :” ); scanf(“ %d %f”, &idNum, &miles); if( miles > 3000) printf(“ Car %d is over the limit.\n”, idNum); printf(“. . .end of program output. . .\n”); getch(); }
#include <stdio.h> main() { int idNum; float miles; printf(“ Please type in car number and mileage :” ); scanf(“ %d %f”, &idNum, &miles); if( miles > 3000) printf(“ Car %d is over the limit.\n”, idNum); printf(“. . .end of program output. . .\n”); getch(); }

Compound statements
Although only a single statement is permitted in an if statement, this statement can be a single compound statement. A compound statement is one or more statements contained between braces as shown below:
{
statement1; statement2; statement3;
}
The use of braces to enclose a set of individual statements creates a single block of statements, which may be used anywhere in a C program in place of a single statement. The general syntax of a compound within a one way if statement is:

if (expression)
{
statement1; statement2; .
.
.
}

The if-else

Related Documents