Você está na página 1de 29

Programming

CT103 Week 5

Note on float
You might notice that when you compile (build) a program like this

You get a warning like this:

Note on float
By default, C assumes that a floating point number is a double default You can ignore the warning, just declare the variable type as double, or else do this (remember the typecast?) to get rid of it

Recap on Testing Data


The if statement works like:
If something is true do A, other wise do B

Cs Relational Operators: Cs
Relational Operator == > < >= <= != Description q Equal to Greater than Less than Greater than or equal to Less than or equal to Not equal to

Simple Example

Shorter version

Get away without {..} when only 1 line of code executed if condition is true

switch statement
U this in certain circumstances where Use thi i t i i t h you want to choose specific actions Use the Switch statement in C to evaluate a large number of values for a single variable or expression. Switch can be much tidier than a long g string of If Else statements.

Switch
switch (expression) { case value1: // do something break; case value2: // do something else break; ... default: break; }

Selection of option determined by value of an expression Type of value, returned by the expression, must be an integer Break important to avoid running on and executing the next case (if you leave it out, it will!)

Example

Sample Output

Conditional Operator

The conditional operator looks like:


relational test ? trueStatement : falseStatement; 1 2 3

The relational test will yield a value of true or false Remember the example:
if (salary > threshold) ( y ) { tax_rate = 50; } else { tax_rate = 20; } 1 2

Example

Another example
void main() { int age; float gift; printf ("how old are you?: ); ( how you?:"); scanf ("%d", &age); if (age < 18) { gift = 5.0; 50 } else { gift = 10.0; } printf ("your gift is %.2f\n", gift); }

Can be written ..
void main() id i () { int age; float gift; g ; printf ("how old are you?:"); scanf ("%d", &age); gift = (age < 18) ? 5.0 : 10.0; printf ("your gift is %.2f\n , gift); ( your % 2f\n" }

Useful example

More cleverer version

Repeating Code
When we want to repeat a piece of code a number of times, an easy way is using the while statement
while (condition) { block of one or more C statements}

The condition is a relational test, just like in the if statement The block of C statements is called the body of the while loop

Using if:
#include "stdafx.h" #include tdi h #i l d <stdio.h> void main() { int amount = 0; int wrongVal = 0; if (amount < 25) { wrongVal++; printf("Amount i t i tf("A t is too small. \ ") ll \n"); printf("Number of wrong values = %d \n", wrongVal); printf ("Try again....enter value: "); scanf( %d &amount); scanf("%d",&amount); } }

Output

Using while:
#include "stdafx.h" #include tdi h #i l d <stdio.h> void main() { int amount = 0; int wrongVal = 0; while (amount < 25) { wrongVal++; printf("Amount i t i tf("A t is too small. \ ") ll \n"); printf("Number of wrong values = %d \n", wrongVal); printf ("Try again....enter value: "); scanf( %d &amount); scanf("%d",&amount); } }

Output

Avoid infinite loops! Y must change a variable i id th You t h i bl inside the while loops body that is used in the condition otherwise you could end up i diti th i ld d in an infinite loop

E.g.
#include "stdafx.h" #include tdi h #i l d <stdio.h> void main() { int amount = 0; int wrongVal = 0; while (amount < 25) { wrongVal++; printf("Amount i t i tf("A t is too small. \ ") ll \n"); printf("Number of wrong values = %d \n", wrongVal); printf ("Try again....enter value: "); / scanf( %d &amount); / /* scanf("%d",&amount); */ } }

Looping

do.while
#include "stdafx.h" #include <stdio.h> void main() { int amount = 0; int wrongVal = 0; do d { wrongVal++; printf( Amount printf("Amount is too small. \n"); \n ); printf("Number of wrong values = %d \n", wrongVal); printf ("Try again....enter value: "); scanf("%d",&amount); } while (amount < 25); }

Only use do..while if the body of the loop must execute at least once

output

Example: BMI Calculator


#include "stdaf h" #incl de "stdafx.h" #include <stdio.h> void main() { float height; float weight; float bmi; int again = 1; do { printf ("Enter your weight in kilos: "); scanf ("%f" &weight); ("%f", printf ("Enter your height in metres: "); scanf ("%f", &height); bmi = (weight)/(height*height); printf ("Your BMI is: %f. \n Enter 1 to do again or 0 to exit.\n", bmi); scanf ("%d", &again); ( %d } while (again == 1); }

Output

Você também pode gostar