notes2

# flow chart for an if statement if num1 7 0 printf

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Output for program on previous page: % a.out 015 is an integer. 3.142 is a floating point number. aabbbcc is a string. % 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...
View Full Document

## This note was uploaded on 03/02/2009 for the course ENGR 3 taught by Professor Ben-yaakov during the Fall '08 term at UCSB.

Ask a homework question - tutors are online