notes2

notes2 - What weve learned so far Basic structure of a C...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
• Basic structure of a C program • Printing output: printf • Reading in input: scanf • Basic arithmetic operations • Decision making: if statements • Debugging programs • Algorithms • Reading for this week • Chap. 3 • Section 4.10
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
We used the following scanf and printf statements last week: – scanf( “%d”, &integer1 ); – printf( “Sum is %d\n\n”, sum ); – Here integer1 and sum were variables, and %d indicated the format of the data (we used %d for integer). Other format types: –%d = decimal integer (can also be of the form %.3d , %5d or %5.3d ) –%f = floating point number (can also be of the form %.2f or %6.2f ) – %lf = double (use %f in printf statement, %lf in scanf statement) –%c = character –%s = string (in printf only)
Background image of page 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /* More on printf */ #include <stdio.h> /* function main begins program execution */ int main() { float pie = 3.1416; printf( "\n%.3d is an integer.", 15 ); printf( "\n%.3f is a floating point number.", pie ); printf( "\n%s is a string\n", "aabbbcc" ); return 0; /* indicate that program ended successfully */ } /* end function main */
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Output for program on previous page: % a.out 015 is an integer. 3.142 is a floating point number. aabbbcc is a string. %
Background image of page 4
if (num1 % 7 == 0 ) printf( “%d is divisible by 7.\n”, num1 ); num1 % 7 == 0 print “num1 is…” true false
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
•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”
Background image of page 6
if (num1 % 7 == 0 ) printf( “%d is divisible by 7.\n”, num1 ); else printf(“%d is not divisible by 7.\n”, num1 ); num1 % 7 == 0 print “num1 is…” true false print “num1 is not…”
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
•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 ) –Examp le s : • printf( “%s\n”, grade >= 60 ? “Passed” : “Failed” ); • grade >=60 ? printf( “Passed\n” ) : printf( “Failed\n” );
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 */
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 32

notes2 - What weve learned so far Basic structure of a C...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online