PPPP
PPPP
C++ Variables
Last Updated : 16 Jul, 2024
Variables in C++ is a name given to a memory location. It is the basic unit
of storage in a program.
The value stored in a variable can be changed during program
execution.
A variable is only a name given to a memory location, all the operations
done on the variable effects that memory location.
In C++, all the variables must be declared before use.
How to Declare Variables?
A typical variable declaration is of the form:
// Declaring a single variable
type variable_name;
Output
a
Time Complexity: O(1)
Space Complexity: O(1)
Types of Variables
There are three types of variables based on the scope of variables in C++
Local Variables
Instance Variables
Static Variables
C++
int main()
{
cout << "Size of char : " <<
sizeof(char) << endl;
cout << "Size of int : " << sizeof(int)
<< endl;
return 0;
}
Output
Size of char : 1
Size of int : 4
Size of long : 8
Size of float : 4
Size of double : 8
C++
int main()
{
cout << "Size of char : " <<
sizeof(char) << " byte"
<< endl;
return 0;
}
Output
Size of char : 1 byte
char minimum value: -128
char maximum value: 127
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes
Time Complexity: O(1)
Space Complexity: O(1)
C++ if Statement
Last Updated : 11 Jan, 2024
Decision-making in C++ helps to write decision-driven statements and
execute a particular set of code based on certain conditions.
The C++ 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 executed based on a certain type of condition.
Syntax
if(condition)
{
// Statements to execute if
// condition is true
}
Flowchart
If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by
default if statement will consider the immediate one statement to be
inside its block.
For example:
if(condition)
statement1;
statement2; // Here if the condition is true if block will consider
only statement1 to be inside its block.
C++
#include <iostream>
using namespace std;
int main()
{
int i = 10;
if (i < 15) {
cout << "10 is less than
15 \n";
}
Example 2
C++
#include <iostream>
using namespace std;
int main()
{
int i = 10;
if (i > 15) {
cout << "10 is greater
than 15 \n";
}
Flowchart if statement:
Operation: The condition after evaluation of if-statement will be either
true or false. The if statement in Java accepts boolean values and if the
value is true then it will execute the block of statements under it.
Note: If we do not provide the curly braces ‘{‘ and ‘}’ after
if( condition ) then by default if statement will consider the
immediate one statement to be inside its block.
For example:
if(condition)
statement1;
statement2;
Java
if (i < 15)
System.out.println("10 is
less than 15");
System.out.println("Outside if-
block");
// both statements will be
printed
}
}
Output
10 is less than 15
Outside if-block
Time Complexity: O(1)
Auxiliary Space: O(1)
Dry-Running Example 1:
1. Program starts.
2. i is initialized to 10.
3. if-condition is checked. 10<15, yields true.
3.a) "10 is less than 15" gets printed.
4. "Outside if-block" is printed.
Example 2:
Java
class IfDemo {
public static void
main(String args[])
{
String str =
"GeeksforGeeks";
int i = 4;
// if block
if (i == 4) {
i++;
System.out.println(s
tr);
}
// Executed by default
System.out.println("i =
" + i);
}
}
Output
GeeksforGeeks
i = 5
Time Complexity: O(1)
Auxiliary Space: O(1)
Example no 3: (Implementing if else for Boolean values)
Input -
boolean a = true;
boolean b = false;
Program –
Java
if (a) {
System.out.println("a
is true");
} else {
System.out.println("a
is false");
}
if (b) {
System.out.println("b
is true");
} else {
System.out.println("b
is false");
}
}
}
Output
a is true
b is false
Explanation-
The code above demonstrates how to use an if-else statement in Java with
Boolean values.
The code starts with the declaration of two Boolean variables a and b,
with a set to true and b set to false.
The first if-else statement checks the value of a. If the value of a is true,
the code inside the first set of curly braces {} is executed and the
message “a is true” is printed to the console. If the value of a is false,
the code inside the second set of curly braces {} is executed and the
message “a is false” is printed to the console.
The second if-else statement checks the value of b in the same way. If
the value of b is true, the message “b is true” is printed to the console.
If the value of b is false, the message “b is false” is printed to the
console.
This code demonstrates how to use an if-else statement to make
decisions based on Boolean values. By using an if-else statement, you
can control the flow of your program and execute code only under
certain conditions. The use of Boolean values in an if-else statement
provides a simple and flexible way to make these decisions.
Advantages of If else statement –
The if-else statement has several advantages in programming, including:
1. Conditional execution: The if-else statement allows code to be
executed conditionally based on the result of a Boolean expression. This
provides a way to make decisions and control the flow of a program
based on different inputs and conditions.
2. Readability: The if-else statement makes code more readable by
clearly indicating when a particular block of code should be executed.
This makes it easier for others to understand and maintain the code.
3. Reusability: By using if-else statements, developers can write code
that can be reused in different parts of the program. This reduces the
amount of code that needs to be written and maintained, making the
development process more efficient.
4. Debugging: The if-else statement can help simplify the debugging
process by making it easier to trace problems in the code. By clearly
indicating when a particular block of code should be executed, it
becomes easier to determine why a particular piece of code is not
working as expected.
5. Flexibility: The if-else statement provides a flexible way to control the
flow of a program. It allows developers to handle different scenarios and
respond dynamically to changes in the program’s inputs.
Overall, the if-else statement is a fundamental tool in programming that
provides a way to control the flow of a program based on conditions. It
helps to improve the readability, reusability, debuggability, and flexibility
of the code.
Flowchart if-else:
Example 1:
#include <iostream>
using namespace std;
int main()
{
int i = 20;
// Check if i is 10
if (i == 10)
cout << "i is 10";
// Since is not 10
// Then execute the else
statement
else
cout << "i is 20\n";
return 0;
}
Output
i is 20
Outside if-else block
Explanation:
Program starts.
i is initialized to 20.
if-condition is checked. i == 10, yields false.
flow enters the else block.
“i is 20” is printed
“Outside if-else block” is printed.
Example 2:
C++
#include <iostream>
using namespace std;
int main()
{
int i = 25;
if (i > 15)
cout << "i is greater than
15";
else
cout << "i is smaller than
15";
return 0;
}
Output
i is greater than 15
Java if-else
Last Updated : 02 Apr, 2024
Decision-making in Java helps to write decision-driven statements and
execute a particular set of code based on certain conditions. 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. In this article, we will
learn about Java if-else.
If-Else in Java
If- else together represents the set of Conditional statements in Java that
are executed according to the condition which is true.
Syntax of if-else Statement
if (condition)
{
// Executes this block if // condition is true
}else
{
// Executes this block if // condition is false
}
Java
// Java program to illustrate if-else statement
class IfElseDemo { public static void main(String args[]) { int i = 20;
Output
i is greater than 15
Outside if-else block
Nested if statement in Java
In Java, we can use nested if statements to create more complex
conditional logic. Nested if statements are if statements inside other if
statements.
Syntax:
if (condition1) {
// code block 1
if (condition2) {
// code block 2
}
}
Below is the implementation of Nested if statements:
Java
// Java Program to implementation// of Nested if statements
Output
You are eligible to donate blood.
Note: The first print statement is in a block of “if” so the
second statement is not in the block of “if”. The third print
statement is in else but that else doesn’t have any
corresponding “if”. That means an “else” statement cannot
exist without an “if” statement.
Frequently Asked Questions
Example 1:
Below program illustrates the use if else if ladder in C++.
C++
int main()
{
int i = 20;
// Check if i is 10
if (i == 10)
cout << "i is 10";
// Since i is not 10
// Check if i is 15
else if (i == 15)
cout << "i is 15";
// Since i is not 15
// Check if i is 20
else if (i == 20)
cout << "i is 20";
return 0;
}
Output
i is 20
Explanation:
Program starts.
i is initialized to 20.
condition 1 is checked. 20 == 10, yields false.
condition 2 is checked. 20 == 15, yields false.
condition 3 is checked. 20 == 20, yields true. “i is 20” gets printed.
“Outside if-else-if” gets printed.
Program ends.
Example 2:
C++
// C++ program to illustrate if-else-
if ladder
#include <iostream>
using namespace std;
int main()
{
int i = 25;
import java.io.*;
class GFG {
public static void main(String[]
args)
{
// initializing expression
int i = 20;
// condition 1
if (i == 10)
System.out.println("i is
10\n");
// condition 2
else if (i == 15)
System.out.println("i is
15\n");
// condition 3
else if (i == 20)
System.out.println("i is
20\n");
else
System.out.println("i is
not present\n");
System.out.println("Outside if-
else-if");
}
}
Output:
i is 20
Outside if-else-if
Java
import java.io.*;
class GFG {
public static void main(String[]
args)
{
// initializing expression
int i = 20;
// condition 1
if (i < 10)
System.out.println("i is
less than 10\n");
// condition 2
else if (i < 15)
System.out.println("i is
less than 15\n");
// condition 3
else if (i < 20)
System.out.println("i is
less than 20\n");
else
System.out.println("i is
greater than "
+ "or
equal to 20\n");
System.out.println("Outside if-
else-if");
}
}
Output:
i is greater than or equal to 20
Outside if-else-if
In this way, the flow of the program depends on the set of conditions that
are written. This can be more understood by the following flow charts: