• About WordPress
    • WordPress.org
    • Documentation
    • Support
    • Feedback
  • Log In
  • Register
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment
Cuitutorial
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment

Programming Fundamental

Home » Blog » If Else statement in c++

If Else statement in c++

  • Posted by saqib
  • Categories Programming Fundamental
  • Date March 29, 2023
  • Comments 0 comment

If Else statement /conditional structure

—In everyday life, we are often making decision.

—“if the age of the person is above 60, then discount the ticket price to 40%”

—The above statement have element of decision making. E.g only if the age is above 60 reduce the ticket by 40%. If the criteria is not matched the ticket shall not be reduced by 40%.

—This decision making process is everywhere in our daily life.

—Everyday programming requires three control structures:

Control Structures are

  • Sequence
  • Selection
  • Iteration





Sequence

Executing instructions in sequence

Working “down the page”

C / C++ follow this sequence of control until some other is specified via return, if, while, do / while, etc.

Sequential Execution

–Statements executed one after the other in order in which they are written

Sequential Execution
Sequential Execution

Selection

—Conditional logic

—Choosing between alternative paths based on some predefined test

—Commonly implemented as “if” statements

—May also be implemented in other statements, such as “switch”, “? : “, or embedded within other types of statements such as “for”

—The statement used for decisions in C++ language is known as the ‘if statement ‘.

—If statement has a simple structure and it helps  to choose among alternative courses of actions.

Structure of if statement

if (condition)

         statement (or group of statements)

-The above statement means , if condition is true, then execute the statement or a group of statements.

-Here the condition is a statement which explains the condition on which a decision will be made.

-Example:  your grade and GPA

if ( the grade is greater than or equal to 60)

              pass the student

  • if selection structure

–Performs an indicated action when the condition is true.

if statement
if statement





—We write the condition in parentheses, followed by a statement of group of statements to be executed.

—We use braces { } to make a group (block) of a number of statements.

—We put ‘{‘ before the first statement and ‘}’ after the last statement.

if (condition)

{

statement;

statement;

.

.

statement;

}

// if statement is only executed when the value is true

Example: Comparing the ages of two students. If the age of first student is greater than second, then display “ student 1 is older”

main()

{

int age1, age2;

age1 = 12;

age2 = 10;

if (age1 > age2)

cout<<“Student1 is older:;

}

Relational Operators

Relational Operator       Meaning

==                              is equal to

<                                is less than

>                                is greater than

<=                              is less than or equal to

>=                             is greater than or equal to

!=                              is not equal to

if/else Structure

—if structure executes its block of statements only when the condition is true, otherwise the statement is skipped.

—The if/else structure allows the programmer to specify that a different block of statement(s) is to be executed when the condition is false.

  • if/else selection structure

–Performs an action when the condition is true and another when the condition is false.

if/else selection structure
if/else selection structure

if (condition)

{

statement(s)

}

else

{

statement (s)

}

Example

if  (age1 > age2)

{

cout<<“ student1 is older”;

}

else

{

cout<< “ student2 is older”;

}

 

—What if student1 and student2 are of the SAME age???

Use   >= operator

Q. Write a program in which users enter a number. If the user enter positive number, add one to the number else add two to the number.

Program

main()

{

int num, neg ,pos;

   cout<<“Please enter the no.”;

   cin>> num;

    if (num > 0)

    pos= pos +1;

    else

     neg= neg +2;

}

Conditional Operator

  • Closely related to if/else structure
  • ?: (Conditional Operator)
  • Only ternary operator in C

–Takes three operands

  • Operands with conditional operator form conditional expression

–Operand 1– condition

–Operand 2 – action if condition true

–Operand 3 – action if condition false

Conditional Operator

grade >= 60 ? “Passed”: “Failed”

Conditional Operator
Conditional Operator





Nested if Statements

—Used for multiple conditions and take some actions accordingly to each condition. Example, Grade, payroll etc.

—In this case we use NESTED IF statement.

if (condition 1 )

Statement1

else  if (condition 2 )

Statement2

…

else if (condition N )

  StatementN

else

  Statement N+1

EXACTLY 1 of these statements will be executed.

Example of grades (if only)

if (grade==‘A’)

cout<<“Excellent”;

if (grade==‘B’)

cout<<“Very Good”;

if (grade==‘C’)

cout<<“Good”;

if (grade==‘D’)

cout<<“poor”;

if (grade==‘F’)

cout<<“Fail”;

—‘if Statement” is computationally one of most expensive statements in program due to the fact that processor has to go through many cycles to execute them for a single decision.

—To avoid this , and alternate of multiple if statements can be use of if/else statements.

Nested if/else

//nested if/else

//printing remarks for each grade

if (grade == ‘A’)

cout<<“Excellent”;

else

if (grade == ‘B’)

cout<<“Very Good”;

else

if (grade == ‘C’)

cout<<“Good”;

else

if (grade == ‘D’)

cout<<“Poor”;

else

cout<<“Failed”;

 

//nested if/else

//printing grades of students

if (grade >= 90)

cout<<“A”;

else

if (grade >= 80)

cout<<“B”;

else

if (grade >= 70)

cout<<“C”;

else

if (grade >= 60)

cout<<“D”;

else

cout<<“Failed”;

 

//nested if/else

//printing grades of students

  if (grade >= 90)

cout<<“A”;

else if (grade >= 80)

cout<<“B”;

else if (grade >= 70)

cout<<“C”;

else if (grade >= 60)

cout<<“D”;

else

cout<<“Failed”;

Feature of Nested if/else

—In the ‘nested if statements’ the nested else is not executed if the first if condition is true and the control goes out of the if block.





 

  • Share:
author avatar
saqib

Previous post

Agile Software Development
March 29, 2023

Next post

Switch in c++
March 29, 2023

You may also like

Switch in c++
29 March, 2023

Switch statement in c++ Multiple- selection construct that is used in  multi decision  making in an efficient way and is easy to read and understand. Structure of Switch Statement switch (expression) {    case constant1:   group of statements 1; …

Data Types in C++
28 February, 2023

Data Types in C++ Unsigned Integers Signed Integers Floating point numbers Characters Logical values Standard Data Types in C++ Following are examples of predefined data types used in C++ int    an integer number (e.g. 10, -5). float    a …

Introduction to Programming Language
20 February, 2023

Introduction to Programming Language  Problem Solving Problem solving is a process of identifying a problem and finding the best solution for it. Problem solving is a skill that can be developed by following a well organized approach. We solve different …

Leave A Reply Cancel reply

You must be logged in to post a comment.

admin@cuitutorial.com
Facebook-f Twitter Youtube Linkedin Instagram Stack-overflow Pinterest Github Quora Whatsapp
Courses
  • All Courses
  • Past Paper
  • Final year projects
  • Interview Questions
  • Contact
Important Pages
  • Privacy Policy
  • Terms of Service
  • Cookie Policy
Links
  • University Events
  • Team
Education & learning platform for All Computer science subjects
Final year projects
Past Paper
Interview questions
Programming, C/C++, Asp.net/MVC. Android, MySql, Jquery, Ajax, javascript, Php, Html5, Bootstrap4.
NTS, GAT, PPSC, FPSC

Copyright © 2021 | Cuitutorial