0% found this document useful (0 votes)
23 views33 pages

Programming Lecture by Bashir

The document discusses C++ input and output, conditional statements, logical operators, and repetitive statements. It provides code examples and explanations of cout, cin, if/else statements, logical operators like AND and OR, and loops like for, while, and do-while.

Uploaded by

mrsfahad550
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
23 views33 pages

Programming Lecture by Bashir

The document discusses C++ input and output, conditional statements, logical operators, and repetitive statements. It provides code examples and explanations of cout, cin, if/else statements, logical operators like AND and OR, and loops like for, while, and do-while.

Uploaded by

mrsfahad550
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 33

C++ Basics

Lec# 5
C++ Basic Input/Output

 C++ Output
#include <iostream>
using namespace std;
int main()
{
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}
How Does a program work

•We first include the iostream header file that allows us to display output.
•The cout object is defined inside the std namespace.
•To use the std namespace, we used the using namespace std; statement.
•Every C++ program starts with the main() function.
• The code execution begins from the start of the main() function.
•cout is an object that prints the string inside quotation marks " ". It is followed by the << operator.
•return 0; is the "exit status" of the main() function. The program ends with this statement, however, this statement is not
mandatory.
C++ Input

#include <iostream>
using namespace std;
int main()
{ int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}
C++ Taking Multiple Inputs

 #include <iostream>
 using namespace std;
 int main()
 { char a; int num;
 cout << "Enter a character and an integer: ";
 cin >> a >> num;
 cout << "Character: " << a << endl;
 cout << "Number: " << num;
 return 0;
 }
Comparison operators
 Comparison operators are binary operators that test a condition and return 1
if that condition is logically true and 0 if that condition is false . expressions
that both have real type or both have pointer to object type
logical operators

 In C++ programming languages, logical operators are symbols that allow you
to combine or modify conditions to make logical evaluations. They are used to
perform logical operations on boolean values (true or false).
 Logical AND ( && ) Operator
 Logical OR ( || ) Operator
 Logical NOT ( ! ) Operator
Logical AND Operator ( && )

 TheC++ logical AND operator (&&) is a binary


operator that returns true if both of its operands are true.
Otherwise, it returns false. Here’s the truth table for the
AND operator:
Operand 1 Operand 2 Result

true true true

true false false

false true false

false false false


Logical OR Operator ( || )

 The C++ logical OR operator ( || ) is a binary operator that


returns true if at least one of its operands is true. It returns false
only when both operands are false. Here’s the truth table for the
OR operator:
Operand 1 Operand 2 Result

true true true

true false true

false true true

false false false


Logical NOT Operator ( ! )

 The C++ logical NOT operator ( ! ) is a unary operator that is used to negate
the value of a condition. It returns true if the condition is false, and false if
the condition is true. Here’s the truth table for the NOT operator:
Operand 1 Result

true false

false true
conditional statements

 The conditional statements (also known as decision control structures) such


as if, if else, switch, etc. are used for decision-making purposes in C/C++
programs.
 They are also known as Decision-Making Statements and are used to evaluate
one or more conditions and make the decision whether to execute a set of
statements or not. These decision-making statements in programming
languages decide the direction of the flow of program execution.
if statement

 The if statement is the most simple decision-making statement. It is used to


decide whether a certain statement or block of statements will be executed
or not i.e if a certain condition is true then a block of statements is executed
otherwise not.
 Syntax of if Statement
 If(condition)
 {
 //statement to execute if
 // condition is true
 }
Condition of IF Statement

 Here, the condition after evaluation will be either true or false. C if statement
accepts boolean values – if the value is true then it will execute the block of
statements below it otherwise not. If we do not provide the curly braces ‘{‘ and
‘}’ after if(condition) then by default if statement will consider the first
immediately below statement to be inside its block.
// C program to illustrate If statement
#include <stdio.h>

int main()
{
int i = 10;

if (i > 15) {
printf("10 is greater than 15");
}

printf("I am Not in if");


}
if-else

 The if statement alone tells us that if a condition is true it will execute a


block of statements and if the condition is false it won’t. But what if we want
to do something else when the condition is false? Here comes the
C else statement. We can use the else statement with the if statement to
execute a block of code when the condition is false. The if-else statement
consists of two blocks, one for false expression and one for true expression.
// C program to illustrate If statement
#include <stdio.h>

int main()
{
int i = 20;

if (i < 15) {

printf("i is smaller than 15");


}
else {

printf("i is greater than 15");


}
return 0;
}
Nested if-else

 A nested if in C is an if statement that is the target of another if statement.


Nested if statements mean an if statement inside another if statement. Yes,
both C and C++ allow us to nested if statements within if statements, i.e, we
can place an if statement inside another if statement.
// C program to illustrate nested-if statement
#include <stdio.h>

int main()
{
int i = 10;

if (i == 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");

// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}

return 0;
}
if else if statements

 The if else if statements are used when the user has to decide among
multiple options. The C if statements are executed from the top down. As
soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the final else statement will
be executed. if-else-if ladder is similar to the switch statement
// C program to illustrate nested-if statement
#include <stdio.h>

int main()
{
int i = 20;

if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Repetitive statements

 Repetitive statements or looping means executing the same section of code


more than once until the given condition is true. A section of code may either
be executed a fixed number of times or while the condition is true.
 for loop
 while loop
 do-while loop
For Loop

 For statement makes programming more convenient to count iterations of a


loop and works well, where the number of iterations of the loop is known
before the loop entered.
 Syntax:
for (initialization; test condition; loop update)
{
Statement(s);
}
 The main purposed is to repeat statements while the condition remains true,
like the while loop. But in addition, for provide places to specify an
initialization instruction and an increment or decrement of the control
variable instruction. So this loop is specially designed to perform a repetitive
action with a counter.
While Loop

 A while loop statement in C programming language repeatedly executes a


target statement as long as a given condition is true.
Syntax :
while(condition)
{
statement(s);
}
 Here, statement(s) may be a single statement or a block of statements. The
loop iterates while the condition is true. When the condition becomes false,
program control passes to the line immediately following the loop.
do-while

 In C, do...while loop is very similar to while loop. The only difference


between these two loops is that, in while loops, test expression is checked at
first but, in the do-while loop, code is executed at first then the condition is
checked. So, the code is executed at least once in do-while loops.
Syntax :
do
{
statement(s);
}while(condition);
While do-while

It is an entry control loop. It is an exit control loop.

The test condition is evaluated before the loop is


The test condition is evaluated after the loop is executed.
executed.

It uses a keyword while. It uses keywords do and while.

Loop is not terminated with a semicolon. Loop is terminated with a semicolon.

It doesn’t execute the body of loop until and unless the The body of the loop is always executed at least once since
condition is true. the test condition is not checked until the end of the loop.

Example: Example:
#include #include
void main() void main()
{ {
int i; int i;
i=1; i=1;
while(i<=5) do
{ {
printf("%d\n",i); printf("%d\n",i);
} } while(i<=5);

You might also like