Practice3 - ReminderVariables#include intmain(void...

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

View Full Document Right Arrow Icon
    Reminder - Variables #include  <stdio.h> int  main( void ) { double  cm, inches; printf("Please enter length in centimeters: "); scanf("%lf",&cm); inches = cm / 2.54; printf("This is equal to %g inches\n", inches); return  0;
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
    Reminder - Characters /* Get the position of a letter in the abc */ #include  <stdio.h> int  main( void ) { char  letter; printf("Please enter a lowercase letter\n"); scanf("%c", &letter); printf("The position of this letter in the abc is %d\n", letter-'a'+1); return  0; }
Background image of page 2
    Reminder - Arithmetic 3 + 4 = 7 3.0 + 4 = 7.0 3/4=0  3.0 / 4 = 0.75 ‘a’/4=24 100%9=1
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
    Selection Statements Selects statements to execute based on  the value of an expression  The expression is sometimes called the  controlling expression    Selection statements:  if statement switch statement
Background image of page 4
    Selection statements: if used to execute  conditionally  a statement or  block  of code.           if  ( expression statement If  expression  is  true statement  is executed (the definition of true is explained shortly) statement  can be replaced by a block of  statements, enclosed in curly braces.
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
    An example /* This program displays the absolute value of a number given by the user */ #include  <stdio.h> int  main( void ) { double  num; printf("Please enter a real number: "); scanf("%lf", &num); if  (num<0) num = -num; printf("The absolute value is %g\n", num); return  0; }
Background image of page 6
    If-else statement if  ( expression statement 1   else   statement 2   if  expression  is true,  statement 1  is executed.  if  expression  is false,  statement 2  is executed  both statements can be (and very often are)  replaced by blocks of statements (“compound  statements”) 
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
    An example (fragment) int  first, second, min; /* … */ if  (first < second)  {    min = first;    printf ("The first number is smaller than the second.\n"); else   {    min = second;    printf ("The second number is smaller than the first\n"); } printf("The smaller number is equal to %d\n", min);
Background image of page 8
    True or false In C, every expression has a  numeric  value An expression is ‘true’ when its  non-zero If it is  zero , it is false Therefore, in the following – if ( expression statement statement  is executed if  expression  is non  zero.
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
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 39

Practice3 - ReminderVariables#include intmain(void...

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

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