Programming Lecture by Bashir
Programming Lecture by Bashir
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 ( && )
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
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");
}
int main()
{
int i = 20;
if (i < 15) {
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
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);