Posts

Showing posts with the label Flow Control

C++ goto Statement

Image
C++ goto Statement In C++ programming, goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program. Syntax of goto Statement goto label; ... .. ... ... .. ... ... .. ... label: statement; ... .. ... In the syntax above,  label  is an identifier. When  goto label;  is encountered, the control of program jumps to  label:  and executes the code below it. Example: goto Statement // This program calculates the average of numbers entered by user. // If user enters negative number, it ignores the number and // calculates the average of number entered before it. # include <iostream> using namespace std ; int main () { float num , average , sum = 0.0 ; int i , n ; cout << "Maximum number of inputs: " ; cin >> n ; for ( i = 1 ; i <= n ; ++ i ) { cout << "Enter n" << i << ...

Switch..case Statement in C++

Image
Switch..case Statement in C++ The ladder  if..else..if  statement allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in ladder  if..else..if , it is better to use  switch  statement. The switch statement is often faster than if...else (not always). Also, the syntax of switch statement is cleaner and easier to understand. C++ switch...case syntax switch (n) ​{ case constant1: // code to be executed if n is equal to constant1; break; case constant2: // code to be executed if n is equal to constant2; break; . . . default: // code to be executed if n doesn't match any constant } When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case. In the above pseudocode, suppose the value of  n  i...