Professional Documents
Culture Documents
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 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.
Examples: if (grade<=100 && grade>=90) printf(You got A\n); if (grade<0 || grade >100) printf(Invalid Grade\n);
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
Nested if Statements
if statement contains another if statement. if ( gameIsOver == 0 ) if ( playerToMove == YOU ) printf ("Your Move\n");
if ( gameIsOver == 0 ){ if ( playerToMove == YOU ) printf ("Your Move\n"); else printf ("My Move\n"); }
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
#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; }
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; }