0% found this document useful (0 votes)
43 views8 pages

Chapter Ii

This document discusses conditional and selective structures in C++ programming. It covers if/else statements, else if statements, nested if statements, the ? operator, and switch/case statements. Examples are provided for each structure. The experimental procedures section provides code listings for sample programs using each structure. Problems at the end ask the reader to compare the different structures, explain the use of break in switch/case, write programs to sort letters and use different conditional structures, and explain how they work.

Uploaded by

yoan winata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views8 pages

Chapter Ii

This document discusses conditional and selective structures in C++ programming. It covers if/else statements, else if statements, nested if statements, the ? operator, and switch/case statements. Examples are provided for each structure. The experimental procedures section provides code listings for sample programs using each structure. Problems at the end ask the reader to compare the different structures, explain the use of break in switch/case, write programs to sort letters and use different conditional structures, and explain how they work.

Uploaded by

yoan winata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Computer Laboratory - JTE Unand 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;

If condition is True, then statement1 is executed. Otherwise, statement2 is executed. A


condition is an expression or a declaration with initialization that selects flow of control. Here is
an example :
if (x == 100)
cout<< "x is 100";
else
cout<< "x is not 100";

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;

1 | C++ Programming Module


Computer Laboratory - JTE Unand Chapter II

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.

2 | C++ Programming Module


Computer Laboratory - JTE Unand Chapter II

An example of switch case selective structure :

#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");
}

3 | C++ Programming Module


Computer Laboratory - JTE Unand Chapter II

III. Experimental Procedure


III.1 Conditional : if else
1. Type this listing program on your C++ source file.
code
1 // Program if Condition
2 #include<iostream>
3 using namespace std;
4
5 int main()
6 {
7 int score;
8
9 cout<<"What is your average score ? ";
10 cin >>score;
11 if(score<70)
12 {
13 cout<< "Sorry, you didn’t make it \n";
14 }
15
16 else
17 { cout<<"Congratulation you passed"<<endl; }
18
19 system ("PAUSE");
20 return 0;
21 }

III.2 Conditional : else if


1. Type this listing program on your C++ source file.
code
1 // Program else if Condition
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 int value;
8
9 cout << "Input the number you want : ";
10 cin >>value;
11
12 if(value>0 && value%2==0)
13 {
14 cout << "This number is an even number" << endl;
15 }
4 | C++ Programming Module
Computer Laboratory - JTE Unand Chapter II

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 }

III.3 Conditional : nested if


1. Type this listing program on your C++ source file.
code
1 // Program nested if Condition
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 int num;
8 cout << "Input a number : ";
cin >>
9
num;
10 if(num>=0)
11 {
12 if(num%2==0)
13
14 cout<<" This number is an even number"<<endl;
15
16 else
17
18 cout<<" This number is an odd number"<<endl;
19
20 }
21 else
cout<<" The number has to be a positive
22
one"<<endl;
23
24 system ("PAUSE");
5 | C++ Programming Module
Computer Laboratory - JTE Unand Chapter II

25 return 0;
26 }

III.4 Selective: switch case


1. Type this listing program on your C++ source file.
code
1 // Program switch case
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 int grade;
8 char letter_grade = 'Z';
9
10 cout << "Enter Your Number Score:" << endl;
11 cin >> grade;
12
13 grade = (grade > 100) ? -10 : grade;
14
15 switch (grade/10)
16 {
17 case 10: cout << "First in Class!\n";
18 letter_grade = 'A';
19 break;
20 case 9: cout << "Congratulations!\n";
21 letter_grade = 'A';
22 break;
23 case 8: cout << "Very Good\n";
24 letter_grade = 'B';
25 break;
26 case 7: cout << "Okay\n";
27 letter_grade = 'C';
28 break;
29 case 6: cout << "Work harder\n";
30 letter_grade = 'D';
31 break;
32 case 5:
33 case 4:
34 case 3:
35 case 2:
36 case 1:
37 case 0:
38 cout << " Sorry you failed\n";

6 | C++ Programming Module


Computer Laboratory - JTE Unand Chapter II

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 }

7 | C++ Programming Module


Computer Laboratory - JTE Unand Chapter II

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).

8 | C++ Programming Module

You might also like