0% found this document useful (0 votes)
2 views9 pages

Grade 10 Q4 Module 1 2 Conditional Loop

This module introduces learners to Conditional Statements and Loops in C++ programming. It covers the definitions, syntax, and examples of various conditional statements (if, if-else, if-else-if) and looping structures (for, while, do-while). The module includes practice exercises and assessments to reinforce understanding of these programming concepts.

Uploaded by

anatasiamaiden78
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)
2 views9 pages

Grade 10 Q4 Module 1 2 Conditional Loop

This module introduces learners to Conditional Statements and Loops in C++ programming. It covers the definitions, syntax, and examples of various conditional statements (if, if-else, if-else-if) and looping structures (for, while, do-while). The module includes practice exercises and assessments to reinforce understanding of these programming concepts.

Uploaded by

anatasiamaiden78
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/ 9

COMPUTER SCIENCE

Quarter 4 – Module 2:
Conditional and Looping Statements

Self-Learning Module
Learner’s Material
I Need to Know

In this module, you will be introduced to different learning activities preparing you
to be skillful before you take the plunge into the world of Programming.
This module is specifically crafted to focus on Programming Language C++
enriched with learning activities that will assess your level in terms of skills and knowledge.

After going through this module, you are expected to

1. define Conditional Statements and Loops,


2. understand the syntax of if, if-else and if-else ladder,
3. understand the syntax of for, while and do-while loops,
4. write a program using Conditional Statements and Loops.

I Will Check On This

True or False. Write T if the statement is correct and F if it is otherwise.

1. The computer has the capability to make decisions based on a given set of
conditions or choices.
2. In if-else statement, when the condition is proven true, statement 1 is executed,
otherwise statement 2 is executed.
3. The looping process will be repeated by the program all over again as long as the
condition remains false.
4. In the absence of else, there is nothing to be executed if the if conditional statement
is evaluated to false.
5. The else part can be included or not in C++ program, which means the program will
still run even without it.
6. The for loop provides specific locations to contain an initialization statement and an
increase/decrease statement.
7. With the use of if-else if-else conditional statement, the computer can decide to
choose one and only one of the given choices and its associated statement will be
executed.
8. The variable initialization specifies the first value where the loop starts.
9. In programming, the character data lowercase b is equivalent to uppercase B.
10. Iteration Structures or Loops are program instructions that repeat some statement
in a specified number of times.

2
Lesson
Conditional Statements and
1 Loops

Lesson Overview

Every procedural programming language has conditional statements and iterative


statements or commonly called loops. Within any program, you can define sections of
code that either repeat in loop or conditionally execute.

What are Conditional Statements?

Conditional Statements, also known as selection statements, are used to make


decisions based on a given condition. If the condition evaluates to True, a set of statements
is executed, otherwise another set of statements is executed.

The if Statement

The if statement selects and executes the statement(s) based on a given condition. If the
condition evaluates to True then a given set of statement(s) is executed. However, if the
condition evaluates to False, then the given set of statements is skipped.

Syntax of if statement

if (condition)
statement 1;

//This program determines if the input age is qualified to vote


//Sample if Conditional Statement
#include <iostream>
using namespace std;
int main()
{
int age; //variable declaration
cout<<"Enter Age:";
cin>>age;
if (age >=18) //condition
cout<<"You are qualified to vote!"; //statement
return 0;
}

3
The if-else Statement

The if-else statement causes one of the two possible statement( s) to execute,
depending upon the outcome of the condition. The if-else statement comprises two parts,
if and else. If the condition is True, the if part is executed. However, if the condition is
False, the else part is executed.

Syntax of if-else statement

if (condition)
statement 1;
else
statement 2;

/*This program determines if the input age is qualified to vote


or not. The qualifying age is 18 years old and above.*/
//Sample if-else Conditional Statement
#include <iostream>
using namespace std;
int main()
{
int age; //variable declaration
cout<<"Enter Age:";
cin>>age;
if (age >=18) //condition
cout<<"You are qualified to vote!"; /*this statement will be
executed if condition is
TRUE*/
else
cout<<"You are not qualified to vote!";/*this statement will be
executed if condition is
FALSE*/
return 0;
}

The if-else-if ladder

Also known as the if-else-if staircase, has an if-else statement within the outermost else
statement. Only those conditions that were first proven true, are the ones to be chosen
and its associated statement(s) will be executed.

Syntax of if-else statement

if (condition1)
statement 1;
else if (condition2)
statement 2;
else if (condition3)
statement 3;
else
statement4;
4
/*This program determines if the input letter is in uppercase or
lowercase. If a character other than letters is entered as input,
the program will display,”It is not an alphabet”.*/
//Sample if-else if-else Conditional Statement
#include <iostream>
using namespace std;
int main()
{
char ch; //assigned data type to variable ch is character
cout<<"Enter an alphabet:";
cin>>ch;
if( ch>='A' && ch<='Z') //condition1
cout<<"The alphabet is in upper case";/*this statement will be
executed if condition1 is
TRUE*/
else if ( ch>='a' && ch<='z' )//condition2
cout<<"The alphabet is in lower case";/*this statement will be
executed if condition2 is
TRUE*/
else
cout<<"It is not an alphabet";/*this statement will be executed if
condition is FALSE*/

return 0;
}

Nested if-else Statement

A nested if-else statement contains one or more if-else statements. The if else can be
nested in three different ways:

1. The if-else statement is nested within the if part.


2. The if-else statement is nested within the else part.
3. The if-else statement is nested within both the if and the else parts.

What are Iteration Structures or Loops?


Iteration Structures or Loops are program instructions that repeat some statement in a
specified number of times. It allows a set of instructions to be performed all over again
until a certain condition is reached, met, or proven and tested as false.

The while loop

The while loop allows programs to repeat a statement or series of statements, over and
over, as long as a certain test condition is true. While Loop is Known as Entry Controlled
Loop because in The while loop first we initialize the value of variable or Starting point of
Execution and then we check the condition and if the condition is true then it will execute
the statements and then after it increments or decrements the value of a variable.

5
Syntax of while loop

initialization;
while (condition)
{
statements;
inc/dec;
{

//This program generates the sequence nos. 12345 using while loop
#include <iostream>
using namespace std;
int main()
{
int n; //variable declaration
n=1;//initialization
while (n<=5)//condition
{
cout <<n; //statement
n++;//incrementation
}
return 0;
}

The for loop

The for loop allows programs to repeat a statement while condition remains true. In This
loop all the basic operations like initialization, condition checking and incrementing or
decrementing all these are performed in only one line. This is similar to the while loop for
performing its execution but only different in its syntax.

Syntax of for loop

for (initialization; condition; inc/dec)


{
statements;
}

//This program generates the sequence nos. 12345 using for loop
#include <iostream>
using namespace std;
int main()
{
int n; //variable declaration
for (n=1; n<=5; n++) //initialization, condition, incrementation
{
cout << n; //statement
}
return 0;
}
6
The do-while loop

The do-while loop is similar to the while loop, except that the test condition occurs at the
end of the loop. Having the test condition at the end, guarantees that the body of the loop
always executes at least one time. This is Also Called as Exit Controlled Loop.

Syntax of do-while loop

initialization;
do
{
statements;
inc/dec;
}
while (condition);

//This program generates the sequence nos. 12345 using do-while


loop
#include <iostream>
using namespace std;
int main()
{
int n; //variable declaration
n=1; //initialization
do
{
cout <<n; //statement
n++; //incrementation
}
while (n<=5); //condition
return 0;
}

I Will Do This

Give the syntax of the following conditional statements and loops.


1. Simple if statement
2. else-if statement
3. if-else if-else statement
4. for Loop
5. while Loop
6. do-while Loop

7
I Learned This

I have learned that the computer has the capability to make decisions based on a
given set of conditions and choices. This can be done through the application of conditional
statements in our program. Conditional statements are ___________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________.
Every Conditional Statement has its own syntax which ______________________
______________________________________________________________________
______________________________________________________________________.

I Practice This

Practice coding the given sample programs as shown above (Conditional Statements and
Loops). You may access this link https://www.onlinegdb.com/online_c++_compiler to write
and run your program to see the result/output.

I Apply This

1. Write a program to display the high school level of a student, based on its year-
entry. For example, the year-entry 1 means the student is a freshman, 2 for
sophomore, and so on. Here are the given criteria:

Year-entry number High School Level

1 Freshman
2 Sophomore
3 Junior
4 Senior

Other entry no. “Out of School”

8
2. Write a program that generates the given sequence numbers:

54321

Post Test

True or False. Write T if the statement is correct and F if it is otherwise.

1. The Looping statement is a program instruction that repeats some statement or


sequence of statements in a specified number of times.
2. If the condition is evaluated to false, then all statements within the body of the loop are
executed.
3. The variable initialization specifies the first value where the loop starts.
4. The looping process will be repeated by the program all over again as long as the
condition remains false.

(For nos. 5-10) Refer to this sample program:

#include <iostream>
using namespace std;
int main ( )
{
int n;
for (n=5; n>=1; n--)
{
cout << n ;
}
}

5. From the sample program shown above, the variable n initialize with n>=1.
6. The program evaluates the condition: n=5.
7. Since n has a value of 5, the condition is evaluated to true, therefore all the associated
statements within the body of the loop are executed.
8. After executing the statement, the program loops again and will execute the
decrementation, thus, the value of n is decreased by 1.
9. When the value of n becomes 4, the looping process will be terminated.
10. The output of this program is 1 2 3 4 5.

REFERENCES

Introduction to Turbo C Programming by Copernicus P. Pepito Pages 19-33, 51-71

http://www.cs.cornell.edu/courses/cs1130/2012sp/1130selfpaced/module2/module2part1/ifloop.html

https://ecomputernotes.com/cpp/control_structure/conditional-statements

You might also like