You are on page 1of 25

exercise

 Write a program to receive two integers and


determines largest and smallest in the group.

 Write a program that reads an integer and


determines and prints whether it is odd or
even. Use the modulus operator. Any number
leaves a reminder of zero when divided by 2
is an even number.
if-else statement
 The expression is first
evaluated, if the expression
is true , statement_1 is
executed, otherwise
statement_2 is executed.

 Syntax: Expression
True

if expression
statement_1
False
else Statement_1
Statement_2
statement_2
if-else statement
 Example:
a) if (cgpa < 2.0 )
status = ‘F’;
else
status = ‘P’;

b) if ( choice == 1)
gender = ‘M’;
else
gender = ‘F’;
Example: if-else statement

#include <iostream.h>
void main()
{
int marks;
cout<< “Enter marks: ”;
cin>> marks;
if (marks >= 50)
{
cout<<“*******CONGRATULATION*******\n”;
cout<< “You pass the exam”;
}
else
cout<< “Sorry, you fail the exam”;
}
if-else-if statement
 Syntax:
if (expression1)
statement1;
else if (expression2)
statement2;
:
:
else if(expressionn )
statementn;
else
last_statement;
If…else if statement
 The if…else if statement is an extension of the "if else“
statement.
 In this form, expression1 is first evaluated. If it is true,
statement1 is executed and the whole statement terminated.
 On the other hand, if expression1 is false, control passes to
the else if part and expression2 is evaluated. If it is true,
statement2 is executed and the whole statement is
terminated.
 If it is false, other else if parts (if any) are tested in a similar
way.
 Finally, if expressionn is true, statementn is executed; if not,
last_statement is executed.
 Only ONE of the statements will be executed
Example 1:if…else-if statement
#include <iostream.h>

void main()
{
int marks;

cout<< "Enter marks:";


cin>> marks;

if (marks >= 80)


cout<< "A";
else if (marks >= 70)
cout<< "B";
else if (marks >= 60)
cout<< "C";
else if (marks >= 50)
cout<< "D";
else
cout<< "F";

}
Example 2:if…else if statement
Write a program to test whether a banking
transaction is a deposit, withdrawal, transfer or
an invalid transaction, than for a valid
transaction enter amount.
#include<iostream.h>
void main()
{
float amount;
char transaction_code;
cout <<"\n****************************************";
cout <<"\n** Types of banking transaction : **";
cout <<"\n** D for deposit **";
cout <<"\n** W for withdrawal **";
cout <<"\n** T for transfer **";
cout <<"\n****************************************";
cout <<"\n\nEnter your transaction : ";
cin >>transaction_code;
if (transaction_code == 'D')
{
cout << "Deposit transaction";
cout << "\nEnter amount : ";
cin >> amount;
}
else if (transaction_code == 'W')
{
cout << "Withdrawal transaction";
cout << "\nEnter amount : ";
cin >> amount;
}
else if (transaction_code == 'T')
{
cout << "Transfer transaction";
cout << "\nEnter amount : ";
cin >> amount;
}
else
{
cout << "Invalid transaction!";
cout << "\nPlease enter the correct transaction code";
}
}
Nested if
 generally take the forms:
if-if else.

 Nested If statement means to use the if


statement inside the other if statement.
if-if else statement
 Example:
 Syntax:
#include<iostream.h>
void main ()
if (expression1) {
int year_served;
if (expression2) char status; // t-tetap, s-sementara
if (expression3) cout<<"Enter year served by employee : ";
statement1; cin>>year_served;
cout<<"Enter the employee job status : ";
else cin>>status;
if (status == 't')
statement2; if (year_served > 7)
else cout<<"Bonus 2 month salary";
else
statement3; cout<<"Bonus 1 month salary";
else if(status == 's')
else cout<<"Bonus 1/2 month salary";
else
statement4; cout<<"salah status";
}
 In this nested form, expression1 is evaluated. If it is false,
statement4 is executed and the entire nested if statement
is terminated;
 if expression1 is true, control goes to the second if (within
the first if) and expression2 is evaluated. If it is false,
statement3 is executed;
 if expression2 is true, control goes to the third if (within
the second if) and expression3 is evaluated.
 If it is false, statement2 is executed; if not, statement1 is
executed.
 Only ONE of the statements is executed.
 One simple rule to follow is: always match an else with
the nearest if before the else.
Checking Numeric Ranges with
Logical Operators

 Used to test if a value falls inside a range:


if (grade >= 0 && grade <= 100)
cout << "Valid grade";
 Can also test if value falls outside of range:
if (grade <= 0 || grade >= 100)
cout << "Invalid grade";
 Cannot use mathematical notation:
if (0 <= grade <= 100) //doesn’t work!
Comparing Strings
 You cannot use relational operators to
compare strings
 Must use the strcmp function to compare
strings
 strcmp compares character-by-character
Strings

Read string
cin.getline(firstString, SIZE);

Comparing string
The expression
strcmp(str1, str2)

compares the strings str1 and str2

 It returns 0 if the strings are the same.


 It returns a negative or positive number if the strings are not same.
// This program correctly tests two strings for equality with the strcmp function.
#include<iostream.h>

void main()
{
const int SIZE = 20;
char firstString[SIZE], secondString[SIZE];

cout<<"Enter first string : ";


cin.getline(firstString, SIZE);
cout<<"Enter second string : ";
cin.getline(secondString, SIZE);

if (strcmp (firstString, secondString) == 0)


cout<<"You entered the same string twice.\n";
else
cout<<"The string is not same.\n";
}
Switch statement
 The switch construct is an elegant alternative
to the more complex if..else-if construct which
is more difficult to read and understand.
 switch statement just can be implemented for
expression with an integer or character
constant.
 Syntax:
switch (expression)
{
case expression_1 :
statement sequence;
break;
case expression_2 :
statement sequence;
break;
:
case expression_n :
statement sequence;
break;
default :
statement sequence;
}
 When the switch statement is executed, the
expression is first evaluated.
 If it evaluates to expression_1, the statement
sequence for case expression_1 is executed followed
by the break which terminates the entire switch
statement.
 If it evaluates to expression_2, then the statement
sequence for case expression_2 is executed followed
by break which then terminates the switch statement;
and so on.
 If it evaluates to none of the case expressions, the
statement sequence for the default case is executed,
thus terminating the switch statement.
Example Switch
#include<iostream.h>
main ()
{ 
 Syntax:
int number; switch (expression)
cout<<"enter a number : "; {
case expression_1 :
cin>> number; statement sequence;
break;
switch ( number ) case expression_2 :
{ statement sequence;
case 1: break;
cout<< " one "; :
break;
case 2: case expression_n :
statement sequence;
cout<< " Two "; break;
break; default :
case 3: statement sequence;
cout<< " Three "; }
break;
default:
cout<< " Other number ";
}
}
break statement
 Used to exit a switch statement
 If it is left out, the program "falls through" the
remaining statements in the switch
statement
// Program to determine whether a student meet the prerequisite of principle of programming
#include <iostream.h>

void main()
{
char gred;

cout<<"----------------------------------------------------------";
cout<<"\nEnter student grade for discrete math : ";
cin>>gred;

cout<<"\n**********************************************************";

switch (gred)
{
case 'A':
cout<<"\nYou deserve to take the principle of programming course.\n";
cout<<"Prerequisite are fulfilled.";
break;
case 'B':
cout<<"\nYou deserve to take the principle of programming course. \nPrerequisite are
fulfilled.";
break;
default:
cout<<"\nYou are not deserve to take the principle of programming course.";
cout<<"\nPrerequisite are not fulfilled.\nMinimun B in discrete math";
}
cout<<"\n**********************************************************";
}
Conditional Operator
 It provides a shorthand method of expressing a simple if/else
statement.
 Syntax:
expression ? statement1 : statement2;
 Example:
(x < 0) ? (y = 10) : (z = 20);
 The condition above is equivalent with
if (x <0)
y = 10;
else
z = 20;
 Can be assigned to a variable:
result = x <= y;
 Assigns 0 for false, 1 for true
 Evaluate
 Let x = 10 and y = -7
result = __________
Exercise
1. Write an if/else statement that assigns 1 to x if y is equal
to 100. Otherwise it should assign 0 to x.

1. Rewrite the above C++ codes by using a switch


statement.

2. Write if statements that perform the following test : If


amount1 is greater than 10 and amount2 is less than
100, display the greater of the two.

1. Rewrite the above C++ codes by using nested if


statement.

You might also like