[SystemC]SystemC Statement and Flow Control

                   SystemC Statement and Flow Control

       All the statements will end with semicolon (;), Number of statements can be grouped by placing them with in curly braces. { };

{
    Statement1;
    Statement2;
    Statement3;
}

1、if and else 

       The if  keyword is used to execute the statement or group of statements based on the condition’s.Below are the few examples,

//if, execution of one statement on condition satisfied
if ( condition ) statement;

//if, execution of block/statement's on condition satisfied
if ( condition ) {
    Statement1;
    Statement2;
}

//if along with else part
if(condition) {
     Statement1;
}
else {
      stetement2;
}

//if, with nested if in else part
if(condition2) {
     Statement1;
}
else  if (condition2) {
   if(condition3)  stetement2;
   else            statement3;
}

       Example:

#include "systemc.h"
int sc_main (int argc, char* argv[]) {
  //declaration and initialization
  int a = 10;
  int b = 30;
   
  //if-else
  if( a > b )
    cout <<" a is greater than b "<<endl;
  else
  {
    cout <<" b is greater than a "<<endl;
    if((b-a) > a)
      cout <<" b-a is greater than a"<<endl;
    else
      cout <<" b-a is less than a "<<endl;
  }
     
  // Terminate simulation
  return 0;
}

       Simulation Output:

b is greater than a 
b-a is greater than a

 2、FOR LOOP 

       for (initialization; condition; increment/decrement) statement;

       for loop works as below,

  • Initialize the loop variable
  • Check for the condition, if condition is satisfied then executes the block
  • Increment/decrement the loop variable value
#include "systemc.h"
int sc_main (int argc, char* argv[]) {
 
  //for-loop
  for(int i=0; i<4; i++)
  {
    cout <<" Value of i = "<<i<<endl;
  }
     
  // Terminate simulation
  return 0;
}

       SIMULATOR OUTPUT:

Value of i = 0
Value of i = 1
Value of i = 2
Value of i = 3

3、WHILE LOOP 

       The while loop will repeats the execution of statement/block until the condition is true.

#include "systemc.h"
int sc_main (int argc, char* argv[]) {
  //declaration and initialization
  int a = 15;
  int b = 10;
 
  //while-loop
  while( a > b ) {
    cout <<" a is greater than b a = "<<a<<" b = "<<b<<endl;
    a--;
  }
 
  cout <<" b is greater than a "<<endl;
     
  // Terminate simulation
  return 0;
}

     SIMULATOR OUTPUT:

 a is greater than b a = 15 b = 10
 a is greater than b a = 14 b = 10
 a is greater than b a = 13 b = 10
 a is greater than b a = 12 b = 10
 a is greater than b a = 11 b = 10
 b is greater than a 

4、DO-WHILE 

       The do-while loop will execute the statement/block and then checks for condition.The difference between do-while and while is, irrespective of the condition the statement/block will be executed at least once in do-while.

#include "systemc.h"
int sc_main (int argc, char* argv[]) {
  //declaration and initialization
  int a = 9;
  int b = 10;
 
  //while-loop
  do{
    cout <<" a is greater than b a = "<<a<<" b = "<<b<<endl;
    a--;
  }while( a > b );
 
  cout <<" b is greater than a "<<endl;
     
  // Terminate simulation
  return 0;
}

    SIMULATOR OUTPUT:

a is greater than b a = 9 b = 10
b is greater than a
发布了185 篇原创文章 · 获赞 118 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/gsjthxy/article/details/105214880