3 and 4 - If - Else, Switch
3 and 4 - If - Else, Switch
Programs we have written so far consisted of a sequence of steps executed one after the
other. The program ended when all statements had been executed.
Now we expand our horizons and look at the decision making capability of the
computer. There are two kinds of BRANCHES.
Example:
This program checks to see if a number is positive.. If it's positive, the program prints
"Your number is positive." Then, for either case whether the number is positive or
negative, the program prints "Goodbye".
#include<stdio.h>
main()
{
int number;
printf(“Enter an integer”);
scanf(“%i”,&number);
if (number >= 0) /* check if number >= 0 */
puts(“Your number is positive."); /* print this only if number >= 0 */
And then execute THIS statement and those that follow in either case
________________________________________________________________________
Any time more than one line is executed in case the EXPRESSION is TRUE you
MUST use braces:
if(number < 0)
{ // THE BRACE COMES UNDER THE i
printf("The number is less than zero"); // TAB LINES INSIDE BRACES
printf("Since we have more than one line we must uses { }"); // TAB
} THE CLOSING BRACE COMES UNDER THE OPENING BRACE
The braces come under the ‘i’ of the if, and EVERYTING INSIDE BRACES MUST
BE TABBED!!!
PRG 155 Relational Operators
A Relational Operator is a symbol that indicates the relationship between two quantities
compared in an expression. the result of the comparison is either TRUE or FALSE.
The program interprets TRUE as a 1 (or 1 as TRUE) and FALSE as a 0 (or 0 as FALSE).
You can demonstrate this in the following program. Try it in the lab or at home.
value = 5 > 6;
printf(“ %d”, value); // This will print?
value = 6>5;
printf(“\n %d ”,value); // This will print?
value = b>7;
prinft(“\n%i”,value); // This will print?
A = 5 B = 10 C = 5
(A > B)
A==C
B != C
A <= B
C != B
A<=C
A!>B
_______________________________________________________________________
Logical Operators
and for :
OR it is | |
Here is how AND and OR work:
Example:
int a = 5, b = 6;
A = 5 B = 10 C = 5
(A > B) && C == 5
A = = C && B < C
B != ( C - A )
B <= A || C != 6
C != B || 6 = = 2*5
A = = C && B > 5
( A + C ) = = B || A = = 1
2- Closed Branch – The IF ....... ELSE statement
Example:
#include(stdio.h)
main()
{
int number;
system(“cls”);
if(number >= 0)
{
printf(“Thank you for entering a positive number\n”);
printf("Remember to use braces with more than one line of code");
}
else
{
printf(" %d is not a positive number!!!!!!!!”, number);
printf(" Once again, braces are used ( because of this statement)");
}
And then execute this STATEMENT and those that follow in either case
Compound If / Else
You can have a string of if/else statements when more than two conditions are to be
checked.
For example, you would use the compound if/else if the user has to select one of three
options in a menu. Then, depending on the option selected the appropriate instructions
are entered. I have shown the date input, calculation and output for option a. For options
b and c I've just printed a statement ot keep the example shorter.
Example:
#include<stdio.h>
main()
{
float radius, Area_Circle;
char selection;
puts(" Selection Menu ");
puts("");
puts("a. Calculate the area of a circle.");
puts("b. Calculate the area of a rectangle.");
puts("c. Calculate the are of right triangle.");
main()
{
int number;
printf("Please enter a number");
scanf("%i", &number);
if(number >0)
printf(" \n\tThe number is greater than zero")
if(number < 0)
printf(" \n\tThe number is negative")
if(number%2 == 0)
printf("\n\tThe number is even ");
if(number%2 == 1)
printf("\n\tThe number is odd");
if(number%3 == 0)
printf("\n\tThe number is divisible by 3");
if(number > 100)
printf("\n\tThe number is greater than 100);
getch();
}
Switch and Conditional Operator
The if / else construct can be written more efficiently using the switch function. The switch can
only be used with integers or characters NOT float.
#include<stdio.h>
main()
{
char input;
printf("Enter A, B or C ");
scanf("%c",&input);
/* START OF IF/ELSE */
if (input == 'A')
puts(" Letter A");
else
printf(" You selected the wrong letter");
printf("\nEnd of IF/ELSE\n");
/************************************************************/
/*OR YOU CAN USE A SWITCH INSTEAD - START OF SWITCH */
puts("\nUSING SWITCH");
switch(input)
{
case 'A' : printf(" Letter A");
break;
case 'B' : printf(" Letter B");
break;
case 'C' : printf(" Letter C");
break;
default : printf(" You selected the wrong letter");
}
printf("\nEnd of SWITCH");
Note the use of break; to force program flow to jump to the end of the switch.
The case label ( the constant used after "case" used in the switch must be either a
character or an integer.
Conditional Operator
This is a shortcut way of writing an If / Else statement called the Conditional Operator.
This type of question will be on the final.
Example:
if (A>5)
printf("A is greater than 5);
else
printf("A is less then 5);
Exercise:
int x, y
if (x>0)
y = x;
else
y = -x;
1. Use a switch to help the user select between various coloured sweaters (R for red, G
for green and B for blue). Then print the colour he/she selected.