0% found this document useful (0 votes)
117 views6 pages

C++ Nested If Program Example

The document provides instructions for a programming assignment asking students to write a program using nested if statements that takes user input for gender and age or year level and status and outputs the corresponding category. It includes a sample C++ program code using nested ifs to categorize based on gender and age. The instructions provide sample output formats and require students to write their own code, run it, and provide two sample outputs, including one invalid combination.

Uploaded by

ai tsuki
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views6 pages

C++ Nested If Program Example

The document provides instructions for a programming assignment asking students to write a program using nested if statements that takes user input for gender and age or year level and status and outputs the corresponding category. It includes a sample C++ program code using nested ifs to categorize based on gender and age. The instructions provide sample output formats and require students to write their own code, run it, and provide two sample outputs, including one invalid combination.

Uploaded by

ai tsuki
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES

QUEZON CITY
COLLEGE OF INFORMATION TECHNOLOGY EDUCATION (CITE)
ITE001- Computer Programming 1

SAMPLE PROGRAM ONLY : Selection Control Structure using Nested If condition ( NOT GRADED)

INSTRUCTION: Read and study the assignment below. Solve the problem using C++ programming
language, compile, run and screen shot the correct output. Copy and paste the source code or program
code and the required sample output (screen shot) in the format below. The output should also display the
school name, course name, student name, program, section, date and title of assessment task.

PROBLEM: Write a program that will let the user input year level and status base on the table below. The output
should be based on the possible combination of year level and status.

GENDER AGE
INPUT OUTPUT INPUT OUTPUT
F or f Female 1 to 17 Minor
M or m Male 18 to 59 Adult
Other 60 and
Invalid Senior
input above
Others Invalid

//Sample if , if-else and nested if


#include<iostream>
using namespace std;
main()
{ int age; char gender;
cout<<("*** USING NESTED IF STATEMENT ****");
cout<<("\nEnter your gender : "); cin>> gender;
cout<<("\nEnter your age : "); cin>> age;
if (gender == 'F' || gender == 'f')
{ if ( age >=1 && age <=17)
cout<<" \nFemale & Minor";
else if ( age >=18 && age <=59)
cout<<" \nFemale & adult";
else if (age >= 60)
cout<<"\nFemale & Senior";
else cout<<"Female & Invalid";
}

else if (gender == 'M' || gender == 'm')


{ if ( age >=1 && age <=17)
cout<<" \nMale & Minor";
else if ( age >=18 && age <=59)
cout<<" \nMale & adult";
else if (age >= 60)
cout<<" \nMale & Senior";
else cout<<"Male & Invalid";
}

else
{ if ( age >=1 && age <=17)
cout<<" \nInvalid & Minor";
else if ( age >=18 && age <=59)
cout<<" \nInvalid & adult";
else if (age >= 60)
cout<<" \nInvalid & Senior";
else
cout<<" \nInvalid & Invalid";
}
}
TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES
QUEZON CITY
COLLEGE OF INFORMATION TECHNOLOGY EDUCATION (CITE)
ITE001- Computer Programming 1
NAME: CANDELARIO, Lovely B.
PROGRAM/SECTION: IT11S2-A21
ASSESSMENT TASK: Selection Control Structure using Nested If () Condition (GRADED)

The following question supports the attainment of Course Intended Learning Outcomes (CILO): Design
computing based solution using control structures, functions, array and other statements.

INSTRUCTION: Read and study the assignment below. Solve the problem using C++ programming
language, compile, run and screen shot the correct output. Copy and paste the source code or program
code and the required sample output (screen shot) in the format below. The output should also display the
school name, course name, student name, program, section, date and title of assessment task.

PROBLEM: Write a program that will let the user input year level and status based on the table below. The output
should be based on the possible combination of year level and status.

YEAR LEVEL STATUS


INPUT OUTPUT INPUT OUTPUT
1 First Year R or r Regular
2 Second Year I or i Irregular
3 Third Year other letters Invalid
4 Fourth Year
other
Invalid
number

Sample Output(1):
Name : Juan Cruz
Section: IT11S1
Enter Year Level : 1
Enter Status :r
The student is First Year and Regular

Enter Year Level : 1


Enter Status :R
The student is First Year and Regular
Enter Year Level : 4
Enter Status :s
The student is Fourth Year and Invalid
ANSWER:

1. SOURCE CODE: Write or paste the program code below

#include<iostream>

using namespace std;

main()

{ int year; char status;

cout<<("Name: Lovely B. Candelario");

cout<<("\nSection: IT11S2-A21");

cout<<("\nEnter your Year Level: "); cin>> year;

cout<<("Enter your Status: "); cin>> status;

if (status == 'R' || status == 'r')

if ( year == 1)

cout<<" \nThe student is First Year and Regular";

else if ( year == 2)

cout<<" \nThe student is Second Year and Regular";

else if ( year == 3)

cout<<"\nThe student is Third Year and Regular";

else if ( year == 4)

cout<<"\nThe student is Fourth Year and Regular";

else cout<<"Invalid";

else if (status == 'I' || status == 'i')

if ( year == 1)
cout<<" \nThe student is First Year and Irregular";

else if ( year == 2)

cout<<" \nThe student is Second Year and Irregular";

else if ( year == 3)

cout<<"\nThe student is Third Year and Irregular";

else if ( year ==4)

cout<<"\nThe student is Fourth Year and Irregular";

else cout<<"Invalid";

else {

if ( year == 1)

cout<<" \nThe student is First Year and Invalid";

else if ( year == 2)

cout<<" \nThe student is Second Year and Invalid";

else if ( year == 3)

cout<<"\nThe student is Third Year and Invalid";

else if ( year ==4)

cout<<"\nThe student is Fourth Year and Invalid";

else cout<<"Invalid";

2. OUTPUT: Show two(2) sample output - one combination of year level and status and one show
invalid combination.
Honor Pledge
“I affirm that I have not given or received any unauthorized help on this
assignment and that this work is my own.”

Common questions

Powered by AI

Control structures like if-else and nested if fundamentally contribute to achieving intended learning outcomes by training students in implementing logical decision-making processes and understanding algorithms' flow . They allow students to design computing solutions that replicate real-world processes, aiding in grasping complex concepts such as flow of control, conditional logic, and problem-solving strategies, thus equipping them with skills crucial for advanced programming and software development tasks .

Incorporating additional layers of complexity, such as integrating functions or arrays, could enhance educational value by fostering understanding of modular programming and data handling beyond basic conditionals . Introducing scenarios with potential exceptions, requiring error handling strategies, would also prepare students for real-world challenges. Offering opportunities to explore alternative control structures like switch-case statements could diversify students' toolkits, promoting flexibility and creativity in approaching programming challenges.

The main challenge with nested if-statements in complex data validation is managing and understanding the flow of logic, especially as the conditions increase, which can lead to confusion and errors. This can result in inefficient code if not managed properly, as seen in handling gender and age in complex categorizations . Additionally, deeply nested structures can make the code harder to read and maintain, which is less of a concern in cases involving explicitly defined relationships between limited inputs, such as year level and status .

Nested if-statements allow for more detailed categorization, accommodating complex logic like multiple checks for each gender and age group, thus making the program more flexible to handle various valid and invalid inputs . However, this complexity can also increase the risk of errors due to the potential for missing a condition. In the year level and status problem, the use of nested if-statements is straightforward due to the limited set of inputs and predefined categories, reducing the likelihood of errors and simplifying error checking .

Enhancing user experience involves providing clear feedback and instructions, ensuring the program communicates effectively, which is evident in multiple user prompts and resulting outputs stating clear categorizations, such as 'Female & Adult' or 'The student is First Year and Regular' . Efficient input validation that anticipates possible user errors and provides helpful messages (e.g., 'Invalid') can also reduce user frustration and guide correct usage, ensuring a seamless interaction. Using clear structure and intuitive logic aids in reducing errors and improving the overall flow, maintaining responsiveness and reliability in user experience.

Including specific school and student information helps contextualize the problem-solving process within the educational environment, ensuring accountability and personal engagement in each assignment. This practice supports tracking individual progress and maintaining academic integrity by associating outputs directly with the student, promoting a sense of ownership while highlighting the application of programming skills to real-world-like scenarios .

The use of char data types allows for concise representation of gender and status information with single character inputs ('F', 'M', 'R', 'I'), which facilitates straightforward comparisons in the conditional statements . Similarly, int data types are well-suited for numerical values used in ages and year levels, enabling easy comparisons through relational operations. These appropriate choices ensure efficient memory usage and fast computations, contributing to the overall performance and reliability of the programs.

Handling gender and age data requires checking multiple conditions to categorize individuals into 'Female & Minor', 'Male & Adult', etc., using nested if-statements based on age ranges and gender inputs . In contrast, handling year level and status data involves mapping specific inputs (such as year level and status) to fixed outputs (e.g., 'First Year and Regular'), which is a simpler task with fewer conditions because the data inputs are predefined rather than open to a range of possibilities .

Specific input-output mapping simplifies programming tasks by providing clear guidance on the expected results for given inputs, reducing ambiguity and potential debugging time. In educational contexts, it allows students to focus on implementing structured logical sequences rather than dealing with unanticipated errors, thus strengthening their foundational understanding by working within predefined boundaries . This approach ensures learning outcomes align with instructional goals.

Using nested if-statements can be advantageous because it introduces students to complex decision-making processes and the importance of order and structure in coding, promoting logical thinking and problem-solving skills . However, it can also be disadvantageous if students become overwhelmed by the complexity or if they rely too heavily on this structure for scenarios where simpler techniques could be used. Understanding when and how to apply nested if-statements efficiently is crucial for deeper programming proficiency.

You might also like