Conditional and Looping Statements PDF

Document Details

SimplerPromethium3601

Uploaded by SimplerPromethium3601

National University of Science and Technology

Tags

C++ programming conditional statements looping statements programming

Summary

This document provides an introduction to conditional and looping statements in C++. It includes various examples, including if-else, nested if-else, and switch case statements. Flowcharts are also used to illustrate how each statement works.

Full Transcript

10/26/2024 #3 Conditional and Looping Statements Together Towards A Green Environment Aim To provide an understanding of Conditional and Looping statements in C++. Objectives To understand the concepts of if, if...

10/26/2024 #3 Conditional and Looping Statements Together Towards A Green Environment Aim To provide an understanding of Conditional and Looping statements in C++. Objectives To understand the concepts of if, if-else and switch To familiarize with syntax of the conditional statements To develop programs using conditional statements To understand the concept of loops To familiarize with the syntax of for, while and do- while loops To understand the difference in working between the loops Together Towards A Green Environment 1 10/26/2024 C++ provides two styles of flow control Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Together Towards A Green Environment Branching  Branching - the program chooses to follow one branch or another.  Many times, it is required to alter the flow of a sequence of instructions.  C++ language provides statements that can alter the flow of a sequence of instructions.  These statements are called Control Statements.  Branching Statements are of the following categories  if  if else  Nested if else  Switch case statements Together Towards A Green Environment 2 10/26/2024 if statement The if... statement is used if the programmer wants to execute codes when the test expression is true. Syntax if (test expression) { statement1 to be executed if test expression is true; } Together Towards A Green Environment Sample Program Write a C++ program to print the number entered by user only if the number entered is negative. Statement 1 Together Towards A Green Environment 3 10/26/2024 if else statement The if...else statement is used if the programmer wants to execute codes in statement1 when the test expression is true and execute codes in statement2 if the test expression is false. Syntax if (test expression) { statement1 to be executed if test expression is true; } else { statement2 to be executed if test expression is false; } Together Towards A Green Environment Sample Program Write a C++ program to compare two number and print the suitable message. Statement Statement 1 2 Together Towards A Green Environment 4 10/26/2024 Nested if else The nested if...else statement is used when a program requires more than one test expression. If the first test expression is true, it executes the codes inside the braces { } just below it. But if the first test expression is false, it checks the second test expression. Together Towards A Green Environment If the second test expression is true, it executes the codes in statement2 inside the braces{ } just below it. This process continues. If all the test expression are false, statements inside else is executed and the control of program jumps below the nested if...else. Together Towards A Green Environment 5 10/26/2024 Syntax of nested if else if (test expression1) { statement1to be executed if test expression1 is true; } else if (test expression2) { statement2 to be executed if test expression1 is false and 2 is true; } else if (test expression 3) { statement3 to be executed if text expression1 and 2 are false and 3 is true; } ….. else { statements to be executed if all test expressions are false; } Together Towards A Green Environment General Flowchart of nested if else Statement 1 Statement 2 Statement 3 Together Towards A Green Environment 6 10/26/2024 Code Sample Together Towards A Green Environment Sample – Algorithm, Flowchart & Code To check whether a number is positive or negative. Algorithm 1. Start 2. Print ”Enter a number”. 3. Read n. 4. If n>0 then print “The number is positive” go to step 7 5. else if n 0) cout

Use Quizgecko on...
Browser
Browser