Module 2 CSC 201
Module 2 CSC 201
Control Structures
Objectives
By the end of this module, you will be able to:
explain the purpose of C++ keywords
understand and use datatypes
know how to make variable declaration and
initialization
identify valid identifiers and variable naming systems
use and know the reasons for global and local
variables
understand program flow and control structures
implement basic Input /Output (I/O)
MODULE – 2: Variable Use, Basic Input/Output,
Control Structures (General Review)
are significant.
Rules for all Identifiers…
1. SEQUENCING:
By natural/default construct , a program proceeds
24 is stored in x
2.4 Basic Control Structures
2. SELECTION:
This is a program control construct/structure used to alter the
natural/ sequencing construct. One of the common statement
used to selectively execute a particular part of a program
based on the programmer’s choice is the if selection
statement.
if(condition) statement;
• If is the keyword
• (condition) is a boolean expression which results into
true/false
• The statement that follows immediately is executed if the
condition is true else it is skipped without being executed,
then the condition is terminated.
If Statement Example:
float score;
cin >> score;
If(score > 79)
{
Cout<< ”\nExcellent
Student”;
}
If else statement Example:
expr
statemen true false
essi
t1
on
statemen statemen
t2 t3
Nested if Example :
Nested IF If Age < 16 years deny scholarship
Else if sex = male grant scholarship in
African Countries
Else grant scholarship in African and
American Countries
If else if else statements : compound if
Input
option
int group;
cin >> group; // read in group type from keyboard
Switch(group){
case 1: cout << “This is SS1 class\n”;
break;
case 2: cout << “This is SS2 class\n”;
break; //what if the break statement is
omitted here?
case 3: cout << “This is SS3 class\n”;
break;
default: cout << “This is not a group\n”;
} // end of switch statement
Switch Statement
while loop
do…while loop
Test true
Initial modific
conditi
value ation
on
Action
false stateme
nt
for loop Implementation Example:
expression1
HI LE
W False (ENDWHILE)
testing
condition
True (DO)
action statement output
(s) (optional
)
output
While loop constructs:
double mark;
mark = 10;
while(mark > 0)
{
cout << “Printing as long as mark has not reached
zero\n”;
mark- -; // decrement the value of mark by one after
each loop
}
2. do…while structure
Action
Output
Test true
conditio
n
false
2. do{ … }while(condition);
Here, there is at least one student in a class for the course to exist.
END OF MODULE ASSESSMENT (EMA)