class_notes3

class_notes3 - More variables… Numeric data types...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: More variables… Numeric data types Integers Floating-point values short int long float double long double can be ‘unsigned’ More variables… Example Data Type Limits Integers Max/Min = ±32,767 short Max/Min = ±32,767 int unsigned int Max/Min = 0/+65,535 Max/Min = ±2,147,483,647 long Floating Point 6 digits of precision float Maximum exponent = 38 Maximum value = 3.402823e38 double 15 digits of precision Maximum exponent =308 Maximum value = 1.797693e308 19 digits of precision Maximum exponent = 4932 Maximum value = 1.189731e4932 long double Output – printf() printf( control string, indentifier(s) ); control string: “text + format specifiers” indentifier(s): 0 or more variable names Output – printf() Numeric Conversion Specifiers Variable Type Specifier Integer Values short %i, %d int %i, %d long %li, %ld Floating Point float %f, %e, %g double %f, %e, %g long double %Lf, %Le, %Lg Input – scanf() scanf( control string, indentifier(s) ); control string: “format specifiers” indentifier(s): 0 or more variable addresses Decision Making: the if statement • Every executable statement in C either performs an action (i.e. performs a calculation, prints output,…) or performs a decision • if statement – Simple example of performing a decision – If a condition is true, then the body of the if statement is executed – Conditions in if are formed by using equality operators and relational operators Decision Making: the if statement • Equality and Relational Operators: (Fig. 2.12) Standard algebraic equality operator or relational operator Equality operators = ≠ Relational operators > < >= <= > < >= <= x>y x<y x >= y x <= y x is greater than y x is less than y x is greater than or equal to y C equality or relational operator Example of C condition Meaning of C condition == != x == y x != y x is equal to y x is not equal to y x is less than or equal to y C Program: Decision Making 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /* Decision Making: using the if statement*/ #include <stdio.h> /* function main begins program execution */ int main() { int num1, num2; /* declare variables */ printf( "\nEnter two integers, and I will tell you\n" ); printf( "the relationship they satisfy: " ); scanf( "%d%d", &num1, &num2 ); /* read two integers */ if (num1 == num2 ) { printf( "%d is equal to %d\n", num1, num2 ); } /* end if */ if (num1 != num2 ) { printf( "%d is not equal to %d\n", num1, num2 ); } /* end if */ C Program: Decision Making continued 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 if (num1 < num2 ) { printf( "%d is less than %d\n", num1, num2 ); } /* end if */ if (num1 > num2 ) { printf( "%d is greater than %d\n", num1, num2 ); } /* end if */ if (num1 <= num2 ) { printf( "%d is less than or equal to %d\n", num1, num2 ); } /* end if */ if (num1 >= num2 ) { printf( "%d is greater than or equal to %d\n", num1, num2 ); } /* end if */ return 0; /* indicate that program ended successfully */ } /* end function main */ Algorithms • Basic strategy (recipe) for getting a program to perform it’s functions (coach’s playbook). Consists of: – Actions to be performed – Order in which these actions are executed • Pseudocode – Statements written in plain English which state what you want the program to do • Flow charts: good way to develop an algorithm – Diamonds: decision symbols – Rectangles: action symbols – Ovals: beginning or end of program or section of code Algorithms • Before writing a program: – Have a thorough understanding of the problem – Carefully plan an approach for solving it • While writing a program: – Know what programming tools are available – Use good programming principles Flow chart for an if statement if (num1 % 7 == 0 ) printf( “%d is divisible by 7.\n”, num1 ); true num1 % 7 == 0 print “num1 is…” false The if…else Selection Statement • if – Only performs an action if the condition is true • if...else – Specifies an action to be performed both when the condition is true and when it is false. • Pseudocode If ( num1 mod 7 ) is equal to 0 Print “num1 is divisible by 7” else Print “num1 is not divisible by 7” Flow chart for an if…else statement if (num1 % 7 == 0 ) printf( “%d is divisible by 7.\n”, num1 ); else printf(“%d is not divisible by 7.\n”, num1 ); print “num1 is not…” false true num1 % 7 == 0 print “num1 is…” The if…else Selection Statement • C code: if ( num1 % 7 == 0 ) { printf( “%d is divisible by 7\n”, num1 ); } else { printf( “%d is not divisible by 7\n”, num1 ); } • Ternary conditional operator (?:) – Takes 3 arguments • ( condition ? value if true : value if false ) – Examples: • printf( “%s\n”, grade >=60 ? “Passed” : “Failed” ); • grade >=60 ? printf( “Passed\n” ) : printf( “Failed\n” ); C Program: if…else 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* decision making: using the if...else statement*/ #include <stdio.h> /* function main begins program execution */ int main() { int num1; /* declare variables */ printf( "\nEnter an integer, and I will tell you\n" ); printf( "if it is divisible by 7: " ); scanf( "%d", &num1 ); /* read integer */ if ( num1 % 7 == 0 ){ printf( "%d is divisible by 7.\n", num1 ); } else { printf( "%d is not divisible by 7.\n", num1 ); } return 0; /* indicate that program ended successfully */ } /* end function main */ C Program: nested if…else 1 2 3 4 5 6 7 8 9 10 11 12 13 /* using nested if...else statements*/ #include <stdio.h> /* function main begins program execution */ int main() { int score; /* declare variables */ printf( "\nEnter your score, and I will tell you\n" ); printf( "your grade: " ); scanf( "%d", &score ); /* read integer */ C Program: nested if…else if ( score >= 90 ) 14 printf( "You got an A.\n" ); 15 else 16 if ( score >= 80 ) 17 printf( "You got a B.\n" ); 18 else 19 if ( score >= 70 ) 20 printf( "You got a C.\n" ); 21 else 22 if ( score >= 60 ) 23 printf( "You got a D.\n" ); 24 else 25 printf( "You got an F.\n" ); 26 27 return 0; /* indicate that program ended successfully */ 28 29 30 } /* end function main */ C Program: ternary conditional operator 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /* using the ternary conditional operator */ #include <stdio.h> /* function main begins program execution */ int main() { int score; /* declare variables */ printf( "\nEnter your score, and I will tell you\n" ); printf( "if you passed: " ); scanf( "%d", &score ); /* read integer */ printf( "%s\n", score >= 60 ? "You passed!!!" : "You failed." ); return 0; /* indicate that program ended successfully */ } /* end function main */ Logical Operators • && ( logical AND ) – Returns true if both conditions are true • || ( logical OR ) – Returns true if either of its conditions are true • ! ( logical NOT, also called logical negation ) – Reverses the truth/falsity of a statement Example Program 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /* logical operators, using the ternary conditional operator */ #include <stdio.h> int main() { int num1, num2, num3, num4; printf( "\nEnter two integers, and I will tell you\n" ); printf( "if they are both even: " ); scanf( "%d%d", &num1, &num2 ); /* read integer */ /* check if both integers are even */ num1 % 2 == 0 && num2 % 2 == 0 ? printf( "\nBoth integers are even.\n\n" ) : printf( "\nAt least one integer is not even.\n\n" ); Example Program 19 20 21 22 23 24 25 26 27 28 29 30 31 32 printf( "\nEnter two integers, and I will tell you\n" ); printf( "if at least one is even: " ); /* check if at least one integer is even */ scanf( "%d%d", &num3, &num4 ); /* read integer */ num3 % 2 == 0 || num4 % 2 == 0 ? printf( "\nAt least one integer is even.\n\n" ) : printf( "\nNeither integer is even.\n\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ Example Program with Logical NOT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /* example with logical NOT */ #include <stdio.h> /* function main begins program execution */ int main() { int num1; /* declare variables */ printf( "\nEnter a positive integer, and I will tell you\n" ); printf( "if it is divisible by 7: " ); scanf( "%d", &num1 ); /* read integer */ if ( !( num1 > 0) ) printf( "\nThe integer you entered is not positive.\n" ); else printf( "\n%d is%sdivisible by 7.\n", num1, num1 % 7 == 0 ? " " : " not " ); return 0; /* indicate that program ended successfully */ } /* end function main */ ...
View Full Document

This note was uploaded on 08/06/2010 for the course ENGR 3 taught by Professor Ben-yaakov during the Summer '08 term at UCSB.

Ask a homework question - tutors are online