Chapter Ii
Chapter Ii
CHAPTER II
CONDITIONAL AND SELECTIVE STRUCTURE
I. Objective
Understanding concept of conditional and selective structure in C++ programming
Can aplicating the conditional and selective structure into programming.
Can use if/ else and switch/case in problem’s solving.
II. Theory
Conditional structure
This structure use to choose an action from some options based on a condition.The
general form of conditional structure is if statement. The format is:
if (condition)
statement1;
else
statement2;
When condition x is 100, then “x is 100” is printed. In other hand, x is not 100, the
second statement is printed.
If the condition has alternative option after false executed. The format is :
if (condition)
statement1;
else if (condition2)
statement2;
else
statement3;
Some cases need conditional more than one, so nested if is good solving. The format is:
if (condition A)
{
if (condition1)
statement1;
else
statement2;
}
else
statement A;
Operator ( ? )
There is other sign whose function is same like if/else condition. It’s format is:
condition?result1:result2
If condition is True,the expression will return result1, if it is not it will return result2.
Selective Structure
The syntax of the switch statement is a bit different than if/else statement. The purpose is
to check several possible constant values for an expression. The format is :
switch (expression)
{ case constant1:
statements1;
break;
case constant2:
statements2;
break;
default:
default statements;}
Program will check value of expression, if the value is same as constant1, statements1
will executed, or if the value is constant2, only statements2 will executed. Otherwise, no value in
case, program run the default statement. Every case must follow by break, to identify program
that one case is finish.
#include <iostream>
using namespace std;
void main()
{
int angka;
cout<< "Choose one number from 1 to 3 :" << endl;
cin>> angka;
switch(angka)
{
case 1:
cout<< "your number is ONE";
break;
case 2:
cout<< "your number is TWO";
break;
case 3:
cout<< "your number is THREE";
break;
default:
cout<< "number is Out Of Range!";
}
system ("PAUSE");
}
16
17 else if(value>0 && value%2!=0)
18 {
19 cout << " This number is an odd number" << endl;
20 }
21
22 else
23 {
cout <<"The number has to be a positive
24
one"<<endl;
25 }
26
27 system ("PAUSE");
28 return 0;
29 }
25 return 0;
26 }
39 letter_grade = 'F';
40 break;
41 default: cout << " Not a recognizable grade" <<
endl;
42 }
43 cout << "Your grade was " << letter_grade << endl;
44 system ("PAUSE");
45 return 0;
46 }
Problems
1. What is different between if/else,else if, nested if,switch case, and operator ‘?’?
2. What is the use of break in switch/case program?
3. Make a simple program to sort alphabet from A to Z (min. 3 character alphabet)!
Explain the program!
4. Make a simple program use operator ‘?’ and then transform that program use if else
condition! Explain the program! (don’t take from modul).
5. Make a program using if/else,else if, nested if,switch case, and operator ‘?’! and explain
it! (don’t take from modul).