0% found this document useful (0 votes)
185 views28 pages

3) Selection Control Structure (Part 2) PDF

This document provides an overview of selection control structures and string library functions in C++. It discusses nested if statements, switch statements, and common string functions like strcpy(), strcmp(), strlen(), and strcat(). Examples are provided for each to demonstrate their syntax and usage. Key topics covered include nested conditions, switch expression and labels, using break, and comparing, copying, concatenating, and finding the length of strings.

Uploaded by

Neslobeng Suam
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)
185 views28 pages

3) Selection Control Structure (Part 2) PDF

This document provides an overview of selection control structures and string library functions in C++. It discusses nested if statements, switch statements, and common string functions like strcpy(), strcmp(), strlen(), and strcat(). Examples are provided for each to demonstrate their syntax and usage. Key topics covered include nested conditions, switch expression and labels, using break, and comparing, copying, concatenating, and finding the length of strings.

Uploaded by

Neslobeng Suam
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
You are on page 1/ 28

CSC128

FUNDAMENTALS OF
COMPUTER PROBLEM SOLVING

Topic 3:
Selection Control Structure (Part 2)
Objectives

 Nested selection
 Switch statement
 C-string functions
The Nested if

b) Non-Linear nested if statement

 Known as the nested if


 Occurs when a number of different conditions
need to be satisfied before a particular action
can occur
 Inside if, has one or more if
The Nested if

 Syntax:
if (condition1)
if (condition2)
Statement1
else
Statement2

if (x < 7)
if (x > 5)
cout << “x is 6”;
The Nested if
The Nested if

 Re-write:

if (x < 7)
{
if (x > 5)
cout << “x is 6”;
}
The Nested if

 Find the policy rate based on gender and age as


in the following table.

Gender Age Rate


< 21 0.05
Male
≥ 21 0.035
< 21 0.04
Female
≥ 21 0.03
The Nested if
Switch Structures

 switch statement is an alternative method for


multiple selections.

 It is often used to replace the nested if statement to


avoid confusion of deeply nested ifs.
Switch Structures
Switch Structures

 Syntax:
switch (expression)
{
case value1: statement1;
break;
case value2: statement2;
break;

case valueN: statementN;
break;
default: default statement; //optional
}
Switch Structures

switch (choice)
{
case 1 : do something;
break;
case 2 :
case 3 : do something;
do something;
break;
default: do something else;
}
Switch Structures

 Value of the switch expression matched with one of


the labels attached to a branch
switch (choice)
{
case 1 : do something;
break;
case 2 :
case 3 : do something;
do something;
break;
default: do something else;

}
 The statement(s) with the match get executed
Switch Structures

 Switch expression is the expression in


parentheses whose value determines which switch
label is selected
 cannot be floating point
 usually is int or char

 Identifiers following case must be constants


Switch Structures

switch (choice)
• break is typically used
{ between cases to avoid
case 1 : do something; fall through
break;
case 2 : • The default statement
case 3 : do something; is executed if the
do something; value of the switch
break; expression is NOT
default : do something; found among switch
} labels
// next statement
Switch Structures

Example :

cin >> grade


swith ( grade )
{
case 'A' : cout << "Score: 90-100";
break;
case 'B' : cout << "Score: 80-89";
break;
case 'C' : cout << "Score: 70-79";
break;
case 'D' : cout << "Score: 60-69";
break;
case 'E' : cout << "Score: 0-59";

default: cout << "Invalid grade was entered!";


}
Switch Structures
Switch Structures
String Library Function

 Use string predefined functions (string.h) and must be


included in the preprocessor directive command.
 eg : #include <string.h>

 Most commonly used string functions :-


 strcpy()
 To copy (to assign) a string into another string
variable.
 Syntax :- strcpy(destination array, source array);
 strcmp()
 To compare between two string variables.
 Syntax :- strcmp(s1, s2);
if s1=s2 then return value is 0
String Library Function (Cont…)

 strlen()

 Will return the length of string variable


 Syntax :- strlen(string variable);
 strcat()

 Will concatenate two string variables together.


 Syntax :- strcat(s1, s2);

More : Liang, Page 313


String Library Function
Example

#include <iostream.h>
#include <string.h>

int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
cout << "str1: "<< str1 << "\nstr2: " <<
str2 << "\nstr3: " <<str3;
return 0;
}
String Library Function

Example

#include <iostream.h>
#include <string.h>

int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strcat (str1, str2);
cout << str1;
return 0;
}
String Library Function
Example

#include <iostream.h>
#include <string.h>

int main()
{
char fruit_1[] = "apple";
char fruit_2[80];

cout<< “What is your favourite fruit? ";


cin.getline(fruit_2, 80);

if (strcmp (fruit_1,fruit_2) == 0)
cout<< "Same fruit!";
else
cout << "Different fruit!";
return 0;
}
String Library Function

Example

#include <iostream.h>
#include <string.h>

int main()
{
char name[40];

cout << "Enter a string:" << endl;


cin.getline(name,40);
cout << "Its length is: " << strlen(name) << "
characters\n";
return 0;
}
Test Yourself 1

 Display the song name based on user’s choice below.

Artis Song
[1 ] As Long As You Love Me
[A] Backstreet Boys
[2] I Want It That Way
[1] This I Promise You
[B] N SYNC
[2] Bye Bye Bye
Test Yourself 2

 Display the artist’s name based on user’s choice


below.
Choice Artist
1 Backstreet Boys
2 N SYNC
3 Boyzone
4 Spice Girls
Test Yourself 3

 Display the song based on artis’s name below.

Artist Song
Backstreet Boys Larger Than Life
N SYNC Bye Bye Bye
Boyzone Picture Of You
Thank You

END

You might also like