0% found this document useful (0 votes)
13 views

R1 Control Structures in C

The document discusses control structures in C++ programming. It provides 5 questions for research on the topics of if/if-else/if-else-if statements, switch cases, loops (while, for, do-while), break and continue statements, and conditional operators. Sample answers are given in a question and response format, with an original response from Google and a revised simpler answer by the student. The questions cover the basic types of control structures, differences between if and switch statements, concepts and differences of loop types, and the purpose of break and continue statements.

Uploaded by

cjerondo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

R1 Control Structures in C

The document discusses control structures in C++ programming. It provides 5 questions for research on the topics of if/if-else/if-else-if statements, switch cases, loops (while, for, do-while), break and continue statements, and conditional operators. Sample answers are given in a question and response format, with an original response from Google and a revised simpler answer by the student. The questions cover the basic types of control structures, differences between if and switch statements, concepts and differences of loop types, and the purpose of break and continue statements.

Uploaded by

cjerondo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Student Name : Carl Justin G.

Erondo Date : Sept 13, 2023


Student Number : Extra Points :
Section/Block : BS ECE 2-F Rating :

RESEARCH #1: CONTROL STRUCTURES IN C++


If | if-else | if-else-if | switch case

INSTRUCTION: Conduct research on the topic of Control Structures in c++ programming by exploring the
following questions (need a hard copy for submission on September 11, 2023):

1. What are the three primary types of control structures in C++? Provide a brief explanation of each
type.
2. How does the "if" statement differ from the "switch" statement in C++ when it comes to making
decisions in your code? Provide examples for both.
3. Explain the concept of a loop in C++. What are the differences between "while," "for," and "do-
while" loops? When would you use each type of loop?
4. In C++, what is the purpose of the "break" and "continue" statements within loops? Provide
examples to illustrate their usage.
5. Describe the role of conditional operators (e.g., "? :" ternary operator) in control structures in C++.
How do they work, and what are some common use cases for them? Provide examples to
demonstrate their functionality.

NOTE: If you want to answer the questions using google or any tool, then copy and paste the original statement and
write the rephrased answer made by you. See format below; (this will be check according to the content of your research
paper)

Answer from Google (Sample Only)


Question: What is a computer hardware?
Answer: Computer hardware is a collective term used to describe any of the physical components of an analog
or digital computer. The term hardware distinguishes the tangible aspects of a computing device from software,
which consists of written, machine-readable instructions or programs that tell physical components what to do
and when to execute the instructions. Hardware and software are complementary. A computing device can
function efficiently and produce useful output only when both hardware and software work together
appropriately. Computer hardware can be categorized as being either internal or external components.
Generally, internal hardware components are those necessary for the proper functioning of the computer, while
external hardware components are attached to the computer to add or enhance functionality.

Link: https://www.techtarget.com/searchnetworking/definition/hardware

Revised Answer:
Computer hardware refers to all the physical parts of a computer, whether it's a regular or digital one. It's what
you can touch and see, like the screen, keyboard, or the computer's brain, the CPU. Hardware is different from
software, which is the computer's instructions that tell these physical parts what to do. Hardware and software
need to work together for a computer to be useful. It's like having a car (the hardware) and knowing how to
drive it (the software) to go where you want. Computer hardware can be divided into two main groups: stuff
inside the computer (like the CPU, memory, and storage) that make it work, and things you can plug into it from
the outside (like a printer or a mouse) to add more features or abilities.

PROGRAMMING 1 | PROG1
Copy and paste the table below to follow the format to answer the questions.

Answer from Google


Question#1: What are the three primary types of control structures in C++? Provide a brief
explanation of each type.
Answer:

1. Sequence Structure

This is the most simple and basic form of a control structure. It is simply the plain logic we write; it
only has simple linear instructions, no decision making, and no loop. The programs we all wrote at the
start of our programming journey were this control structure only. It executes linearly line by line in a
straight line manner.

2. Selection Structure

Sometimes in our program, we will need to write certain condition checks that this part of code should
only execute if a particular condition meets or another part of code should run.

3. Loop Structure

Suppose we are asked to write a program that prints "Hello World" once. Now, we are asked to write a program
that will print "Hello World" 10 times; But this seems like, a lot of repetition, but we can manage it somehow.
Now, what if we have to print it 100 or, let's say, 1000 times? Are we going to copy-paste the same line 1000
times? No, that will not be feasible. There comes the use of loop control structures

Link: https://www.scaler.com/topics/control-structure-in-cpp/

Revised Answer:
In programming, there are three fundamental control structures: the Sequence Structure,
which involves executing instructions sequentially without decision-making or looping; the
Selection Structure, which enables conditional execution of code based on specific conditions;
and the Loop Structure, which automates repetitive tasks by executing code iteratively for a
specified number of times or until certain conditions are met, enhancing code efficiency and
maintainability.

Answer from Google


Question#2: How does the "if" statement differ from the "switch" statement in C++ when it comes to
making decisions in your code? Provide examples for both.
Answer:

In our day-to-day lives, we always decide on things by using if-else. For example, let's
consider a situation where we need to think about the number of days present in each
month. If the month is either of January, March, May, July, August, October, and
December, the answer is 31. If the month is either April, June, September, and November,

PROGRAMMING 1 | PROG1
the answer is 30. If the month is a leap year, February, the answer is 29. If not a leap year
February, the answer is 28. The if-else statement allows the programmer to do exactly that
with their code. A condition check is done. If it is true, control goes to one block of code,
and if it isn’t, then control goes to a different block of code defined in else. The else
statement can either be a single statement, or it can even be a block of statements. In
the switch statement, we compare the condition value with multiple cases. When
there is a match with any one of the cases, the block of code corresponding with
that case is executed. Each case has a name or a number, which is known as its
identifier. If none of the cases matches the condition, the block of code
corresponding to the default case is executed. The same example of finding
number of days in each month is done using the switch below.

Link: Difference Between if else and Switch - Scaler Topics

Revised Answer:
The if-else statement in programming allows us to implement this logic in our code. It checks a
condition - if the condition is true, one block of code is executed; if it’s false, another block of code
(defined in the else statement) is executed. The else statement can be a single statement or a block of
statements.

The switch statement offers another way to handle multiple conditions. It compares the condition
value with multiple cases. When there’s a match with any case, the corresponding block of code is
executed. Each case has an identifier, which can be a name or a number. If none of the cases match
the condition, the code block corresponding to the default case is executed.

For instance, we can use a switch statement to determine the number of days in each month.

Answer from Google


Question#3: Explain the concept of a loop in C++. What are the differences between "while," "for,"
and "do-while" loops? When would you use each type of loop?
Answer:

In Programming, sometimes there is a need to perform some operation more than once or (say) n
number of times. Loops come into use when we need to repeatedly execute a block of statements.
Manually we have to write cout for the C++ statement 10 times. In Loop, the statement needs to be
written only once and the loop will be executed 10 times as shown below. In computer programming,
a loop is a sequence of instructions that is repeated until a certain condition is reached. There are
three types of loops which is For, While, and Do-While. A For loop is a repetition control structure
that allows us to write a loop that is executed a specific number of times. The loop enables us to
perform n number of steps together in one line. While Loop executes a statement repeatedly, until the
value of condition becomes false. The test takes place before each iteration. In Do-while loops also
the loop execution is terminated on the basis of test conditions. The main difference between a do-
while loop and the while loop is in the do-while loop the condition is tested at the end of the loop
body, i.e do-while loop is exit controlled whereas the other two loops are entry-controlled loops.

PROGRAMMING 1 | PROG1
Note: In a do-while loop, the loop body will execute at least once irrespective of the test condition.

Link: https://www.geeksforgeeks.org/cpp-loops/

Revised Answer:
Loops serve as valuable constructs that facilitate the execution of repetitive tasks without the
need for redundant code. These loops come in three primary forms: For loops, which allow us
to perform actions a specified number of times; While loops, which execute actions as long as
a given condition remains true; and Do-While loops, ensuring that a particular task is carried
out at least once, followed by a condition check to determine if it should continue. Loops
enhance programming efficiency by automating repetitive operations and eliminating the
need for redundant code, thereby streamlining the coding process.

Answer from Google


Question#4: In C++, what is the purpose of the "break" and "continue" statements within loops?
Provide examples to illustrate their usage.
Answer:
Break and continue are known as jump statements because they are generally used to change or
manipulate the regular flow of the program, loops, etc. when a particular condition is met.

The break statement is used to terminate the execution of the current loop. Whenever there is a need
to end the loop, you need to add the break statement. Once the break statement is met, all the
iterations are stopped, and the control is shifted outside the loop.

The continue statement is used to move to the next iteration, it is also used for termination, but
unlike break, it is not used to terminate the entire execution of the loop but only the current iteration
of the loop.

PROGRAMMING 1 | PROG1
Link: https://www.simplilearn.com/tutorials/cpp-tutorial/break-and-continue-statements-cpp

Revised Answer:
In programming, we use "break" and "continue" to change how our code behaves in certain
situations. "Break" helps us stop a loop completely when a condition is met, while "continue" lets us
skip just one part of the loop and move to the next one. They're like special commands that help us
control our code's flow.

Answer from Google


Question#5: Describe the role of conditional operators (e.g., "? :" ternary operator) in control
structures in C++. How do they work, and what are some common use cases for them? Provide
examples to demonstrate their functionality.
Answer:
Arithmetic operators
These operations are used to perform various mathematical operations on ‘integer’, ‘float’ and
‘double’ data types. Functions of all the operators are straightforward except for ‘++’ and ‘–’, which
are used to increment or decrement the value of the variable by ‘1’ respectively. These operators can
be used as ‘i++ or i–’ and ‘++i or –i’. The ‘++i’ operation indicates that ‘increase the value of by ‘1’ and
then use that value; whereas ‘i++’ indicates that ‘use the value of i first’ and then increment it by 1

#include<iostream>
using namespace std;

int main() {
int a = 10;
int b = 20;

cout << "a + b = " << a + b << endl; // Addition


cout << "a - b = " << a - b << endl; // Subtraction
cout << "a * b = " << a * b << endl; // Multiplication

PROGRAMMING 1 | PROG1
cout << "b / a = " << b / a << endl; // Division
cout << "b % a = " << b % a << endl; // Modulus

a++; // Increment operator


cout << "a++ = " << a << endl;

b--; // Decrement operator


cout << "b-- = " << b << endl;

return 0;
}

Relational operators
Relations operators are used of checking various equality conditions e.g ‘greater than’, ‘equal’ or ‘less
than’ etc.

#include<iostream>
using namespace std;

int main() {
int a = 10;
int b = 20;

// Relational operators
cout << "a > b: " << (a > b) << endl; // Greater than
cout << "a < b: " << (a < b) << endl; // Less than
cout << "a >= b: " << (a >= b) << endl; // Greater than or equal to
cout << "a <= b: " << (a <= b) << endl; // Less than or equal to
cout << "a == b: " << (a == b) << endl; // Equal to
cout << "a != b: " << (a != b) << endl; // Not equal to

return 0;
}
Bitwise operators
Bitwise operators are used with boolean data type to calculate the results of boolean expressions or
to perform shift operations on boolean data.

#include<iostream>
using namespace std;

int main() {
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101

int result;

// Bitwise AND operator


result = a & b; // 12 = 0000 1100
cout << "a & b: " << result << endl;

// Bitwise OR operator
result = a | b; // 61 = 0011 1101
cout << "a | b: " << result << endl;

PROGRAMMING 1 | PROG1
// Bitwise XOR operator
result = a ^ b; // 49 = 0011 0001
cout << "a ^ b: " << result << endl;

// Bitwise NOT operator


result = ~a; // -61 = 1100 0011
cout << "~a: " << result << endl;

// Bitwise left shift operator


result = a << 2; // 240 = 1111 0000
cout << "a << 2: " << result << endl;

// Bitwise right shift operator


result = a >> 2; //15 = 0000 1111
cout << "a >> 2: " << result << endl;

return 0;
}
Logical operators
Logical operators are used to perform logic ‘and’, ‘or’ and ‘not’ operations, which are shown in Table
3.4. Unlike, ‘bitwise logic operators’ these operators do not check the second operator if the first
operator gives the result e.g. in ‘0 && 1 = 0’, the result does not depend on the value of second
operand i.e. ‘1’; because first operand is ‘0’, therefore the result will be zero, whether the second
operand is ‘0’ or ‘1’.

#include<iostream>
using namespace std;

int main() {
bool a = true;
bool b = false;

// Logical AND operator


cout << "(a && b): " << (a && b) << endl;

// Logical OR operator
cout << "(a || b): " << (a || b) << endl;

// Logical NOT operator


cout << "(!a): " << (!a) << endl;
cout << "(!b): " << (!b) << endl;

return 0;
}

Conditional operator
In conditional operator, the condition is specified before ‘?’ and the ‘true and false’ results
are seperated by ‘:’. For example, in this listing, condition ‘(a %2)== 0)’ is specified before ‘?’
at Line 14. Note that, the result before ‘:’ (i.e. even) is assigned to variable ‘result’ if the
condition is true, otherwise the result after ‘:’ (i.e. odd) will be assigned to variable ‘result’.
Finally, Line 16 will display the message for even or odd number.

PROGRAMMING 1 | PROG1
#include<iostream>
using namespace std;

int main() {
int a = 10;
int b = 20;

// Conditional operator
string result = (a > b) ? "a is greater than b" : "a is not greater than b";
cout << result << endl;

return 0;
}

Link: https://cppguide.readthedocs.io/en/latest/cpp/control.html

Revised Answer:
Arithmetic operators are used for mathematical operations on integer, float, and double data types.
The '++' and '--' operators increment or decrement a variable's value by 1. The '++i' operation
increments the value first, then uses it, while 'i++' uses the value first, then increments it. Relational
operators check equality conditions such as 'greater than', 'equal to', or 'less than'. Bitwise operators
work with boolean data types to calculate boolean expressions or perform shift operations. Logical
operators perform logical 'and', 'or', and 'not' operations. They don't check the second operand if the
first operand determines the result. For example, in '0 && 1 = 0', the result is 0 regardless of the
second operand's value. Lastly, Conditional operator specifies a condition before '?'. The results for
'true' and 'false' are separated by ':'. For instance, in '(a %2)== 0)', if the condition is true, the result
before ':' is assigned to a variable; otherwise, the result after ':' is assigned.

PROGRAMMING 1 | PROG1

You might also like