You are on page 1of 13

CS 1111 Computing Fundamentals First Semester 2008-2009

Chapter 6 Making Decisions

Decision making constructs

The if statement The switch statement The conditional operator

The if Statement
The syntax is: if( condition){ statement1; statement2; } else{ statement3; statemnet4; } If the condition evaluates to True, then statement1 and statement2 (the body of if statement) will be executed. Otherwise, statement3 and statement4 (The body of else statement) will be executed.

The if Statement
// Program to calculate the absolute value of an integer int main (void) { int number; printf ("Type in your number: "); scanf ("%i", &number); if ( number < 0 ){ number = - number; } printf ("The absolute value is %i\n", number); return 0; } Type in your number: -100

The absolute value is 100

The if Statement
Some Examples on if statement: - if( a == 0) - if( a > 0) - if( a < 0) - if( a >= 0) - if( a <= 0) - if( a != 0) - if( 0 ) checks if a is equal to 0. checks if a is greater than 0. checks if a is less than 0. checks if a is greater or equal than 0. checks if a is greater equal than 0. checks if a is not equal to 0.

This is always evaluates to false.

The if-else Construct


// Program to determine if a number is even or odd #include <stdio.h> int main () { int number_to_test, remainder; printf ("Enter your number to be tested: "); scanf ("%i", &number_to_test); remainder = number_to_test % 2; if ( remainder == 0 ) printf ("The number is even.\n"); else printf ("The number is odd.\n"); Enter your number to be tested: 1234 return 0; The number is even. }

Compound Relational Tests


If we need to combine more than one condition inside if statement, we use the following && : used if we need both conditions to be true || : used if we need at least one condition to be true

Examples: if (grade<=100 && grade>=90) printf(You got A\n); if (grade<0 || grade >100) printf(Invalid Grade\n);

Operator Precedence & Associativity

Operators
++ * + < == && || ?: = , += -= *= /= %= -/ <= != > >= + % ! (type)

Associativity
right to left left to right left to right left to right left to right left to right left to right right to left right to left left to right

Type
unary multiplicative additive relational equality logical AND logical OR conditional assignment comma

Fig. 4.16

Operator precedence and associativity.

Nested if Statements
if statement contains another if statement. if ( gameIsOver == 0 ) if ( playerToMove == YOU ) printf ("Your Move\n");

if ( gameIsOver == 0 && playerToMove == YOU ) printf ("Your Move\n");

if ( gameIsOver == 0 ){ if ( playerToMove == YOU ) printf ("Your Move\n"); else printf ("My Move\n"); }

The else if Construct

if ( expression 1 ) program statement 1 else if ( expression 2 ) program statement 2 else program statement 3

if ( expression 1 ) program statement 1 else if ( expression 2 ) program statement 2 else program statement 3

Program to implement the sign function

#include <stdio.h> int main (void) { int number, sign; printf ("Please type in a number: "); scanf ("%i", &number); if ( number < 0 ) sign = -1; else if ( number == 0 ) sign = 0; else // Must be positive sign = 1; printf ("Sign = %i\n", sign); return 0; }

Please type in a number: 1121 Sign = 1

(Rerun) Please type in a number: -158 Sign = -1

Switch Statement
C language has another type of conditional statements, the switch statement. The switch statement can be used when the condition is evaluated to a single value. In other words, it can be used instead of the following if statement:
if ( x == something ) else if ( x == something else) else if ( x == something else) else

Switch Statement
switch ( expression ) { case value 1: program statement program statement ... break; case value 2: program statement program statement ... break; ... case value n: program statement program statement ... break; default: program statement program statement ... break; }

#include <stdio.h> int main (void) { float value1, value2; char operator; printf ("Type in your expression.\n"); scanf ("%f %c %f", &value1, &operator, &value2); switch (operator) { case '+': printf ("%.2f\n", value1 + value2); break; case '-': printf ("%.2f\n", value1 - value2); break; case '*': printf ("%.2f\n", value1 * value2); break; case '/': if ( value2 == 0 ) printf ("Division by zero.\n"); else Try to run the printf ("%.2f\n", value1 / value2); program break; default: without one of printf ("Unknown operator.\n"); the break break; statements } return 0; }

You might also like