Title: Structure of code:
1Structure of code
/ A simple C program / include
ltstdio.hgt include ltconio.hgt void main(
) printf(\n This is a C program) printf(\n
Press any key to exit) getch( )
2Variables
void main( ) int x float y double
z printf(\n Enter value for x) scanf(d,
x) printf(\n Enter value for
y) scanf(f, y) printf(\n Enter value
for z) scanf(lf, z) printf(x d, y
f, z lf, x, y, z) getch( )
3printf and scanf
void main( ) int x float y double
z printf(\n Enter value for x) scanf(d,
x) printf(\n Enter value for
y) scanf(f, y) printf(\n Enter value
for z) scanf(lf, z) printf(x d, y
f, z lf, x, y, z) getch( )
4Defining Variables
void main( ) float pounds, ounces printf(\n
Enter value for pounds) scanf(f,
pounds poundsounces16 ouncespounds16 pr
intf(f pounds is f ounces, pounds,
ounces) getch( )
Need to order calculations correctly.
5Math.h
include ltstdio.hgt include ltconio.hgt include
ltmath.hgt void main( ) float x, sinx,
cosx,powx2 printf(\n Enter a
number) scanf(f, x) sinx
sin(x) cosx cos(x) powx2
pow(x,2) printf(\n sin f is f, cos f is f,
f squared is f, x, sinx, x, cosx,
x, powx2) getch( )
Help files HELP menu ? INDEX. Then type name of
command.
6Loop runs the same command while certain
conditions are satisfied
Do.while Loop
void main( ) int i 0 do printf(\n
d, i) i while (ilt10) getch( )
7While Loop Very similar to Dowhile
While Loop
void main( ) int i 0 while (ilt10)
printf(\n d, i) i getch( )
8For Loop More detailed conditions than the
DoWhile or While loops
For Loop
void main( ) int i double value, root for
(i1, ilt20, i) valuei
rootsqrt(i) printf(\n Sq. root of lf is
lf,value,root) getch( )
Loop 1. i1, value 1, prints ?1. 2. For
statement increments i by 1 i i1 i 2, value
2, prints ?2. 19. i 19, value 19, prints
?19. 20. i 20, value 20, prints ?20. 21. i no
longer ? 20 so for loop is not run.
9If.Else gives 2 (or more) conditions that may
be satisfied.
If.else statements
void main( ) int z0 do if
(z20) printf(\n EVEN) else printf(\n
ODD) z while zlt20
I have put this in a dowhile loop otherwise z
would just keep increasing
- Ifelse if.else
- If condition what happens
- Second if condition what happens
- Else what happens if neither 1 nor 2
- are meet.