C++ के Statements
In this page:
type variable_name = value; // declaration statement
variable_name = expression; // expression statement
function_name(arguments); // function call statement
Statement क्या है?
एक statement C++ में सबसे छोटी complete instruction है जिसे compiler execute करता है, जैसे एक variable declare करना, एक function call करना, या एक expression evaluate करना, और एक C++ program मूल रूप से order में execute होने वाले statements का एक sequence है।
उदाहरण: What is a Statement?
#include <iostream>
int main() {
int x = 5; // declaration statement
x = x + 1; // expression statement
std::cout << x; // function call statement
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Expression Statements
एक expression statement एक expression से बनता है, जैसे एक assignment या एक function call, जिसके बाद एक semicolon आता है, और यह statement की सबसे common किस्म है, जो सिर्फ अपने side effect के लिए execute होती है।
उदाहरण: Expression Statements
#include <iostream>
int main() {
int total = 0;
total = total + 10; // expression statement, executed for its side effect
std::cout << total << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compound Statements (Blocks)
एक compound statement, या block, कई statements को curly braces के अंदर एक साथ group करता है, जिससे कई statements को एक single unit की तरह treat किया जा सकता है जहाँ भी language को सिर्फ एक statement चाहिए, जैसे किसी if या loop के अंदर।
उदाहरण: Compound Statements (Blocks)
#include <iostream>
int main() {
if (true) {
int a = 1;
int b = 2;
std::cout << a + b << std::endl; // block treated as one statement
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declaration Statements
एक declaration statement एक नया variable introduce करता है, इसका type और name specify करते हुए, और optionally एक initial value, उस identifier को बाद के statements में इस्तेमाल के लिए available बनाते हुए।
उदाहरण: Declaration Statements
#include <iostream>
int main() {
int score = 100; // declaration statement: type, name, initial value
std::cout << score << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Semicolons से Statement Termination
braces में enclosed compound statements को छोड़कर, C++ का हर simple statement एक semicolon से terminate होना चाहिए, जो compiler को exactly बताता है कि एक instruction कहाँ खत्म होता है और अगला कहाँ शुरू होता है।
उदाहरण: Statement Termination with Semicolons
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a = 1; int b = 2;
// Print to the console with cout
std::cout << a + b << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
int x = 5जैसे statement के अंत में semicolon छोड़ देना, जो अगली line पर एक compile error cause करता है।if (x > 0);के बाद एक stray semicolon लगाना, जो empty statement को body बना देता है और बाद वाला block हमेशा चलता है।- यह मान लेना कि
{ }block के अंदर declared variables closing brace के बाद भी available हैं, जब वे वहाँ out of scope हो जाते हैं।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: