Escolar Documentos
Profissional Documentos
Cultura Documentos
CT103 Week 5
Note on float
You might notice that when you compile (build) a program 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
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 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
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
Output