0% found this document useful (0 votes)
8 views20 pages

C++ Decision Making Statements Guide

Uploaded by

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

C++ Decision Making Statements Guide

Uploaded by

bluephoenix
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Branching (Selection)

Decision making statements


Chapter 04
IT1134
Problem: Read two numbers and print the bigger number.
Input: Two numbers A,B
Output: Bigger
Variables: A,B
Algorithm
STEP 1: START
STEP 2: READ A,B
STEP 3: IF A>B THEN
PRINT A
ELSE
PRINT B
ENDIF
STEP 4: END
 Example1: Write an algorithm that reads two values,
determines the largest value and the smallest value.

 Example 3: Draw a flowchart to find the largest of three


numbers A, B, and C (not using and).
Decision making statements
C++ Statements
• A simple C++ statement is each of the individual
instructions of a program, like the variable declarations
and assignment statements. They always end with a
semicolon (;), and are executed in the same order in
which they appear in a program.

• A compound statement is a group of statements (each


of them terminated by its own semicolon), but all grouped
together in a block, enclosed in curly braces: {}:{
statement1; statement2; statement3; }
if Statements
 An if statement is a construct that enables a program to specify
alternative path of execution.
 if statement executes an action if and only if the condition
is true. The syntax for a one-way if statement is shown here:
if (boolean-expression)
{
statement(s);
}
The boolean-expression is enclosed in parentheses
#include <iostream>
using namespace std;
int main () {
int a= 20;
if( a < 20 ) {
cout<< "a is less than 20" <<endl;
}
if( a > 20 ) {
cout<< "a is greater than 20" <<endl;
}
if( a == 20 ) {
cout<< "a is equal to 20" <<endl;
}
return 0;
}
if-else Statements
if (boolean-expression)
{
statement(s)-for-the-true-case;
}
else
{
statement(s)-for-the-false-case;
}

if( a < 20 ) {
cout<< "a is less than 20" <<endl;
}
else {
cout<< "a is greater than or equal to 20" <<endl;
}
Nested if-else Statements
The if...else statement executes two different codes depending upon whether the test
expression is true or false. Sometimes, a choice has to be made from more than 2
possibilities. The nested if...else statement allows you to check for multiple test
expressions and execute different codes for more than two conditions.
if (testExpression1)
{
// statements to be executed if testExpression1 is true
}
else if(testExpression2)
{
// statements to be executed if testExpression1 is false and testExpression2 is true
}
else if (testExpression 3)
{
// statements to be executed if testExpression1 and testExpression2 is false
and testExpression3 is true
}..
else
{
// statements to be executed if all test expressions are
false
}
#include <iostream>using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0)
{ cout<<"You entered a negative integer: " << number << endl;
}
else
{ cout << "You entered 0." << endl;
}
return 0;
}
switch … case Statements
1 #include <iostream>
2 using namespace std;
3 int main ()
4 {
5 char grade;
6 cout<<"Input your grade for IT1144:";
7 cin>>grade;
8 switch(grade)
9 {
10 case 'A’ :
11 cout << "Excellent! Your Grade is " << grade<<endl;
12 break;
13 case 'B’ :
14 cout << "Well Done! Your Grade is " << grade<<endl;
15 break;
16 case 'C’ :
17 cout << "Well Done! Your Grade is " << grade<<endl;
18 break;
19 case 'D’ :
20 cout << "You Passed! Your Grade is " << grade<<endl;
21 break;
22 case 'F’ :
23 cout << "Better try again! Your Grade is " <<grade<<endl;
24 break;
25 default :
26 cout << "Invalid grade" << endl;
27 }
28
29 return 0;
30 }
Switch … case statement
Switch … case statement
Switch … case statement
switch(i) {
case 'M':
case 'm':
cout<<"Individual is married "<<endl;
break;
case 'D':
case 'd':
cout<<"Individual is divorced"<<endl;
break;
case 'W':
case 'w':
cout<<"Individual is widowed "<<endl;
break;
default:
cout<<"Default "<<endl;
}

You might also like